简体   繁体   English

Android:在后台连接到Bluetooth LE设备

[英]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. 我正在尝试从应用程序的Application类使用bleDevice.connectGatt(this, true, gattCallback)连接到BLE外围设备。 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. 当我关闭显示器并等待几分钟然后再打开外围设备时,永远不会调用gattCallback

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. 您可以扫描该设备,从Lollipop开始,当扫描结果出现时,该设备将被唤醒。 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. 同样,通过Lollipop,您可以为扫描设置过滤器,如果外围设备具有固定的mac地址,则可以将其用作过滤器,一旦外围设备开始投放广告,设备就会唤醒。

Apart from that you could use an AlarmManager to sporadically wake up the device. 除此之外,您还可以使用AlarmManager偶尔唤醒设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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