0

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193

1 Answers1

0

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).

Dariusz Seweryn
  • 3,212
  • 2
  • 14
  • 21
  • OK, that works. I didn't realize once in the Operation Page of the sample app, I need to click on CONNECT button before I can write to the BLE device. What is this level of connection for? I initially thought that once the device was discovered and connected with the rxBleConnection object, we are good to go for reading/writing to the device. – Khai Trinh Sep 30 '16 at 16:15
  • Of course you're right but in the sample application every activity is handling connection on it's own. If the activity pauses the connection is closed. – Dariusz Seweryn Sep 30 '16 at 16:47
  • Also, how can I streamline this code by automatically perform connection without this extra step with the connect button in the Operation Page? – Khai Trinh Sep 30 '16 at 16:48
  • How can I change the code in the ConnectionExampleActivity.java such that I can begin read/write to the BLE device once connected assuming I know the READ/WRITE characteristic UUIDs? Thanks, Khai – Khai Trinh Sep 30 '16 at 16:58
  • You can just `.establishConnection()` then `.flatMap()` it and perform `.writeCharacteristic()` or whatever you need. I encourage you to check out other topics with `rxandroidble` tag on Stackoverflow for inspiration (for instance here: http://stackoverflow.com/questions/38523715/how-to-establish-connection-and-then-read-characteristics-in-one-activity or http://stackoverflow.com/questions/38940943/how-do-i-effectively-read-temperature-from-two-ble-devices-at-the-same-time ) – Dariusz Seweryn Sep 30 '16 at 17:00