22

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.

shtolik
  • 1,341
  • 22
  • 29
Christopher Souvey
  • 2,890
  • 21
  • 21
  • Did you find out the answer or any additional information in the meantime? If so, please post them, because I am currently having exactly the same problem. – Jan Schejbal Jan 28 '13 at 02:16
  • the bt connect should use more battery than a non-sleeping device, so hold a wakelock because you WILL drain the battery (however not as bad as wifi) – NikkyD Jun 30 '14 at 15:06
  • is this BLE 4.x, or regular bluetooth ? – zrgiu Apr 07 '17 at 21:41

3 Answers3

2

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.

Valentin
  • 646
  • 7
  • 10
  • 1
    The same is true of BLE. if you call connectGatt() with autoconnect=true, when the peripheral accepts the connection then the phone will wake. No wakelock required. I have not bothered to check whether this behaviour prevents the CPU from going into deep sleep – Mark Ch Jun 29 '17 at 22:22
  • 1
    Just to be a clear, a background service will also work fine, especially if it's made STICKY. The device will still wake. The reason to use foreground would be to give best possible chance of not being killed due to low resources – Mark Ch Jun 30 '17 at 21:44
0

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.

Suresh Sharma
  • 1,826
  • 22
  • 41
-3

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.

Stack Programmer
  • 3,386
  • 1
  • 20
  • 12
  • 1
    I just tested it, and it seems that on a Galaxy Nexus (as the "server"), an incoming Bluetooth connection does wake the device up. (Same goes for sending an ICMP Ping to the Wifi IP). – Jan Schejbal Feb 01 '13 at 09:54
  • On a Nexus 4 it seem that way, too, but I cannot be 100% sure because the device loves to wake up for no apparent reason, too. At least it always woke up when needed. With an Android 2.3.3 device (Galaxy Ace), it triggers a visible pairing request on both sides, making it useless (and thus pointless to test wakeup). – Jan Schejbal Feb 02 '13 at 06:21