Reedyuk / blue-falcon

A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android
https://bluefalcon.dev
Apache License 2.0
321 stars 43 forks source link

iOS / Android diferncies #123

Closed kcn1-71 closed 3 months ago

kcn1-71 commented 3 months ago

Hey guys! Maybe you can help me

I'm using your library in a multi-platform application, all logic is shared

There is a problem that the library does not send requests to the connected device.

On Android, I can scan a device, connect to it, request a characteristic reading, see the read request logs on the gatt server, get the reading result in didCharacteristcValueChanged

On iOS I can scan the device, connect to the device, enter the bonding code, after that I try to request a reading of the characteristics

From the logs I see that client.readCharacteristic(peripheral, сharacteristic) is called, but the call does not reach the client (I have a Gatt server on ESP32) and of course didCharacteristcValueChanged is never called

Tell me, maybe there is some trick here for iOS?

Reedyuk commented 3 months ago

can you confirm. In your common code you are connecting and sending the request to read the characteristic, this is working in android but not in ios?

I would recommend you clone this repo, publish to your maven local, you could then try and add additional logging to locate where things are going wrong.

Here is a question, can you read ANY characteristic? I think because its a characteristic that needs to be 'subscribed' to, (on iOS), you need to perform "notifyAndIndicateCharacteristic". I had a similiar issue where i could read some characteristics but others i couldn't and it was because they need to be set to notify of a change - a behaviour that was slightly different on android.

kcn1-71 commented 3 months ago

can you confirm. In your common code you are connecting and sending the request to read the characteristic, this is working in android but not in ios?

Correct

Here is a question, can you read ANY characteristic? I think because its a characteristic that needs to be 'subscribed' to, (on iOS), you need to perform "notifyAndIndicateCharacteristic". I had a similiar issue where i could read some characteristics but others i couldn't and it was because they need to be set to notify of a change - a behaviour that was slightly different on android.

I tried to read encripted and not encripted characteristic, tried to client.notifyAndIndicateCharacteristic(connected, characteristic, true) before request, but it doesn'work

I would recommend you clone this repo, publish to your maven local, you could then try and add additional logging to locate where things are going wrong.

Can you recomend some specific place frome where is better to start checking?

Reedyuk commented 3 months ago

well i guess the first stop is to put in a println in places like notifyAndIndicateCharacteristic, to ensure you are calling this code. Then i would be adding logging around the https://github.com/Reedyuk/blue-falcon/blob/master/library/src/iosMain/kotlin/dev/bluefalcon/PeripheralDelegate.kt Adding in items for the discovery of services and characteristics, to ensure what you are calling has been discovered by iOS. You should be seeing some of the pre-existing println's appearing in your console.

kcn1-71 commented 3 months ago

I did publishToMavenLocal and got this set of folders Screenshot 2024-05-15 at 17 40 22

I don’t understand which one I need to connect to my project? Or do I need to connect each of them?

Reedyuk commented 3 months ago

You do not need to worry about which to pick, gradle will deal with that. You just need to add

mavenLocal()

in your build gradle for your project, i used it in the android example: https://github.com/Reedyuk/blue-falcon/blob/458659a05ab07133dc7c4ad10d567d2b076c0ab3/examples/Android-Example/build.gradle#L25

kcn1-71 commented 3 months ago

Ok, so I added some logs in iOS side of library

connect disconnect scan stopScanning changeMTU Called as expected

readCharacteristic notifyCharacteristic indicateCharacteristic notifyAndIndicateCharacteristic Never called

Reedyuk commented 3 months ago

even the basic readCharacteristic is not being called? Are you sure you are calling it correctly? I would imagine other users of the library would of raised this issue if this was the case.

kcn1-71 commented 3 months ago

Ok, I have localized the error

In my gatt server, all names of services and characteristics are hardcoded with lines like 620b871f-0002-4013-9c5b-ff5b98eedacb

Callbacks on the Android side return the names of services and characteristics in the form 620b871f-0002-4013-9c5b-ff5b98eedacb

And callbacks on the iOS side return them in the form 620B871F-0002-4013-9C5B-FF5B98EEDACB

The search was carried out by comparing strings, I simply copied all the names from the server to my client, and on Android they matched, but not on iOS

kcn1-71 commented 3 months ago

This may all seem stupid, but it was quite unexpected

Reedyuk commented 3 months ago

oh what a pain, i think i have had a similar issue in the past, i would be tempted to force upper case but wasn't sure it was a good idea.

Reedyuk commented 3 months ago

Can you close the issue if you feel the problem is fixed