Comments (7)
Hello there.
Question: Is possible to subscribe to multiple characteristic notifications at once?
Answer: It is possible to subscribe to notifications from multiple characteristics but not in one line.
Explanation: There are almost infinite use cases for the BLE applications and it is impossible to cover them all. I feel that with the current state of the code it is quite easy to implement such a feature but baking it into the library won't benefit many people.
From what I understand you would like to map characteristics values into a POJO model in this scenario either to manually assign a characteristic value to a property or have a model that is very generic (a Map<UUID, byte[]>
perhaps). In the first situation you would need to either set notifications by hand or match the resulting byte[]
by hand. In the second situation you will end up with something like:
final UUID serviceUuid = // your service UUID
final Map<UUID, byte[]> genericModel = new HashMap<>();
final Observable<RxBleConnection> connectionObservable = // your connectionObservable
connectionObservable
.flatMap(connection ->
connection.discoverServices()
.flatMap(services -> services.getService(serviceUuid).map(BluetoothGattService::getCharacteristics)) // get characteristics you're interested in
.flatMap(Observable::from) // deal with every characteristic separately
.flatMap(characteristic -> connection
.setupNotification(characteristic) // setup notification for each
.flatMap(observable -> observable), // to get the raw bytes from notification
Pair::new) // merge characteristic with byte[] to keep track from which characteristic the bytes came
)
.subscribe(
pair -> genericModel.put(pair.first.getUuid(), pair.second),
throwable -> { /* handle errors */}
);
I hope this answers your question.
Best Regards
from rxandroidble.
Thanks! It helps! :)
My main concern is that as I'm learning RxJava I missed that.
from rxandroidble.
Just have in mind that Android allows to setup only a finite number of notifications.
from rxandroidble.
Yeah I believe that are 4 for version below 4.4 and then incremented the value to 7 from 5.0! But thanks for the remainder :)
from rxandroidble.
I have another related question with what you have shared yesterday:
I can subscribe perfectly to the notifications and I can map those to a pair to latter transform the bytes[] values to meaningful data.
The same applies if I want to read those characteristics that allows reading with the following snippet of code:
connectionObservable.flatMap(connection ->
connection.discoverServices()
.flatMap(services -> services.getService(SERVICE_ID).map(BluetoothGattService::getCharacteristics))
.flatMap(Observable::from)
.filter(characteristic -> BleUtils.hasReadProperty(characteristic.getProperties()))
.flatMap(connection::readCharacteristic, Pair::new)
.compose(valueAdapterTransformer)
)
.subscribe(pair -> logger.d(TAG, "Pair: " + pair));
I would like to upon the first connection: read an amount of characteristics that has the reading property and only after that subscribe to those notifications that have the notification property pretty much as you explained me yesterday.
The thing is that when I do the first part the reading observable won't emit a finish a doOnComplete
call as is waiting for more, so I can't kickstart or compose the next operation which is subscribing and listening to changes. I know for sure the amount of characteristic that has the read property but I would like to do it in a generic fashion (i.e it doesn't matter if I have 7 or 15 characteristics that I want read, I only want to read them all, write the pojo values, and after that start listening the notifications).
(Sorry to bother you again with more questions about RxJava than the library itself but I've been searching for examples and I can't find nothing valuable).
Maybe the option is to use an observable that counts the emission of the first one and then after that chain through that to start listening notifications.
How is the best option to achieve that?
BTW I think that this answers would be good to create a Wiki and explain advanced usage of the library (providing more examples to the sample app). I would gladly do it.
Thanks
Edit 1: Updated the title to be more meaningful.
from rxandroidble.
Actually this kind of questions should go to stackoverflow.com with the tag rxandroidble
as these are not questions about the library itself but use-cases.
from rxandroidble.
Yeah, you're right :)
(I'm going to create the question there and I'll update this comment with the link).
Edit:
Question is asked in StackOverflow.
I'll close this 'non issue' issue.
Thanks!
from rxandroidble.
Related Issues (20)
- Does RxAndroidBle support the use of Bluetooth Gatt's connect method? HOT 4
- Building fork help HOT 6
- It is not possible to disconnect the Bluetooth while it is in the process of connecting. HOT 1
- BleGattCharacteristicException while readCharacteristic HOT 12
- scanResult.bleDevice.name not showing Ble updated name HOT 1
- BLUETOOTH_SCAN set usesPermissionFlags="neverForLocation",Is there a problem? HOT 2
- GenericFailure<CharacteristicValueUpdateError> HOT 11
- Send long bytes twice HOT 2
- Write request is not processed in the random order HOT 2
- Long Bytes Send HOT 8
- Creating a Fork HOT 2
- Set Preferred PHY / Read PHY HOT 2
- RxBleClient mulit connections HOT 4
- Is the library still maintained? HOT 3
- Android 14 breaking change HOT 15
- Google Pixel 5 loses bonding info after BT adapter cycle HOT 4
- Metode readcharacteristic not stream HOT 2
- Unable to readCharacteristic after writeCharacteristic HOT 5
- In the case of unsuccessful Bluetooth connection, actively disconnect without executing context. unregisterReceiver (receiver); At this point, there may be multiple Bluetooth broadcast registrarReceivers, which may crash on some Huawei phones due to a broadcast limit of approximately 1000. Suggest fixing this issue. HOT 2
- Can't scan after connect dispose/clear HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rxandroidble.