Android USB Host and Device Sleep - android

I have a USB device that I am interfacing with an android app using USB host mode. The device sends some data approximately once every 10 seconds but in some cases (high priority data) the data can come anytime. On the android app side I have a service that reads the data using USB host mode API. My android app processes the data and writes data back to the usb device. Now all works fine till the android device goes to sleep. The service goes to pause and all communication stops. I understand that I can use partial wake lock to keep the CPU running and the communication going and that it will have a huge impact on the battery. I though have two questions:
Since the USB device can send data anytime, I really need the android service to alive all the time especially since higher priority data can come anytime. This could potentially mean that I acquire the partial wake lock and never release it. Havent been able to find in documentation if this is allowed and if yes what are the ramifications besides draining the battery?
Does the USB Host mode API or any other API provide a different and potentially better solution?
The app is a custom app with limited users and so battery drain is not a big issue. I just want to understand if there are other issues I need to be careful about or if there is a much better way to do it.

Related

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

Android : Battery drains around 4 hours common with wifi ON and screen ON always

My app is single activity app which is used to generate token slip using WiFi printer over wifi which are connected locally. So the WiFi is always ON. Also the screen is always ON.
I have set android:keepScreenOn="true" in my activity xml file for this.
The tablet is exclusively used for this single app only not more than that. Even though, the battery drains around 4 hours.
Is this common? Or Would I change anything to achieve good battery backup?
Generally if your device is old, There is a chance to expect this kind of situation.
To debug about your network traffic, Follow this link
The network traffic generated by an app can have a significant impact
on the battery life of the device where it is running. In order to
optimize that traffic, you need to both measure it and identify its
source. Network requests can come directly from a user action,
requests from your own app code, or from a server communicating with
your app.
Link is here

How to receive call on android + pjsip when phone in deep sleep

I'm trying to build voip app based on PJSIP on Android. I decided to use TCP for connection to make the connection be persistent. And app right now has background service (same process) which is responsible for all interactions with SIP server. TCP keep alive timeout is set to 1 minute (just for testing purposes). Everything works fine until the moment when phone goes into a deep sleep mode. From logs I see that phone still sends TCP KA but with 4 minutes interval (why???). But the main issue is if I try to call from another phone to this one the phone keeps sleeping and skips all INVITE messages. Appreciate any help.
When the Android device goes into deep sleep, the CPU throttles down and almost all OS services are suspended. See for example:
Android Sleep/Standby Mode
what is the difference between Sleep mode and Deep sleep mode?
If this were not the case, the OS and most other components would still be on, which would drain battery extremely fast, and most users would uninstall your app.
In order to receive incoming messages (such as the INVITE), it's recommended to use GCM. In this, you need to implement a server piece that is essentially handling the client's responsibilities while the client device is asleep.

Android opportunistic communication vs power management (what happens to service when phone goes to sleep)

I am writing an application for android that will use opportunistic communication. So when two devices are nearby they exchange data. For now it will happen when they are in the same wifi network (using mdns) or when they have paired bluetooth interfaces.
I was wondering from power manager point of view. Will my ongoing service be paused when phone enters sleep mode? If yes how do I prevent it?
Also any general tips how to make it consume as little power as possible? I was thinking to wake it up from time to time, but then there's a chance that it will miss a mdns query sent by other device which I want to avoid to maximize throughput.
And just to clarify - yes, I do want to use opportunistic communication and no I do not want to use any solution that involves connecting to the internet.
Any help appreciated.
Use Wakelock to indicate that your application needs to have the device stay on.
but it consumes considerable amount of power.
you can also use AlarmManager to start your app at that particular time when wifi is available

Categories

Resources