kable icon indicating copy to clipboard operation
kable copied to clipboard

How to use requestConnectionPriority?

Open somq opened this issue 3 years ago • 5 comments

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!

somq avatar Aug 02 '22 13:08 somq

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

twyatt avatar Aug 02 '22 17:08 twyatt

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!

somq avatar Aug 02 '22 21:08 somq

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.

twyatt avatar Aug 03 '22 06:08 twyatt

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!

somq avatar Aug 04 '22 14:08 somq