1

I've been working on BLE read and write on google's nexus 6&9.

I want to write a characteristic on remote device and notify the remote device once written.

The following code writes characteristics

if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
    byte[] test = hexStringToByteArray("3132333435363738393031323334353637383930");
    //byte[] value = new byte[1];
    //value[0] = (byte) (21 & 0xFF);
    Log.d("text",characteristic.getDescriptors().size()+"");
    BluetoothGattDescriptor descriptor1 =  characteristic.getDescriptor(UUID.fromString(serviceOneCharUuid));
    descriptor1.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    characteristic.setValue(test);
    if(mBluetoothGatt.writeDescriptor(descriptor1)){Log.d("text","writedescriptor");}
    if(mBluetoothGatt.writeCharacteristic(characteristic)){Log.d("text","setValue");}}

I've seen some posts that told me to write descriptor first but it doesn't work.

I tried to write to BLE usb device and it shows the write function works as expected. And also, the callback functions on local device works as well.

However, the oncharacteristicchanged function on remote deviced is not called Here's the code of all callback functions writted in a class named BluetoothLeService

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){
        Log.d("text",new String(characteristic.getValue()));
    }
    @Override
    public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            Log.d("text","on read");
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
        Log.d("text", new String(characteristic.getValue()));
    }

Anyone knows what may be wrong in these codes or I missed anything?

m.s.
  • 16,063
  • 7
  • 53
  • 88
Edward
  • 161
  • 1
  • 1
  • 12

1 Answers1

0

I had a similar issue getting the onCharacteristicChanged to get called. The way I got it to work is waiting until after onDescriptorWrite is called before writing the characteristic to a BLE device.

Yoong
  • 54
  • 3