-
Notifications
You must be signed in to change notification settings - Fork 609
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
Android vs iOS Performance / Connectivity / Issues / Questions #963
Comments
Update: When I disable the code again, and don't restart my phone, the auto connect is working again aswell (still with the mentioned delay above) |
Hi @graphefruit Quite a lot of things going on there! So... firstly, I can say confidently the plugin will give you all the bits you need to achieve this... but there'll be a bit of work to get there 🙂
Is this because you don't know the device id (guid)? Or because the guid doesn't successfully connect if you just call connect directly? The scanning should not be needed if the phone has previously seen the device, and the mac address is unchanged (or the phone has bonded with the device). One thing to note is that you can safely scan while connecting to a Bluetooth device. In a similar app I've worked on, I basically run the scan after app launch until I've found all devices, but I immediately connect and process each device as it's found. It's pretty power efficient overall, and no major down sides.
This is a known effect of using
Does the mac address of the device remain constant? Some devices use a private randomised mac address for privacy/security reasons. Does the device id remain constant between scans too, or does it change?
Are you scanning between disconnect or connect? Sometimes Android itself will hold the connection open without telling the app. This is especially common when the device is bonded. If you call To help debugging, I'd highly recommend getting nrf Connect on a second Android phone so you can confirm the device you're trying to connect to is actually broadcasting. I've experienced issues in the past where a BLE device does not properly resume broadcasting... and I've also had issues where other phones in my room were fighting to connect to the device and I had forgotten about it 😅 .
|
Hello @peitschie , thank you for this enormous & fast feedback! Let me try to get into detail:
The
Thats awesome to know! I gonna rewrite my code to this changes, because it takes me some awaits less, and less ui-blockings in the end.
Thats strange because Even when I don't connect the bluetooth scale and just want to connect the pressure device its taking this time. Also the question: does the In my mind it would look like this:
If this would work that easy, I think I'll go test with the
The device id remains (gladly) the same, thats why I can store it in the indexDB/localstorage.
I just implemented yesterday, when disconnect, I search for the device again, before triggering the connect - just to make sure. I don't bond the devices, after these devices would not offer a popup with a code e.g.
Thanks for the tip! Gonna test it out after I've changed all my codes like mentioned above
Oh thank you! Thats very good to know, I just did it for testing purpose to diminish everything, but its better to remove it so again! Thanks again for your help in this case |
Interesting. On Android it's a bit inconsistent between manufacturers and versions (some manufacturers require you to scan before you can connect). For iOS, have you looked at turning on the Having said that, for my own uses, I pretty much always scan then connect when working with unbonded devices 🙂
Basically, there's a lot of variables that can be tuned by the peripheral, including things like advertising intervals, max/min connection intervals, etc. Because it's an unbonded device, BLE service discovery must be performed on every connect, which is a fairly expensive operation to perform. Large variations between peripheral classes is fairly normal (though worth talking to the peripheral manufacturer about if you notice a discrepancy between iOS and Android).
This is manufacturer-dependent on Android, and infinite on iOS.
Worth trying this! You'll also need to subscribe to adapter state changes, because disabling the Bluetooth adapter may not always fire the
Worth being aware that if Android has the connection still open behind your back, you won't see the device in a scan. I usually call |
Hey @peitschie, I've basically fixed many things with your input here. One issue I've encountered and I could reproduce now over again is: When I restart my Android-Phone, and i call Again, thats just with the pressure device, which runs with a simple 2032 battery, not any aa/aaa onces. I've tried it with the original app, there the connection is established without any problems, and nearly instant. I've tried a workaround to go with the first iteration, set a timeout for 10 seconds, and after 10 seconds I call disconnect, and call the connect function again -> with this its working. I've tried to go to like 1 second -> this is not working. This is not a great solution, but atleast something that is working. Maybe you got some more insights here Thanks Edit: Also I've tried NRFC-Tool while rebooting the phone: the device is found wihle rebooting, so the disconnect is real, and the device is advertising normaly again. |
@graphefruit are you able to try v1.7.0-alpha.1 and see if that helps anything?
I've added a hardcoded 1s delay after connect before running service discovery just to see if that makes any impacts. It would be good to capture some adb logs showing both the bad initial connection, and the follow-up good connection. Something like |
Hey @peitschie, To be honest it looks like a big mess for me as a non familiar |
Yep... Usually looks the same to me too 😆 I'm interested about the log snippet from the start of the connect call through to the disconnect reporting. As a quick side question, what error object do you get when the disconnect is called on that first connect attempt? |
Do you have the source code for this app? I wonder what Bluetooth library they're using? |
{"name":"PRS31074","id":"EC:D4:69:87:79:62","errorMessage":"Peripheral Disconnected"}
No but I'm in contact with the manufacturer, I'll ask him.
I'll crawl the logs this evening and post them here! Btw: If you want to have a look at my sourcecode here we go: Where all the magic starts: https://github.com/graphefruit/Beanconqueror/blob/2ddcec39f7d551ee0ca2d9beccded6dabc4b09e8/src/app/app.component.ts#L609 And here where to connection logic than grabs to the pressure sensor: Thanks again for your efforts & helping hands |
Hey @peitschie , here we go:
https://github.com/pauldemarco/flutter_blue Also I did the logs, and copy & pasted the necessary things - I hope those helps. The connecting id is
Thanks! |
This line here gives us a gatt error to start from:
I noticed that you are stopping the scan at the same time as trying to connect. I've seen this cause problems on some phones. It might be worth putting a 5-10 second delay there before calling stop scan (still call connect immediately) and see if this helps the stability at all? |
Hey @peitschie, Also worth mentioning, while the first |
For the flutter-based app @graphefruit , do you know what setting is being used on https://pub.dev/documentation/flutter_blue/latest/flutter_blue/BluetoothDevice/connect.html It looks like the default value in the Flutter library is true, while the |
What you're describing looks something like #766 Other users have reported that |
Their app does not use autoConnect. |
Just to double confirm, their app is explicitly setting |
The flag is set to false.
With .autoConnect I cant (even) more connect to the pressure sensor, this may be because I enabled pairing and had a bit trouble with the device, but anyhow, even calling .disconnect after a timeout and then going with .autoConnect is not working. The only real solution I got at the moment is to call |
@graphefruit out of interest, do you have anything else connected/paired with the phone? Does this connect problem happen across all Android devices, or just a specific Android version/manufacturer? Just wondering as another dev has reported that Android disconnects their peripheral when a smartband device is paired with the phone: #956 (comment) |
@graphefruit another random idea... I noticed that FlutterBlue does |
Hey @peitschie ,
I had this issue on Google Pixel 2 and Google Pixel 4a 5g, more Android devices I don't realy have to test at home.
I had a call with the manufacturer the last days. And voila, it connects (nearly) instantly after the search after a phone reboot, after that it also connects instantly. BUT Anyhow, I'm getting a new firmware of the manufacturer the next couple of days, because this one will disable a pairing possibility with my android device - maybe some issues also results from this. Anyhow my idea - I don't know if this is possible: Hope these information helps |
Hey @peitschie The app waits for the device getting connected state, and then calls discoverServices. |
@graphefruit that's a lot of really great insight there. Thanks for taking the time to detail all this out here! |
Right. This matches what this plugin does as well, so at least we're all aligned there. |
Hello @peitschie, Before we start: Its just with one specific scale, and not with all. Removing the scan before connecting to the scale and then Funfact: Meanwhile I've got another firmware from the manufacturer, and many things could be removed. Hope all these inside help Thanks! |
This is very much making me appreciate how complex Android's BLE stack really is. Basically, we have the following systems in play:
I wonder if starting a scan explicitly and then auto-connecting confuses some stacks 🤔 So based on what you're saying here though, the two best approaches are:
I've taken some notes and might turn this into a support doc here someday. Thanks as ever for continuing to report your findings back. They're most insightful! |
This comment was potentially against the wrong issue @graphefruit ? Should be against #975? |
Hey BLE-Contributors,
thank you all for the hard work you did with this plugin! Its awesome that this is so well maintained and working pretty great!
After I've stepped up on my app to not just connect one single ble, rather multiple once, I've encountered some issues.
Some context to this: I'm developing a coffee app, where you can connect bluetooth scales, bluetooth pressure gauges, aswell as temperature devices and soon also a bluetooth refractometer and coffee particel analyser.
All these devices are connected inside the settings, and the device-id is stored in a database, so when the app starts up again, the app connects to all known devices.
What I've encountered:
The issue with this is, that the users start up the app, and after that maybe just the bluetooth scale, so basically I wait for about 60 seconds to find all devices - but thats not a great and proper solution.
-> Would there maybe be the possibility to add the search solution to ble.autoConnect for iOS, that it searches internal the device, before it connects?
Whats really strange is that on iOS the connection is about 0-1 second - so much much faster.
2.1. Also I've encountered when I stored the device id, and I autoConnect to the pressure device it sometimes never connects, and I need to disconnect (forget the id), restart the app and start searching again.
2.2 Also I've encountered, when I search the device, connect to it on Android, disconnect and connect again, the device sometimes is not found anymore. (Funny that on iOS this works like without no issues)
On Android I also scanWithOptions
I've made some progress with it, but still not fluent & good, and far away from the iOS performance.
Also I've disconnected normal connected Bluetooth-Devices like a watch or bluetooth airpods, which also did some improvement in connectivity.
I'm using the latest release 1.6.3, and tried with Pixel 2XL Android 11, and Pixel 4a 5g Android 13.
Same issues on both devices.
The bluetooth scales, are connecting directly, so its real just the pressure sensor which is doing strange.
BUT with the original app of the manufacturer, which is written in flutter, it has no issues at all :<.
The connected smart-scale devices sends about one request each 100ms, the connected pressure sensor, also would send between 20-100ms one request. (so faster then the smart scale)
My setup is Ionic 6, with Cordova,
Platform:
I'm also using ble.withPromises... solution.
Thanks for your help, hope. the information are enough, else I'm happy to provide more information!
Lars
The text was updated successfully, but these errors were encountered: