I am using following react library react-native-ble-manager
I am trying to perform read and write operations on BLE device.I am successfully able to perform read operation. But I am getting error code 128 while writing to BLE device.
first, I am enabling notification for characteristic -
BleManager.startNotification(peripheralId, serviceId, characteristicId)
Writing is like this -
converting 'hex' value to base64 string -
const base64String = new Buffer('0x00B00050D0', 'hex').toString('base64');
BleManager.write(peripheralId, serviceId, characteristicId, base64Value)
Write operation return error code -128
:(
UPDATE -- This is code snippet to start notification and write value- complete file can be found here- BluetoothLeService.java
public void writeCharacteristic(BleCharacteristic bleCharacteristic, String inputValue) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
if (!bleCharacteristic.isNotificationStarted()) {
Log.w(TAG, "Notification not started please start notification");
return;
}
BluetoothGattCharacteristic bluetoothGattCharacteristic = bleCharacteristic.getBluetoothGattCharacteristic();
bluetoothGattCharacteristic.setValue(inputValue);
mBluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic);
}
public void setCharacteristicNotification(BleCharacteristic bleCharacteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
boolean enable = !bleCharacteristic.isNotificationStarted();
Log.d(TAG, "setCharacteristicNotification(device=" + mBluetoothDeviceAddress + ", UUID="
+ bleCharacteristic.getUUID().toString() + ", enable=" + enable + " )");
BluetoothGattCharacteristic characteristic = mBluetoothGatt.getService(bleCharacteristic.getServiceUUID()).getCharacteristic(bleCharacteristic.getUUID());
mBluetoothGatt.setCharacteristicNotification(characteristic, enable);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[]{0x00, 0x00});
boolean result = mBluetoothGatt.writeDescriptor(descriptor);
bleCharacteristic.setNotificationStarted(result);
Log.d(TAG, "setCharacteristicNotification(device=" + mBluetoothDeviceAddress + ", UUID="
+ bleCharacteristic.getUUID().toString() + ", enabled=" + result + " )");
}