Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add convenient extensions for for writing and reading/observing bluetooth characteristic #756

Open
dasdranagon opened this issue Jan 4, 2024 · 1 comment
Assignees

Comments

@dasdranagon
Copy link
Contributor

dasdranagon commented Jan 4, 2024

Is your feature request related to a problem? Please describe.
I'm always frustrated when need to make additional data transformations if I want to simply read/write/observe bluetooth characteristics.

Describe the solution you'd like
I would like to have an extension of device flow which allows to access flow of bytes arrays or discovered characteristic for writing/reading.

For example:

operator fun Flow<List<Service>>.get(uuidString: String) = this[uuidFrom(uuidString)] // Accss service with (string) id (so it can be used cross-platform)
operator fun Flow<List<Characteristic>>.get(uuidString: String) = this[uuidFrom(uuidString)] // Access characteristic with (string) id

fun Flow<Device?>.dataFlow(serviceUUID: String, characteristicUUID: String) = characteristicsFlow(serviceUUID, characteristicUUID).flatMapConcat {
    it.enableNotification()
    it
}.map { it ?: bytesOf() }

fun Flow<Device?>.characteristicsFlow(serviceUUID: String, characteristicUUID: String) = services()[serviceUUID]
    .characteristics()[characteristicUUID]
    .filterNotNull()

Usage:

deviceFlow.characteristicsFlow(SERVICE_UUID, RX_CHARACTERISTIC_UUID)
val data = deviceFlow.dataFlow(SERVICE_UUID, TX_CHARACTERISTIC_UUID).take(10).toList()
@dasdranagon
Copy link
Contributor Author

@splendo splendo deleted a comment from dasdranagon Jan 5, 2024
dasdranagon added a commit that referenced this issue Oct 16, 2024
…istics-access-helpers

Feature/#756 bluetooth characteristics access helpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants