Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#165] Added support for native BluetoothGattCallback usage in custom… #237

Merged
merged 1 commit into from
Jun 29, 2017

Conversation

uKL
Copy link
Collaborator

@uKL uKL commented Jun 29, 2017

Added support for native BluetoothGattCallback usage in custom operations.

Really silly performance test:

  • based on JUnit
  • invoke onCharacteristicRead like the system would do 1000000 times
  • assert on number of read events received from RxJava based implementation and the native one
  • test does not include load and delay introduces by the ExecutorService, wrapping emissions through the RxJava API

RxJava API @ 1000000 calls => 7905ms (0.008ms/call)
Native API @ 1000000 calls => 2076ms (0.002ms/call)

@dariuszseweryn dariuszseweryn self-requested a review June 29, 2017 12:36
Copy link
Owner

@dariuszseweryn dariuszseweryn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@dariuszseweryn dariuszseweryn added this to the 1.4.0 milestone Jun 29, 2017
@uKL uKL force-pushed the feature/165_access_to_low_level_callbacks branch from 6a5a05d to 12c9b5a Compare June 29, 2017 14:53
@uKL uKL changed the base branch from master to develop June 29, 2017 14:58
@uKL uKL merged commit bad5797 into develop Jun 29, 2017
@uKL uKL deleted the feature/165_access_to_low_level_callbacks branch June 29, 2017 15:23
@6bangs
Copy link

6bangs commented Jun 29, 2017

Cool! Can this be used for a long-running task concurrently with other usages of RxBleConnection (writing and reading characteristics, etc)?

@uKL
Copy link
Collaborator Author

uKL commented Jun 30, 2017

@6bangs no, it's not recommended. When the operation is completed we are releasing the radio lock. Having multiple bluetooth operations not synchronized may cause abnormal behaviors.

Anyway, the native callback is automatically released when the operation is finished. Is there any reason you wanted to use it that way?

@6bangs
Copy link

6bangs commented Jun 30, 2017

Yep. We deliver OTA updates transparently to our users while the device is otherwise in use (reading heart rate, etc). It works great currently, but it takes 4-6 hours to complete.

Maybe we could use this if we opened a second RxBleConnection to the device specifically for OTA. Is there any reason that wouldn't work?

@dariuszseweryn
Copy link
Owner

dariuszseweryn commented Jul 1, 2017

Hello @6bangs
Could you explain in a bit more detail how do you achieve that? You want to have a constant stream of Heart Rate with a fixed interval - do you perform a read operation once every second (or a different amount of time) for that or do you use notifications? The library is implementing a priority queue but it is not available through the API. It would be possible to add for instance a low priority characteristic write to the next API.
Again if you would describe your use-case in greater detail - I will be able to think about how to make it possible.

Edit: And to answer your question - it is only possible to have a single RxBleConnection to a specific device at any given moment. You will get BleAlreadyConnectedException if you will try to open a new connection to the same device when it is already established.

@6bangs
Copy link

6bangs commented Jul 2, 2017

Of course. Our device uses characteristics not defined in the BLE spec for both OTA and heart rate measurement (among other things). We set up notifications for heart rate measurement via the usual setupNotification method of RxBleConnection. Most traffic happens via notifications, but during OTA we do a lot of writing until the OTA process is complete. We also write occasionally to update the state of the device (set the heart rate monitoring interval, etc).

We can perform all of these operations concurrently, and it's working really well. OTA just takes forever due to RxJava overhead.

With regard to BleAlreadyConnectedException, is that a limitation of RxAndroidBle or the platform? I haven't actually tried connecting multiple BluetoothGatts to the same device with the same app, but I assumed it must be possible since multiple apps can connect to the same device at the same time.

@RobLewis
Copy link

RobLewis commented Jul 3, 2017

@6bangs Are you using the ConnectionSharingAdapter to multiplex your various reads and writes with a single RxBleConnection? I recently switched to this from my somewhat lame home-brewed solution and it seems to be working well. More here.

@6bangs
Copy link

6bangs commented Jul 3, 2017

Yep, exactly. Sorry, I forgot that detail; wrote that code almost a year ago. If I recall, I found it in an RxAndroidBle example. I bet it's something that's useful enough it should probably be called out in the README.md.

@dariuszseweryn
Copy link
Owner

@6bangs The BleAlreadyConnectedException is a conscious decision to prevent people from interfering with their own interactions with the connection from different places in the code. This decision was taken because it is impossible to create BLE connections in the same stateless manner as HTTP.

It is possible to achieve what you want but you would need to put the OTA and updating the state of the peripheral in the same RxBleRadioOperationCustom and manage writes all by yourself.

@6bangs
Copy link

6bangs commented Jul 3, 2017

Ah, cool. Will look into that.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants