Closed hfsamb closed 1 year ago
Hi @hfsamb, thanks for the good description and sample code. I may have an idea what is the problem and it is most probably related to the Android Bluetooth permissions.
Android 12 onwards there are dedicated permissions for Bluetooth. Android 11 and below requires the location permission to be approved by the app user to make BLE scanning to work.
I believe the permissions are correctly asked in your application, but in addition there is a small detail related to Android service. As the Android 11 and below needs the location permission for Bluetooth scanning, also the service needs to be declare with android:foregroundServiceType="location"
By using your code snippet I did small example project https://github.com/JOikarinen/PolarBleSdkWithAndroidService which shall work as expected. Please have a try.
Hi @JOikarinen , thank you for the answer, it solved the problem indeed.
Should I add the ACCESS_BACKGROUND_LOCATION permission to the app? It seems to be required according to docs but it worked without it in my tests.
Should I add the connectedDevice foreground service type, as in android:foregroundServiceType="connectedDevice|location"
? Is it required for long timed connection to Polar?
Hi @hfsamb,
Should I add the ACCESS_BACKGROUND_LOCATION permission to the app? It seems to be required according to docs but it worked without it in my tests.
the documentation says this:
When your app starts a foreground service while the app is running in the background, the foreground service has the following limitations:
Unless the user has granted the ACCESS_BACKGROUND_LOCATION permission to your app, the foreground service cannot access location.
ACCESS_BACKGROUND_LOCATION is needed if the foreground service is launched while application is background. Maybe not the most typical use case, but depends on your app. At least in the example app the ACCESS_BACKGROUND_LOCATION is not needed as the foreground service is started by the button and app is definitely foreground at that moment.
Should I add the connectedDevice foreground service type, as in
android:foregroundServiceType="connectedDevice|location"
? Is it required for long timed connection to Polar?
- I must admit, I couldn't really found out the exact description for "connectedDevice". I am not sure is it needed. The documentation don't tell me exactly when it is needed:
Auto, bluetooth, TV or other devices connection, monitoring and interaction.
- the key word "bluetooth" is mentioned in documentation, so maybe it shall be added as argument too. In my own testing I have only used
android:foregroundServiceType="location"
without problems.
Platform your question concerns:
Device:
Description: I made a foreground service that receives heartbeat values from polar and I noticed that the reconnect feature is not working if the app does not have an activity on screen.
In a small test app shown below there is an activity that will get polar id from user and a button that will start foreground service. In the service I connect to device using polar ble api and can get periodic HB callbacks but if connection is lost then it is not reconnected automatically.
I noticed that if I keep the activity that started the service on display then I can reconnect as expected. If activity is paused or finished then the service cannot reconnect.
Is there a limitation on reconnect feature if sdk connection was made in service?
In the tests attached logs, device lost connection due to power down and reconnection was performed with a power up after a few seconds.
fail.txt pass.txt