Android BLE disconnected when screen off (without charger) - android

It's working well on most Android devices.
Other device's screen gets locked after 1 minutes BLE device gets disconnected,if device without charger.
Thanks for any suggestions!

this might come from the Keep Wi-Fi on during sleep option (which might affect all radios) - as it seems this only exists in 8.0 but not in 8.1 (at least on the Pixel 2 XL, according to this article).

You might be having this problem because your app is not properly configured to handle Doze mode.
While there do not seem to be any restrictions on using BLE itself, doze mode might be messing with the control mechanism for your bluetooth connection (ex: a background service).
To see if this is the issue, you can whitelist your app manually via Settings > Battery > Battery Optimization and disabling optimization for your app.
Furthermore, as of Android 8.0, there are restrictions on background services when your app is not in the foreground. So, you may need to create a foreground service to handle the connection.

Related

Android: how to execute service when nearby found some ble device

Right now I'm developing client application for Android that works with physical locks with our controllers. User can open lock via Bluetooth using this application (connection -> secure session creation -> sending some user key).
All works fine except one feature: opening lock when Android device screen is off.
I am using following approach:
Foreground service that periodically scans for nearby BLE devices and when scan is successful (nearby lock device found using BLE filters), application tries to connect and send lock key to lock device.
So, there are two problems:
Android kills foreground service in few minutes after screen off. After setting ignore battery optimization it works ok, however I can't find universal way to navigate user to those settings (because those settings are vendor specific, see https://dontkillmyapp.com/ and android open battery settings programically).
Background limits for BLE scanning are tricky.
I know one application that achieved same thing without foreground service: https://play.google.com/store/apps/details?id=eu.hoermann.ast.bluesecur, this application seems to work good in background without user interaction with battery usage optimization menu.
So, question is:
what is the best way to trigger background process (device screen is turned off) when nearby suitable BLE device available?
Your requirements perfectly describe the scenario of a companion app. Disadvantage: it is only available from Android 12 (API level 31) onwards.
I think you shoul add a wake lock with the service:
https://developer.android.com/training/scheduling/wakelock
https://developer.android.com/training/scheduling/wakelock#cpu

is there any way to continue Bluetooth Low Energy scan,never stop for a android app

I 'm developping a continue scan android app, because I have to collect some important info from another device , I have to scan ble device,and never stop, otherwise, I may miss some important info. Is there any way for a android app to continue Bluetooth Low Energy scan,never stop ,scan for a year if the important device don't appear. During the year, my android phone always keep sufficient electricity,but sometimes I will press home button , sometimes I will make Screen Off (not power off ,just make screen off).
Modern Android has implemented various battery saving measures which will prevent you from continuous data collection.
Doze mode is implemented by the Android framework. See: Scanning for Bluetooth LE devices when the phone is in doze mode, don't work. CallBack method doesn't called when doze mode
Additionally various device manufacturers have implemented their own battery savings systems which will kill your app even when you are following Android framework rules. See: https://dontkillmyapp.com/ for details.

Can a paired/bonded bluetooth device wake an Oreo or higher Android device

I have a security system where moving a Bluetooth LE device causes my android application to do some work. After some of the newer Android updates, I believe API 26, I could no longer get these updates if the Android device's screen was off. I was trying to think of a way around this and came up with an idea:
What if I paired the Android device and the Bluetooth device - would this allow for the message to be received when the Android screen is off?
Previously, all my BLE devices were part of a mesh, so I would regularly have the Android app check for changes in the mesh and turn the screen on to notify the user if needed. Now, because of the background execution limits introduced, I must require the Android device to have the screen on at all times, which leads to the death of display over time.
I'm not that great at programming in C, which is the language the BLE chips are programmed in, so I wanted to ask this first before spending hours just to see this NOT work. So, just to repeat the question: Will having a BLE device paired with the Android device allow a message through these restrictions when the Android screen is off?
If not - is there a way I can make this work?
Thank you for any info!
Use notifications instead of advertisements. This will deliver data instantly and works even if the screen is off. Newer versions of Android have introduced more restrictions for BLE scanning when the screen is off.
Use connectGatt with autoConnect set to true to automatically connect and reconnect in case the connection drops.
Use a Foreground Service in your app process to prevent the app process from being killed. If your app process exits, all Bluetooth connections are dropped as well.
You could use CompanionDeviceManager to gain some extra permissions.
You don't really need to be paired but it has some benefits such as faster connection setup and correctly remembering the address type (flaw in the API that this is missing).

Wake up android app via push notification while in doze without FCM

Greatings!
We are developing an android app with Xamarin that has to meet the following restrictions:
wifi yes, but no internet connection
no FCM/google play services (app should also work in China)
push messages from a Manager-SW (uses rabbitmq) need to arrive quickly after being send (< 1min) AND also when the device is in doze mode
no pulling mechanism
At the moment we are having a wifilock and a wakelock. The battery optimization for this app is turned off. This way messages always arrive even when the device goes to doze mode.. with some exceptions.
There are devices like the HUAWEI P20 that have their own battery save solution (App launch) --> app is not running although we have all the stuff above
Also this solution is not ideal considering battery usage.
I have looked into alternatvies like Pushy, but for me it seems they have problems with doze mode too.
Do you know any alternatives to FCM?
Is there any easy way to go around the Huawei stuff without the user having to change the settings manually?
Many thanks to all of you in advance!

How to disable power saving mode programmatically in android

I'm creating an app that took coordinates every minutes and send to the server using service, but when app is in background and mobile is in idle mode it kill my service but when the phone is not in the power saving mode it doesn't kill my service, because of this scenario i have to disable the power saving mode...
Unfortunately you can not do such thing. And after Marshmallow android introduce Doze behavior for battery optimization which was the biggest halt seems in android for user check this out here.
Please read the above link first before approaching further.
And for more information the power saver mode behavior varies device to device. I myself tested in Samsung Grand and lenovo Vibe devices . Some of Devices will not allow services in background and any network access to the application.
You can find work around for the problem in android documentation in above link

Categories

Resources