Our Android application connects to our nRF52 (BLE) within 2..3 seconds while the APP in foreground. The Advertisement is picked during a periodic scan performed by a dedicated service.
When the APP is in background, although the scan is performed (proved by breakpoint, and an incrementing counter), there are no scan results.
As soon as the APP is bough into foreground, the scan picks the device normally.
Both the Android API StartScan() and the deprecated StartLeScan() have exactly the same behaviour.
What is required to make the scan work in the background ?
Solved it. I used the service object reference previously initalised from within a user thread.
I moved this initialization to function OnStartCommand() (within the service class), after which it worked.
Foreground/background - same performance.
My implementation uses the raw Android native API. Thank you for the contributions.
BLE background scanning is a hit and miss in Android. Then there are different drivers plus different Android version which is a pain to debug and test. Connecting to known devices is more reliable.
Thank you for your comment - it helped me to re-think, and I made it work.
Now, attempting to re-connect using 'device.ConnectGatt()' seems to work well, and it must be much better than scanning periodicaly.
Hi,
I did this today and got a surprising good outcome. This single line of code appears to setup bluetooth to work like it is in foreground mode, scanning works and bluetooth connects every time without fail. Give it a try.
I add the code in my main page before call the background service.
IBluetoothLe _bluetooth = CrossBluetoothLE.Current;
Just tried it. I see it refers to the 'Plugin'. As my application is already optimal, It made no difference.
Happy to have got rid of the scanning though. Thanks.
Answers
hi,
You might want to try the Plugin-BLE, It takes care of most the plumbing for you and it works pretty well. The is a compile version in Nuget.
https://github.com/xabre/xamarin-bluetooth-le
Thanks, this is interesting. However a brief search reveals that this plug-in has the same issue as me, using the raw native.
https://github.com/xabre/xamarin-bluetooth-le/issues/57
Solved it. I used the service object reference previously initalised from within a user thread.
I moved this initialization to function OnStartCommand() (within the service class), after which it worked.
Foreground/background - same performance.
My implementation uses the raw Android native API. Thank you for the contributions.
BLE background scanning is a hit and miss in Android. Then there are different drivers plus different Android version which is a pain to debug and test. Connecting to known devices is more reliable.
Thank you for your comment - it helped me to re-think, and I made it work.
Now, attempting to re-connect using 'device.ConnectGatt()' seems to work well, and it must be much better than scanning periodicaly.
Hi,
I did this today and got a surprising good outcome. This single line of code appears to setup bluetooth to work like it is in foreground mode, scanning works and bluetooth connects every time without fail. Give it a try.
I add the code in my main page before call the background service.
IBluetoothLe _bluetooth = CrossBluetoothLE.Current;
Just tried it. I see it refers to the 'Plugin'. As my application is already optimal, It made no difference.
Happy to have got rid of the scanning though. Thanks.
Hi. Could you please show your implementation? Got the same problem with scan in background.