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:
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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…