Ble receipt error code - 128 when writing to the characteristic

I am using the following react-native-ble-manager reaction library

I am trying to perform read and write operations on a BLE device. I am successfully performing a read operation. But I get an error code of 128 when writing to a BLE device.

firstly, I include a notification for the characteristics -

BleManager.startNotification(peripheralId, serviceId, characteristicId) 

Writing - like this -
converting the value of "hex" to base64 -

  const base64String = new Buffer('0x00B00050D0', 'hex').toString('base64'); BleManager.write(peripheralId, serviceId, characteristicId, base64Value) 

Enter Operation Return Error Code -128

:(

UPDATE - This is a code snippet to start notification and record values, the full 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 + " )"); } 
+6
source share
1 answer

This is a resource error. Are you sure you wrote the handle? If you do not set the descriptor (BluetoothGattDescriptor) and just call the record, you will get this error.

Here is an example:

 protected static final UUID CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"); public boolean setCharacteristicNotification(BluetoothDevice device, UUID serviceUuid, UUID characteristicUuid, boolean enable) { if (IS_DEBUG) Log.d(TAG, "setCharacteristicNotification(device=" + device.getName() + device.getAddress() + ", UUID=" + characteristicUuid + ", enable=" + enable + " )"); BluetoothGatt gatt = mGattInstances.get(device.getAddress()); //I just hold the gatt instances I got from connect in this HashMap BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); gatt.setCharacteristicNotification(characteristic, enable); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CHARACTERISTIC_UPDATE_NOTIFICATION_DESCRIPTOR_UUID); descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : new byte[] { 0x00, 0x00 }); return gatt.writeDescriptor(descriptor); //descriptor write operation successfully started? } 

Source

+2
source

Source: https://habr.com/ru/post/1012971/


All Articles