android 4.3 Bluetooth ble don't called onCharacteristicRead()

后端 未结 1 1980
没有蜡笔的小新
没有蜡笔的小新 2020-12-06 14:35

I\'ve set the notification into android, It is not calling to method onCharacteristicRead()???? It does not enter into the function. Why it is happening so??

相关标签:
1条回答
  • 2020-12-06 15:22

    First of all onCharacteristicRead will fire if you have read a characteristic by:

     mBluetoothGatt.readCharacteristic(characteristic);
    

    Reading a characteristic and setting up notifications are two different things. What is the type of your characteristic you want to get data from?

    Is it:

    • read
    • notify
    • indicate

    If it is read you can read the characteristic using the mBluetoothGatt.readCharacteristic(characteristic); method but if its notify or indicate first you will have to read the characteristic's descriptor by calling:

    mBluetoothGatt.readDescriptor(ccc);
    

    Once you read it, it should return data by calling the onDescriptorRead callback.
    Here you can set up (subscribe) to the charactersitic through either notification or indication by calling:

    mBluetoothGatt.setCharacteristicNotification(characteristic, true)
    

    once it returns true you will need to write to the descriptor again (the value of notification or indication)

    BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(CCC);
    clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    // or
    //clientConfig.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    mBluetoothGatt.writeDescriptor(clientConfig);
    

    Once this is done you will get notifications throuhg onCharacteristicChanged callback every time the characteristic changes.

    you can read more about Bluetooth connection on Android here
    and about Bluetooth Characteristics here

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