android 4.3 Bluetooth ble don't called onCharacteristicRead()

流过昼夜 提交于 2019-11-28 00:26:06

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!