Search code examples
androidbluetooth-lowenergyandroid-bluetoothrxandroidble

RxAndroidBle - Write characteristic and receive notifications - No notifications received


I'm using RxAndroidBle library in my app to do some write/read characteristics. Everything works fine, except one use-case I can't get around.

The problem: I need to write a timestamp to a specific characteristic (INTERVAL_START_TIMESTAMP), all records (stored on the BLE device) newer than that timestamp will be pushed on another characteristic (INTERVAL_RECORD). But I don't get any notifications at all...

This is my code:

var connection: RxBleConnection? = null
    MyApplication.getRxBleClient(context).getBleDevice(mac)
            .establishConnection(false)
            .flatMap {
                connection = it
                connection?.setupIndication(UUID.fromString(Characteristic.INTERVAL_RECORD.uuid))
            }
            .doOnNext {
                connection?.writeCharacteristic(UUID.fromString(Characteristic.INTERVAL_START_TIMESTAMP.uuid), ByteUtils.toByteArray(timestamp)
                        ?.subscribe({
                            Log.e("Interval_start_timestamp", "write " + ByteUtils.toInt(it))
                        }, {
                            Log.e("Interval_start_timestamp", "write " + it.message)
                        })
            }
            .flatMap { it }
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                Log.e("Notification received", "" + ByteUtils.toInt(it))
            }, {
                Log.e("Notification error", "" + it.message)
            })

In my logs, I can see that notification is set up correctly, and that the writeCharacteristic is successful as well. I just don't receive any notifications. Nothing is wrong with the BLE device, because everything is working fine on iOS.

Any help or suggestions?

Edit: Here are my logs:

D/BluetoothGatt: connect() - device: DE:E4:DD:A1:D9:AC, auto: false
D/BluetoothAdapter: isSecureModeEnabled
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=5e7c5a4a-2fda-438c-b83d-4f944b5b6e79
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=8
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=8 device=DE:E4:DD:A1:D9:AC
D/RxBle#BluetoothGatt$1: onConnectionStateChange newState=2 status=0
D/RxBle#ClientOperationQueue: FINISHED ConnectOperation(184940035) in 4124 ms
D/RxBle#ConnectionOperationQueue: QUEUED   ServiceDiscoveryOperation(53595065)
D/RxBle#ConnectionOperationQueue: STARTED  ServiceDiscoveryOperation(53595065)
D/BluetoothGatt: discoverServices() - device: DE:E4:DD:A1:D9:AC
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=6 latency=0 timeout=500 status=0
D/BluetoothGatt: onSearchComplete() = Device=DE:E4:DD:A1:D9:AC Status=0
D/RxBle#BluetoothGatt$1: onServicesDiscovered status=0
V/RxBle#ServiceDiscoveryOperation: Preparing services description
D/BluetoothGatt: onConnectionUpdated() - Device=DE:E4:DD:A1:D9:AC interval=39 latency=0 timeout=500 status=0
V/RxBle#ServiceDiscoveryOperation: --------------- ====== Printing peripheral content ====== ---------------
PERIPHERAL ADDRESS: DE:E4:DD:A1:D9:AC
PERIPHERAL NAME: null
-------------------------------------------------------------------------

Primary Service - Generic Access (00001800-0000-1000-8000-00805f9b34fb)
Instance ID: 1
-> Characteristics:
    * Device Name (00002a00-0000-1000-8000-00805f9b34fb)
      Properties: [ READ WRITE ]
    * Appearance (00002a01-0000-1000-8000-00805f9b34fb)
      Properties: [ READ ]
    * Peripheral Preferred Connection Parameters (00002a04-0000-1000-8000-00805f9b34fb)
      Properties: [ READ ]

Primary Service - Generic Attribute (00001801-0000-1000-8000-00805f9b34fb)
Instance ID: 8
-> Characteristics:
    * Service Changed (00002a05-0000-1000-8000-00805f9b34fb)
      Properties: [ INDICATE ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)

