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

How to use requestConnectionPriority? #367

Open
somq opened this issue Aug 2, 2022 · 4 comments
Open

How to use requestConnectionPriority? #367

somq opened this issue Aug 2, 2022 · 4 comments

Comments

@somq
Copy link

somq commented Aug 2, 2022

I'm trying to use requestConnectionPriority but cannot find a way to call the method on the client.

I read #36 and the attached pull request !125 and tried peripheral.requestConnectionPriority(Priority.High) with no success on v0.18.0.

I might be wrong but I'm guessing the method is missing in the Peripheral interface though it is implemented in the AndroidPeripheral Class.

Is there something I'm missing? Thanks in advance!

@twyatt
Copy link
Member

twyatt commented Aug 2, 2022

Oh. 🤦

I think the CoroutineScope.peripheral extension functions on Android needs to return AndroidPeripheral rather than Peripheral interface. I'll investigate if that is the best approach.

As a workaround, can you (on Android only) cast to AndroidPeripheral until I've had a chance to update the extension functions?

val peripheral = scope.peripheral(..) as AndroidPeripheral

@somq
Copy link
Author

somq commented Aug 2, 2022

Thanks for your answer, your point looks consistent... Unfortunate!
I did cast it and can now access the interface.

(_peripheral as AndroidPeripheral).requestConnectionPriority(Priority.High)

Thanks again!

@twyatt
Copy link
Member

twyatt commented Aug 3, 2022

It appears that changing the return type to AndroidPeripheral isn't a feasible option:

Screen Shot 2022-08-02 at 11 37 12 PM

At least not for the CoroutineScope.peripheral extension function that is available to common (expect / actual) code.
Making only some of the CoroutineScope.peripheral extension functions return AndroidPeripheral makes for an inconsistent API (and can be confusing for newcomers to the library).

A CoroutineScope.androidPeripheral (on Android only) could be introduced, but that feels like it unnecessarily increases the API surface, which I'd prefer to avoid. Unless I can think of a compelling reason(s), it is probably best to expect library consumers to create their own CoroutineScope.androidPeripheral (or similar) extension function (that performs the cast), or perform the cast at the usage site.

Unless a similar "connection priority" functionality can be configured on the other platforms (then a common function could be provided on Peripheral interface) — but I'm not aware of a similar functionality for Apple or JS. 😞

Apologies, I'm not aware of a better solution.

@somq
Copy link
Author

somq commented Aug 4, 2022

Well, the casting does the job at least, no worries.
I did successfully call it and yeah... Seems like it's working better (seems!)
The thing that the method is Android only and does return only an ACK instead of a complete calback is really confusing and tells a lot in itself how hacky it is (thanks Android team😅)

The "right" way in my mind would be to add it to the PeripheralBuilder constructor in an onConnection callback and make it does nothing for js & apple platforms (basically the same way requestMtu works).

Something like:

val peripheral = scope.peripheral(advertisement) {
    onConnection {
        requestConnectionPriority(Priority.High)
    }
}

That said, that's working as-is and I feel like a short note in the docs would be sufficient to avoid the refactor.
Thanks again for your help anyway!

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

2 participants