3

I want to track connection with BLE device even when phone goes sleep - there's a callback to track connect/disconnect BluetoothGattCallback.

So going along the tutorial from above there's a Service

public class BluetoothLeService extends Service {

    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            //...
        }

        //...
    }

    //...
}

To connect I call

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

As you can see I do not acquire the wake lock anywhere.

  1. So what happens with the callback if device goes sleep?
  2. Will it be called? Or I need to explicitly acquire the wake lock.
  3. Or maybe the BluetoothDevice takes care about waking the device up when callback will be called? (however I see that it's not documented anywhere)
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103

2 Answers2

1

as long as your Service is alive - so will be the callback. keep in mind that the service may shout down by automatically by the system, specially if your app is not currently in background, or it's under low memory pressure.

I guess you don't need to worry about wake lock for this one, since the system will "wake up itself" for you. I would say that there will be better chance to your Service not to be stopped by the system if you'll have a wake lock, but it will cost you on precious battery life, and extra permissions

keeping that in mind, plus knowing that the "worse" think can happen is that your service will be stopped, bringing to conclusion that instead of solving this problem with wake lock, can be solved in much simpler way: perform the gatt connection when the service will re-start! (you should start it as sticky for this) . that way you'll conserve battery life, and won't keep in force the CPU active.

I'm doing myself from my app constant background periodic BLE scans, and connecting to GATT services from an android Service class, and I did not faced any problems from the kind you are worry from.

so I would suggest you not to worry about that until you faced a concrete problem.

Community
  • 1
  • 1
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
0

@Tal Kanel is absolutely right, you do not have to worry about device being in deep sleep here because gatt service binder threads which are delivering the result by calling the BluetoothGattCallback will wake your application up. But you still need to have a android service to keep your application alive in background.

kaps
  • 190
  • 3
  • 9
  • 1
    if you can, then please provide an AOSP code where can I see if it wakes up the processor. – Marian Paździoch Apr 02 '15 at 07:17
  • There is no explicit waking up of the processor programmatically. As far as I understand, BLE chip wakes up the host threads by delivering the connection and indication events. And in turn host wakes up the GattService binder threads waiting for indications/connection events and then those binder threads deliver the results to the application. In our application also we need prolonged connectivity and our peripheral keeps on sending data to the central every min, which is received by application even in deep sleep as expected. – kaps Apr 02 '15 at 23:24