Read data continuously from the ble in the Android application
I want to continuously read data from ble hardware in Android application through Bluetooth. The connection is completed. I can send data from the application to ble, but I can't read data from ble
Oncharactersticchanged method must be called when getting some data from ble, but this callback method was not called. I want to notify ble
writeChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(BLEUtils.CLIENT_CHARACTERISTIC_CONfig_UUID, BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
// Enable local notifications
gatt.setCharacteristicNotification(writeChar, true);
boolean succes = gatt.writeDescriptor(descriptor);
It returns false
resolvent:
I recommend that you set enable when subscribing to features_ NOTIFICATION_ Value. This is my code to do this:
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
List<BluetoothGattService> services = getSupportedGattServices();
for (BluetoothGattService service : services) {
if (!service.getUuid().equals(UUID_TARGET_SERVICE))
continue;
List<BluetoothGattCharacteristic> gattcharacteristics =
service.getcharacteristics();
// Loops through available characteristics.
for (BluetoothGattCharacteristic gattCharacteristic : gattcharacteristics) {
if (!gattCharacteristic.getUuid().equals(UUID_TARGET_CHARACTERISTIC))
continue;
final int charaProp = gattCharacteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
setCharacteristicNotification(gattCharacteristic, true);
} else {
Log.w(TAG, "Characteristic does not support notify");
}
}
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
Then you can subscribe to features through notification:
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
:BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
After that, you can start writing to the device:
@Override
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
if (descriptor.getCharacteristic().getUuid().equals(UUID_TARGET_CHARACTERISTIC)) {
Log.i(TAG, "Successfully subscribed");
}
byte[] data = <Your data here>;
BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE);
BluetoothGattCharacteristic charac = Service
.getCharacteristic(UUID_TARGET_CHARACTERISTIC);
charac.setValue(data);
mBluetoothGatt.writeCharacteristic(charac);
} else {
Log.e(TAG, "Error subscribing");
}
}
And you will receive a callback oncharactersticchanged to read without actually calling the read operation
Remember that mbluetoohgatt can only process one operation at a time. If you perform another operation without completing the previous one, it will not be put in the queue, but will return false