K2Digital
K2Digital

Reputation: 603

Not Receiving BLE Notifications in Android

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.

Upvotes: 4

Views: 2076

Answers (1)

JoelWass
JoelWass

Reputation: 575

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.

Upvotes: 0

Related Questions