Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ble-getting error code - 128 while writing to characterstic

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 + " )");
}
like image 773
Subham Avatar asked Dec 07 '16 11:12

Subham


People also ask

What is characteristic in Ble?

A characteristic object represents a characteristic of a Bluetooth® Low Energy peripheral device. If read or write are supported in the object Attributes property, you can read characteristic values using read or you can write characteristic values using write .

What is client characteristic configuration?

The "Client Characteristic Configuration" descriptor contains information about whether notification or indication is enabled or disabled. You can use read to get the current data.

What is service UUID in Ble?

A Universally Unique Identifier (UUID) is a globally unique 128-bit (16-byte) number that is used to identify profiles, services, and data types in a Generic Attribute (GATT) profile. For efficiency, the Bluetooth® Low Energy (BLE) specification adds support for shortened 16-bit UUIDs.


1 Answers

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

like image 197
Faraz Avatar answered Oct 20 '22 20:10

Faraz