Search code examples
rxandroidble

Issue with Notification and Disconnect


We are trying to upgrade existing app with your framework, other things are working fine like connection/read/write however we are facing issues with Notification/Disconnect

Can you please guide for following scenarios:-

  1. Need call back for disconnection
  2. Notification not working we are not able to receive any notification alert
  3. Is there any method to check characteristics of devices, as we have different devices and some characteristics are not present in all devices, when we try to read/write non present chacraterstics on devices, it throws exception and app crashes

Code :-

connection.writeDescriptor(
    Defs.SVC_AUTOMATIONIO_UUID, 
    Defs.CHAR_AUTOMATION_IO,
    Defs.DESC_CLIENT_CHAR_CONFIGURATION_UUID,
    BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
)
    .subscribe(
        this::onWriteSuccess,
        this::onWriteFailure
    );

connection.setupNotification(iCharUuid)
    .flatMap(notificationObservable -> notificationObservable)
    .subscribe(
        this::onNotificationReceived,
        this::onConnectionFailure
    );

Thanks Swayam


Solution

  • In general you don't have to write descriptor manually to enable notifications. The library does it for you.

    Try: (example)

    rxBleConnection.setupNotification(Defs.DESC_CLIENT_CHAR_CONFIGURATION_UUID)
                        .flatMap(notificationObservable -> notificationObservable)
                        .subscribe(this::onNotificationReceived, this::onNotificationSetupFailure);
    

    In order to get callback for disconnection: (example)

    1. You can observe onError from establishConnection method.
    2. You can setup connection status observable

    bleDevice.observeConnectionStateChanges().subscribe(this::onConnectionStateChange);

    To check characteristics you can go with service discovery: (example)

     bleDevice.establishConnection(this, false)
                    .flatMap(RxBleConnection::discoverServices)
                    .first() // Disconnect automatically after discovery
                    .subscribe(this::processDiscoveredServices, this::onConnectionFailure);