Bluetooth not connecting on 4.4.2

前端 未结 2 1384
温柔的废话
温柔的废话 2020-12-29 22:37

I\'ve got a Bluetooth device that has connected on all versions of Android that I have tried prior to 4.4.2. Now, it\'s not connecting on the Galaxy Tab 4 or the S3. The Tab

2条回答
  •  Happy的楠姐
    2020-12-29 23:16

    Try this code, this is working android 4.4.2 on nexus 7

    private boolean refreshDeviceCache(BluetoothGatt gatt){
        try {
            BluetoothGatt localBluetoothGatt = gatt;
            Method localMethod = localBluetoothGatt.getClass().getMethod("refresh", new Class[0]);
            if (localMethod != null) {
               boolean bool = ((Boolean) localMethod.invoke(localBluetoothGatt, new Object[0])).booleanValue();
                return bool;
             }
        } 
        catch (Exception localException) {
            Log.e(TAG, "An exception occured while refreshing device");
        }
        return false;
    }
    
    
        public boolean connect(final String address) {
               if (mBluetoothAdapter == null || address == null) {
                Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
                    return false;
            }
                // Previously connected device. Try to reconnect.
                if (mBluetoothGatt != null) {
                    Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
                  if (mBluetoothGatt.connect()) {
                        return true;
                   } else {
                    return false;
                   }
            }
    
            final BluetoothDevice device = mBluetoothAdapter
                    .getRemoteDevice(address);
            if (device == null) {
                Log.w(TAG, "Device not found.  Unable to connect.");
                return false;
            }
    
            // We want to directly connect to the device, so we are setting the
            // autoConnect
            // parameter to false.
            mBluetoothGatt = device.connectGatt(MyApp.getContext(), false, mGattCallback));
            refreshDeviceCache(mBluetoothGatt);
            Log.d(TAG, "Trying to create a new connection.");
            return true;
    

    }

提交回复
热议问题