Android Bluetooth Low Energy Pairing

后端 未结 3 481
庸人自扰
庸人自扰 2020-12-22 21:38

How to pair a Bluetooth Low Energy(BLE) device with Android to read encrypted data.

Using the information in the Android BLE page, I am able to disc

相关标签:
3条回答
  • 2020-12-22 21:42

    When you get the GATT_INSUFFICIENT_AUTHENTICATION error, the system starts the bonding process for you. In the example below I'm trying to enable notifications and indications on glucose monitor. First I'm enabling the notifications on Glucose Measurement characteristic which can cause the error to appear.

    @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                if (GM_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                    mCallbacks.onGlucoseMeasurementNotificationEnabled();
    
                    if (mGlucoseMeasurementContextCharacteristic != null) {
                        enableGlucoseMeasurementContextNotification(gatt);
                    } else {
                        enableRecordAccessControlPointIndication(gatt);
                    }
                }
    
                if (GM_CONTEXT_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                    mCallbacks.onGlucoseMeasurementContextNotificationEnabled();
                    enableRecordAccessControlPointIndication(gatt);
                }
    
                if (RACP_CHARACTERISTIC.equals(descriptor.getCharacteristic().getUuid())) {
                    mCallbacks.onRecordAccessControlPointIndicationsEnabled();
                }
            } else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                // this is where the tricky part comes
    
                if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
                    mCallbacks.onBondingRequired();
    
                    // I'm starting the Broadcast Receiver that will listen for bonding process changes
    
                    final IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
                    mContext.registerReceiver(mBondingBroadcastReceiver, filter);
                } else {
                    // this situation happens when you try to connect for the second time to already bonded device
                    // it should never happen, in my opinion
                    Logger.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
                    // I don't know what to do here
                    // This error was found on Nexus 7 with KRT16S build of Andorid 4.4. It does not appear on Samsung S4 with Andorid 4.3.
                }
            } else {
                mCallbacks.onError(ERROR_WRITE_DESCRIPTOR, status);
            }
        };
    

    Where the mBondingBroadcastReceiver is:

    private BroadcastReceiver mBondingBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
    
            Logger.d(TAG, "Bond state changed for: " + device.getAddress() + " new state: " + bondState + " previous: " + previousBondState);
    
            // skip other devices
            if (!device.getAddress().equals(mBluetoothGatt.getDevice().getAddress()))
                return;
    
            if (bondState == BluetoothDevice.BOND_BONDED) {
                // Continue to do what you've started before
                enableGlucoseMeasurementNotification(mBluetoothGatt);
    
                mContext.unregisterReceiver(this);
                mCallbacks.onBonded();
            }
        }
    };
    

    Remember to unregister the broadcast receiver when exiting the activity. It may have not been unregistered by the receicver itself.

    0 讨论(0)
  • 2020-12-22 21:43

    You might need to check the Kernel smp.c file, which method of paring it invoke for paring. 1) passkey 2)Just work or etc . i guess if it will be able to invoke MIMT and passkey level of security , there will not be any authentication issue. Make sure all flags is set to invoke the SMP passkey methods. track by putting some print in smp.c file.

    A solution which works in ICS : with btmgmt tool in android and hooking it in encryption APIs. with passkey or any other methods. it works. You might need to add the passkey APIs in btmgmt from latest bluez code.

    0 讨论(0)
  • 2020-12-22 21:47

    i think new android 4.4 provide pairing method. same problem already i am facing so wait for update and hope over problem solved createBond() method .

    http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#setPairingConfirmation%28boolean%29

    0 讨论(0)
提交回复
热议问题