7

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 + " )");
}
Subham
  • 503
  • 1
  • 9
  • 22
  • According to [this](https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.1.0_r1/stack/include/gatt_api.h#49) that error means `GATT_NO_RESOURCES`. Not sure what that means though – Distjubo Jan 03 '17 at 17:15
  • Just out of curiousity, what happens when you use `writeWithoutResponse` instead of `write`? Does that fix your issue? – Distjubo Jan 03 '17 at 17:19
  • @Distjubo I tried it with writeWithoutResponse and it didn't throw any error but problems are 1. I am expecting a response after write 2. Characteristic type is Writable not writeWithoutResponse – Subham Jan 04 '17 at 05:48

1 Answers1

3

This is a no resources error. Are you sure you wrote the descriptor? If you don't set the descriptor (BluetoothGattDescriptor) and just call write, you'll 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

Community
  • 1
  • 1
Faraz
  • 643
  • 4
  • 15
  • Yes, I am doing this in similar way. First I do enable notification for characteristic and trying to write. But it always return with error code -128. I will also post my code soon. – Subham Jan 09 '17 at 13:14
  • Okay, please let me know once you have updated the post with the latest code. – Faraz Jan 10 '17 at 03:19
  • I have pushed my code here -https://github.com/shubhapp/BleManager/blob/master/Application/src/main/java/com/km2/blemanager/connection/BleConnectionManager.java. I observed one thing today - When I am trying to write at very first time I am getting status 128 but after, that I am always getting status 0 until I disconnect and connect again. But I never received any output value – Subham Jan 10 '17 at 10:40
  • @Subham Looked through your code. You aren't setting a descriptor. https://github.com/shubhapp/BleManager/blob/master/Application/src/main/java/com/km2/blemanager/connection/BleConnectionManager.java#L127 – Faraz Jan 10 '17 at 18:36
  • I have updated the question please look. Please let me know what i am missing in above code snippet. – Subham Jan 11 '17 at 05:17
  • I don't have the BLE device you have to test this, so you'll have to debug yourself. See some samples from GitHub which accomplish something similar: http://www.programcreek.com/java-api-examples/index.php?api=android.bluetooth.BluetoothGattDescriptor Good luck! – Faraz Jan 11 '17 at 17:12
  • @Faraz I am also facing same issue . but in my case it giving me in reading characteristic.My descriptor also giving me success in onDescriptorwrite can you please guide me.? – android_sh Oct 30 '17 at 14:14