Some devices don't receive UDP broadcasts when screen off - android

I can't get a Note 2 running 4.1.2 or Nexus One running 2.2 to stay 'awake enough' with the screen off to keep acting on UDP broadcast packets. I have tried various wakelocks and permissions (see below), it works fine on a Desire HD on 2.3, a Huawei U8110 (T-Mobile pulse mini) on 2.1 and a Samsung Galaxy Mini on 2.2.1.
It does receive and act on new TCP connections directly to its IP/port, but not broadcast UDP packets. If I tap the power button to wake the screen it immediately responds to broadcasts.
Forcing the the screen to stay on (PowerManager.SCREEN_DIM_WAKE_LOCK or higher) does keep it listening but that's not practical.
I'm acquiring wakelocks:
powerManager.newWakeLock(PARTIAL_WAKE_LOCK)
wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL)
wifiManager.createMulticastLock()
And declaring permissions:
android.permission.INTERNET
android.permission.WAKE_LOCK
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
System Wifi sleep policy is 'never'.
The working devices all timeout their screens but still respond to the UDP broadcasts.
In the debugger's Threads view I can see it is still blocking at
DatagramSocket.receive()
whereas on the working devices that returns with a packet which is then logged to logcat.
All devices are running the same .apk.

An update for anyone else with the same problem:
Google seem to think this should work fine:
http://code.google.com/p/android/issues/detail?id=34278
The Nexus One is old so I guess nobody cares.
The Note 2 however is not so I asked in Samsung's developer forum. They don't care either:
http://developer.samsung.com/forum/board/thread/view.do?boardName=General&messageId=247968#postId247968
My current workaround is to provide an option in user-preferences to grab a full wakelock instead of the partial. I added code to cover my UI with a black screen which disappears on a tap; although the backlight is still on this is less annoying and stops potential screen burn. Turning the other lights (capacative buttons) off should be doable but IMHO it's 'good' to let the user know their phone is actually still fully awake.
It stops the device's normal lockscreen timeout. A manual power button press will lock but turns the screen off and so stops UDP receiving, on wake the lockscreen stays on forever until the user unlocks.
So this provides a poor user experience but it's the best I've found.
Any suggestions welcome.

Related

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).

Bluetooth Low Energy scan result delivery is reduced when the screen is OFF

I am developing an android application that uses Bluetooth Low Energy to receive data from a peripheral device.
The device itself sends the data as part of it's advertising message and it could send the message at any time.
So the applications works by continuously scanning for BLE devices.
When the screen is ON the application receives scan results as intended.
When the screen is OFF it appears that it only receives scan results every 5 seconds.
I guess that the phone is trying to optimize for battery life but this app is mission-critical so we can not afford to lose messages.
The scanning is done by a Service which is set as a 'foreground service' so that the OS knows that it is important.
The service also acquires a PowerManager.PARTIAL_WAKE_LOCK but that does not seem to help.
The application is excluded from battery optimizations so that it does not enter Doze or deep sleep mode.
The application is using the following ScanSettings:
ScanSettings.SCAN_MODE_LOW_LATENCY
ScanSettings.CALLBACK_TYPE_ALL_MATCHES
ScanSettings.MATCH_MODE_AGGRESSIVE
ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT
I am working with a Galaxy S7 but I have been told the same behaviour happens with a Galaxy S7 Edge.
I have tested with other non-samsung devices and they work as intended (continuously receiving scan results when the screen is OFF).
The question: is there something that I, as a developer, can do to make the BT adapter deliver scan results continuously even when the screen is OFF?
Samsung automatically change scan mode to low_power if screen is turned off. It's their customisation on their Android OS, so unfortunately no way to change it.

Android BLE disconnected when screen off (without charger)

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.

AlarmManager does not wake tablet device from sleep

I cannot get some Android devices to wake up using the AlarmManager. Following the advice in other StackOverflow posts, I am testing CommonsWares cwac-wakeful demo unmodified, compiled in Eclipse, and sent directly to my tablet devices.
When the screen is on the app wakes every 15 minutes and dutifully writes a log message, if I switch the screen off (short press on the power button) and remove the USB cable, then reconnect say 30 minutes later, there are no log messages for the period during which the device was asleep. The alarm does trigger immediately after I wake the device up (according to the log message timestamp). If I leave the USB cable connected the alarm does trigger even with the screen off, presumably because the device is in "debug" mode.
I have seen this behaviour on an older Android 2.2 tablet and I think it might be a bug in the hardware or OS common to both devices. However, I would like to rule out a bug in the cwac-wakeful utility and "operator error" if possible.
Has anyone else seen this behaviour and have any suggestions about further diagnostics or remedies?
I actually get the same behaviour when I set the built-in clock alarm from the home page, i.e. the A90 and the VEGAn-TAB don't wake up until the next manual power on. Therefore it must be a fundamental problem with the tablets rather than a bug in AlarmManager or WakefulIntentService.

Auto Power on Android when the charger is Connected

I'm Developing an Android Application where it's constantly required to keep the App up an running at all time. with the phone connected to a power outlet at all time, Is it possible that if the phone's battery died to Auto-start up the app with no user intervention . *i.e to power up the phone automatically without hitting any power button and start up android since charger is connected. *
then start up my app with android.intent.action.BOOT_COMPLETED and RECEIVE_BOOT_COMPLETED permission
PS: I have tried android.intent.action.ACTION_POWER_CONNECTED but it only works if the phone is already powered up.
I have exactly same requirement as yours.
Can we plan a solution like this:
The Android device is always connected to charger's output.
Charger's power supply is connected to a custom made hardware device which controls power supply to charger.
This hardware device puts charger off until battery is getting low. i.e. 20% remaining.
Once battery is less than 20%, that device automatically switch on the charger.
The "custom made hardware" gets it input from android device itself via USB/WiFi/Bluetooth.
Just a thought. May not be practically possible.
No way doing it if you do not root your device and add reboot scripts to the programs running to show battery charging pictures when device is turned off and charging.for Samsung s2 here is an answer
Maybe you can make an additional app that waits for the BOOT_COMPLETED with its BroadcastReceiver and then check wether the power is connected and start as a new Intent your application?
Just a quick thought..
I have the same problem.
The solution that i will to implement:
1.- get out two(here is my doubt, ¿what two wires?, because i see three, i will use my multimeter for see continuity between wires when i push the button) wires from the power button phone.(when you push that button, it just connect two poles like a switch).
2.- When you have the two wires, so, we build a little circuit with a timer like a 555(integrated circuit). The purpose of the circuit will connect the two wires(from power button phone) by a 5 seconds period every 15minutes.
Obviously this circuit will be running always.

Categories

Resources