Search code examples
rxandroidble

RxAndroidBle rxBleConnection.writeCharacteristic


I am running the entire sample application provided in RxAndroidBle from scanning to discover services to writeCharacteristic. I am trying to debug into the flow and put a break point in onWriteClick() of the CharacteristicOperationExampleActivity.java file. Clicking the WRITE button does nothing. Break point wasn't caught.

Reading the instruction from the blog RxAndroidBle

Stating that discovering characteristic is optional for write. But the way this sample app's activities are setup, one has to go thru discovering the characteristics before the Characteristic Operation page will be shown. On the characteristic page, I selected the read/write characteristic entry to get to the Operation page. Isn't that the correct way to operate the app?

Also, is there a way to handle writeCharacteristic without having to discover its characteristics? I don't want to show the characteristic view and the user has to pick the correct characteristic to be able to read and write to the BLE device.

In any case, the sample app discovered my BLE device and connected to it but failed to write to it however. Does anyone have experience with RxAndroidBle, please help.


Solution

  • There seems to be a bug in the example - I couldn't make it to work (despite connecting the buttons were disabled) - will need to look into it. As for the quick-fix you can replace the onConnectToggleClick() method with:

    @OnClick(R.id.connect)
    public void onConnectToggleClick() {
    
        if (isConnected()) {
            triggerDisconnect();
        } else {
            connectionObservable
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnSubscribe(() -> connectButton.setText("Connecting"))
                    .subscribe(
                            rxBleConnection -> {
                                Log.d(getClass().getSimpleName(), "Hey, connection has been established!");
                                updateUI();
                            },
                            this::onConnectionFailure
                    );
        }
    }
    

    The sample application is not meant to be run with any particular BLE device so to show possible BluetoothCharacteristics of an unknown device it needs to perform an explicit discovery to present them to the user. When using the library with a known device you can safely use UUIDs of BluetoothCharacteristics you're interested in without performing the discovery (it will be done underneath either way but you don't need to call it explicitly).