I have develop one android application which will talk to server 24*7 over WiFi but when phone goes to sleep mode it stops talking to server means socket is getting closed so for resolving this I added code to acquire partial wake lock in service onCreate() and release it on OnDestroy() method o service so even though phone goes to sleep mode my application can talk to the server.
Problem is: If you keep the device idle for longer period (more than 8 hours ), it stops communicating with the server and WiFi turned off. I heard about deep sleep mode of device, in this case it will shutdown CPU,WiFi etc.. so how to restrict it for getting CPU and Wifi turned off?
Please help me with some sample example.
Regards,
Piks
I assume you have tried full wake lock as well?
Related
I bought myself a smartwatch that has Android KitKat running.
I have connected it to my HTC m9 smartphone using Bluetooth.
I have created an application that is running on both smartwatch(client mode) and smartphone(server mode).
The applications are talking each other over a Bluetooth Socket connection.
When I get a phone call on my phone, I am sending a Bluetooth notification over the Bluetooth socket to the watch and make it vibrate.
All is good as long as the watch's CPU is up and running.
When the watch goes to sleep, and I get a phone call on the smartphone, the notification is not processed by the watch because watch's CPU is sleeping.
Using a partial lock on the watch does the thing but it consumes the battery to fast.
I don't need a partial lock on the phone, because when the phone receives a call, the phone is "magically" awaken, so my application's service is able to send the notification.
I also can't use AlarmManager on the watch, because I need the watch to wake up when I get a bluetooth socket notification, not on regular intervals.
A WakefullRecevier is also not solving the problem because my watch needs to wake up first so that the WakefullReceiver puts the power lock (or have I got this wrong ?!) and runs its code.
So the question is: How does the phone wake up on incoming call? Or on SMS received? It is a hardware thing..an interrupt?
Is there any way of waking up the watch when a bluetooth message is received?
Thank you
For the past few days I've been playing around with the watch making different tests.
I am not answering the original question but since nobody else answered the question I will just write down my own conclusions which are good enough for my application and maybe help others with similar problems.
1. When the paired watch is in Bluetooth range of the phone, a power lock is not needed on the watch.
Any messages sent by the phone are received by the watch.
If the watch stays idle for some time, the 1st Bluetooth message sent by the phone is received with about 1 second delay. Probably Android watch goes into some kind of low power mode but is able run my code when Bluetooth message is received. Of course Bluetooth messages are received almost immediately by the watch while the watch is not idle.
2. Best way to find out if a Bluetooth socket is still connected, is to write into it.
Socket read is a blocking call but it does not throw IOException when watch goes out of Bluetooth range. I wasn't able to find a read timeout property I can change.
So I am just writing a byte into the socket every 60 seconds to detect if socket is not connected anymore. Battery impact is minimum while socket down detection timing is acceptable.
3. When the paired watch is NOT in Bluetooth range of the phone anymore, Bluetooth socket re-connect strategy is required.
Using a partial lock in this case does not seem like a good approach since it keeps the CPU awake. Also trying to re-connect to often to the phone kills the battery.
The re-connect strategy is based on application needs.
I ended up using WakefulBroadcastReceiver and AlarmManager to make a socket connect attempt with a given period.
4. Every now and then, when the watch goes out of Bluetooth range, then comes back into range, then again and again the Bluetooth socket seems to go in some kind of 'zombie' state. Sometimes the socket.connect call just blocks without succeeding or throwing error, some other times the socket.connectcall does not throw error given the impression that connection has been established, but a subsequent write always throws IOException.
Whenever I encounter these kinds of situations I just stop/start Bluetooth adapter which seems to solve the problem.
5. Pay attention to stop/start the Bluetooth adapter. During Bluetooth restart attempt Android can put the CPU to sleep. I wasn't expecting this behavior but it really happens. So if you want to do this as fast as possible without any delay, make sure the application acquires a PARTIAL_WAKE_LOCK during the re-start attempt. Of course don't forget to release the lock as soon as possible.
I am currently working on an application which requires a service running in the background to play music when the phone is in sleep mode.
I would like to know what are the actions that can be performed to trigger play music action while the device is in sleep mode.
Basically when your phone is in sleep mode it shuts down the CPU, when it is in sleep mode only thing which works is GSM or CDMA radio that is your incoming calls, SMSs, IP packets and AlarmManager...
After this answer you would think that, It even stop the services also if the phone is in sleep mode.
Then my Answer is, Yes. it does, since the CPU is powered down, all processes freeze in place. For more information you can visit this link.
Thanks to #CommonsWare for great answer.
I wrote an application with one activity and remote service. The main goal of this service is keep net connection with remote TCP server. I use in activity PARTIAL_WAKE_LOCK to prevent system going in sleep mode. But after about twenty minutes when screen is off, the connection stops. It looks it starts unfortunately sleep mode and cut connection. When I turn on my device connection is starting again (my codÄ™ detects error and try to set connection again).
Is a better way to keep connection whole time? I do not have to worry about battery - the device is plug in charge all time. The priority is keep connection.
Could somebody give advice how to resolve my problem?
Regards Artik
There are 3 states an Android phone can be in: Awake, Asleep and Off. See: Android: Sleep stages/levels on an Android device?
In sleep mode screen goes off, CPU shuts down along with Wi-Fi and GPS radios. See: http://developer.android.com/reference/android/os/PowerManager.html and Android Sleep/Standby Mode
But GSM or CDMA radio still works and the device can receive incoming calls, SMSes, and IP packets. Also, Google introduced new Google Cloud Messaging service that sends data to devices and can, as I understood, wake the device if necessary. See: http://developer.android.com/guide/google/gcm/index.html
So, my question is: can application on Android device be updated via Google Play if the device is in sleep mode? I mean is Google Play allowed to receive some kind of messages from the cloud, wake the device and update applications? I assume that the application is allowed to be updated automatically and can be updated through mobile network (3G/4G).
Thank you.
UPDATE: I did some research and it happens that Nippey's answer and comments are correct. I did not see any updates to wake my device but as soon as the device is awake updating mechanism starts to work normally. So, theoretically, Google Play Store can start updating your applications right after the device wake up.
The device is able to process push-messages while in sleep mode.
As soon as you trigger an update via the play.google.com, this will issue a push request, which, as a consequence will wake up your device. The update will be installed immediately.
So, it doesn't work in sleep mode, but if the device gets a trigger to wake up, it will do what has to be done.
I have implemented one application which talks over TCP/IP to the server via Wi-Fi, it has to talk to server even device goes to sleep mode so for that I implemented Partial wake lock so even though device goes to sleep mode CPU will be turned on so Wi-Fi also be turned on.
Problem: If I keep the device for longer duration around 7-8 hrs idle with my application running , it seems like device goes to deep sleep mode and when i checked the logcat i found the message : "Wifi Watchdog Service (android.server.ServerThread) for wifi does not require the watchdog".
Not able to undertsand why this message display for the router?
Can anyone put some good inputs regarding this?
Regards,
Piks