简体   繁体   中英

Android: Connect to Bluetooth LE device in background

I'm trying to connect to a BLE Peripheral with bleDevice.connectGatt(this, true, gattCallback) from the app's Application class. The device will send advertising packages only every few hours.

This works fine as long as the app is open/active. When I turn the display off and wait a few minutes and then turn on the peripheral, the gattCallback is never called.

Is there any other way to wake up the app when the bluetooth device gets alive than creating a foreground service?

You can scan for that device, beginning from Lollipop the device will be woken up when there is a scan result. Also with Lollipop you can set a filter for the scan and if your peripheral has a fixed mac address you can use that as filter, then your device wakes up, as soon as your peripheral starts advertising.

Apart from that you could use an AlarmManager to sporadically wake up the device.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM