I’m working on an Android application that runs in the background and enables support for a Bluetooth accessory. I would like to be constantly listening in the background for the Bluetooth device to try and open a socket to the phone. My question is whether it is possible to achieve this without constantly keeping a partial wakelock, since that would obviously have severe battery consequences. So what I’m wondering is what effect Bluetooth has on the phone falling asleep. Does the phone stay awake when there is an open Bluetooth socket? Does the Bluetooth chip wake up Android automatically if a device tries to connect? I’ve attempted to do some testing to answer these questions, but it’s difficult to isolate what is happening with wake locks; in addition, I don’t know if I can rely on the behavior I observe or if it subject to change on other devices.
Yes, if your application is running and a thread is in serverSocket.accept() method, incoming connection wake up phone, so there is no need to use wakelocks. However, make sure that your service is in foreground and is not killed by system.
If you are developing it for devices target to marshmallow based or above, there is DOZE mode to treat such conditions. You then need not to worry about these thing. It can handle the WAKE_LOCK with appropriate mechanism.
The phone does not stay awake if there is an open Bluetooth socket, and neither does the Bluetooth chip wake up Android if a device tries to connect. Usually there is a background thread running to accept connections on the open port and as soon as a device tries to connect, it is this thread which reads the connection, gets some sort of authentication from the incoming device(I am assuming that there is a security protocol in place to accept any new incoming connections) and once the incoming connection is authenticated, an independent thread is created/notified to handle subsequent information exchange with this thread.
So the background process would consume some power and battery drain and it is also responsible for keeping Android partially awake(partially as its a background process and you can always control how frequently it checks for incoming connections). Usually this background process is not run always, its run only when Bluetooth is turned on in Android. So you can also create a thread which should run only when Bluetooth is switched on in Android, else it should sleep.