0

I have an Android 10 BLE app that I've been working on. It periodically connects and disconnects to and from two GATT servers. This works while the app is active, and I have it on a foreground service so that the connections keep going out while the phone is in use. However, the end goal of my application requires these connections to keep happening when the screen is off. I've acquired a WakeLock to be safe, and even that is not capable of letting the BLE connections go out.

Is there anything I can do with Android 10 to do this? Thank you.

Damino
  • 91
  • 1
  • 8
  • 1
    A Foreground Service should be enough. Wake lock should not improve anything. Do you only connect / disconnect or do you also perform a BLE scan? – Emil Jun 16 '20 at 08:05
  • I am not performing a BLE scan. I get and store the MAC addresses of the servers before creating the foreground service, and the foreground service uses those stored values to connect. When the screen turns back on, the connection initiation process begins again. – Damino Jun 16 '20 at 08:07
  • 1
    That should work. Do you mean that as soon as you turn the screen off the connection drops? – Emil Jun 16 '20 at 12:53
  • The connection doesn't drop, but I can't connect to anything else. There seems to be a connection queue building up though, because I've gotten to a point where I turn my screen on and the app crashes due to having too many broadcast operations enqueued. – Damino Jun 16 '20 at 17:32
  • 1
    "broadcast operations"? I think you need to show your code here. A Bluetooth HCI log would be nice as well. In that log you can see if Android really attempts to connect or not. – Emil Jun 16 '20 at 17:40
  • I can't seem to generate a log, even with the developer option enabled. I am however seeing that existing connections stop dead in their tracks if the screen is off, not even disconnecting, but staying in limbo. Once I turn the screen back on, without even unlocking, things try to go back to normal. Could these events be firing off onPause() / onResume() and having things go haywire there? I'm going to experiment with that in the meantime. – Damino Jun 16 '20 at 19:20
  • I would like to update that I was definitely mistaken. Those methods change nothing in terms of screen-off functionality. It's still giving up, even if I disable battery optimizations in my settings. – Damino Jun 16 '20 at 19:30

1 Answers1

0

I think I solved it. I ended up changing my autoConnect in connectGatt() to false and it just started working.

Damino
  • 91
  • 1
  • 8
  • 1
    That should mean the connection attempt will be aborted if it doesn't succeed in like 30 seconds. See https://stackoverflow.com/questions/40156699/which-correct-flag-of-autoconnect-in-connectgatt-of-ble for more info. – Emil Jun 16 '20 at 23:31