Primary Service - Unknown service (e3ba1523-2785-005b-6676-d476eabca48e)
Instance ID: 12
-> Characteristics:
    * Unknown characteristic (e3ba1524-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1526-2785-005b-6676-d476eabca48e)
      Properties: [ READ INDICATE ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1527-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]
    * Unknown characteristic (e3ba1528-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]
    * Unknown characteristic (e3ba1529-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba152a-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]
    * Unknown characteristic (e3ba152c-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]
    * Unknown characteristic (e3ba152d-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]
    * Unknown characteristic (e3ba152e-2785-005b-6676-d476eabca48e)
      Properties: [ READ ]
    * Unknown characteristic (e3ba152f-2785-005b-6676-d476eabca48e)
      Properties: [ READ ]
    * Unknown characteristic (e3ba1530-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1531-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1532-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1538-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba1539-2785-005b-6676-d476eabca48e)
      Properties: [ READ NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (e3ba153a-2785-005b-6676-d476eabca48e)
      Properties: [ READ WRITE ]

Primary Service - Unknown service (00001530-1212-efde-1523-785feabcd123)
Instance ID: 53
-> Characteristics:
    * Unknown characteristic (00001532-1212-efde-1523-785feabcd123)
      Properties: [ WRITE_NO_RESPONSE ]
    * Unknown characteristic (00001531-1212-efde-1523-785feabcd123)
      Properties: [ WRITE NOTIFY ]
      -> Descriptors: 
        * Client Characteristic Configuration (00002902-0000-1000-8000-00805f9b34fb)
    * Unknown characteristic (00001534-1212-efde-1523-785feabcd123)
      Properties: [ READ ]
--------------- ====== Finished peripheral content ====== ---------------
D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
D/RxBle#ConnectionOperationQueue: QUEUED   DescriptorWriteOperation(82308959)
D/RxBle#ConnectionOperationQueue: FINISHED ServiceDiscoveryOperation(53595065) in 1235 ms
D/RxBle#ConnectionOperationQueue: STARTED  DescriptorWriteOperation(82308959)
D/RxBle#BluetoothGatt$1: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
D/RxBle#ConnectionOperationQueue: QUEUED   CharacteristicWriteOperation(85617269)
D/RxBle#ConnectionOperationQueue: STARTED  CharacteristicWriteOperation(85617269)
D/RxBle#BluetoothGatt$1: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
E/Interval_start_timestamp: write 1548979200
D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms

Solution

  • Assuming the notifications/indications being triggered by a write to a proper characteristic it looks like the information flow is correct. i.e.

    D/BluetoothGatt: setCharacteristicNotification() - uuid: e3ba1526-2785-005b-6676-d476eabca48e enable: true
    D/RxBle#ConnectionOperationQueue: STARTED  DescriptorWriteOperation(82308959)
    D/RxBle#BluetoothGatt$1: onDescriptorWrite descriptor=00002902-0000-1000-8000-00805f9b34fb status=0
    D/RxBle#ConnectionOperationQueue: FINISHED DescriptorWriteOperation(82308959) in 91 ms
    D/RxBle#ConnectionOperationQueue: STARTED  CharacteristicWriteOperation(85617269)
    D/RxBle#BluetoothGatt$1: onCharacteristicWrite characteristic=e3ba1527-2785-005b-6676-d476eabca48e status=0
    E/Interval_start_timestamp: write 1548979200
    D/RxBle#ConnectionOperationQueue: FINISHED CharacteristicWriteOperation(85617269) in 92 ms
    

    First setting the notification then writing the characteristic and no infomation about any errors. In this situation I think that the problem is with assumptions and would double check if:

    1. Correct UUID is used for the notification/indication characteristic
    2. Correct UUID is used for the characteristic that should trigger notifications
    3. The value that is written onto characteristic that should trigger notifications is correct (e.g. if no mistake was made in ByteUtils.toByteArray(timestamp) conversion - i.e. endianness)