4
votes

I am working with a BLE device in ANDROID.

Here is where I set up the endpoint 0000fff4-0000-1000-8000-00805f9b34fb to receive notifications (CLIENT_CHARACTERISTIC_CONFIG is 00002902-0000-1000-8000-00805f9b34fb

 public void k2DigitalNotification(BluetoothGattCharacteristic characteristic,
                               boolean enabled)
{
    Boolean myStatus;

    if (MY_BLUETOOTH_SERVICE.equals(characteristic.getUuid()))
    {
        Log.v(TAG, "Characteristic: " + characteristic.getUuid());

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
                UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        myStatus = mBluetoothGatt.writeDescriptor(descriptor);
        Log.v(TAG,"Write Status::"+myStatus);
        bluetoothGatt.setCharacteristicNotification(characteristic, true);

    }
}

Here is the code where I write out to the device. THIS WORKS! 100%, the data shows up at the embedded BLE device. (endpoint is 0000fff1-0000-1000-8000-00805f9b34fb )

   public void k2digitalWriteToCharacteristic(BluetoothGattCharacteristic characteristic) {
    if (BLE_ENDPOINT.equals(characteristic.getUuid()))
    {

        Log.v(TAG,"0Xfff1");

        byte[] data3Send = new byte[4];
        data3Send[0] = 0x31;
        data3Send[1] = 0x01;
        data3Send[2] = 0x5a;
        data3Send[2] = 0x0d;;

        characteristic.setValue(data3Send);
        boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
        Log.v(TAG, "Status is:" + String.valueOf(status));
    }
}

The BLE device is sending the data out.....I've verified this in apps like nRFMaster from Nordic Semi or BLEScanner from Bluepixel.

But I NEVER EVER see the callback.

        public void onCharacteristicChanged(BluetoothGatt gatt,
                                        BluetoothGattCharacteristic characteristic) {
        Log.v(TAG,"A Characteristic Change?!?!?!");
        byte[] data = characteristic.getValue();
        Log.v(TAG,"Here is the data: "+data[0]);
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }

Any help? I've been banging my head against the wall all week.

1
Enabling notifications is quite tricky. I could post an answer to various things you could try and what you have to watch out for, but for a quick sanity check I would suggest trying SweetBlue, specifically BleDevice.enableNotify(). This is my lib but honestly it's the fastest way to know whether the problem is with your Android-side code or elsewhere, then you can go from there.Doug Koellmer
Thanks for the tip. I'll download this and try it out now.K2Digital
Have you verified the characteristic's data can be received by indication, rather than notification?Zimano
Thanks for the heads up Zimano. I had tried that. Actually the answer was a bug on my part. Note in the packet above.....I overwrote the 5a because I didn't advance the array index in the packet I was sending......dumb mistakeK2Digital

1 Answers

0
votes

My issue was regarding the ble device was busy. I was attempting to read and subscribe without much gap in time and the ble device was busy. you can check if the enable notification was successful with the following code:

Boolean status = bluetoothGatt.writeDescriptor(descriptor);

This will return true if the writeDescriptor was successful and false otherwise - hence helping you debug if the ble device is busy.