我试图用http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8#the-new-way-fast-detections唤醒我的前台服务
预期行为
查看第一个信标并将 PendingIntent 发送到正在使用 BeaconConsumer 启动我的前台服务的 BroadcastReceiver。
实际行为
在打瞌睡模式之后,什么都没有被调用。
重现此行为的步骤
使用此处的相同代码:http ://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8#the-new-way-fast-detections 比将手机置于打盹模式并返回终端:
adb shell dumpsys deviceidle force-idle
adb shell dumpsys deviceidle 解除强制
广播接收器代码:
class BluetoothScanReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val bleCallbackType = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1)
if (bleCallbackType != -1) {
val scanResults = intent.getParcelableArrayListExtra<Parcelable>(
BluetoothLeScanner.EXTRA_LIST_SCAN_RESULT)
println("xxxxx BluetoothScanReceiver onreceive ")
context.startBeaconScanning()
}
}
}
Ble扫描仪的初始化
val settings = ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_POWER).build()
val bluetoothManager = applicationContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter
val i = Intent(applicationContext, BluetoothScanReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, i, FLAG_UPDATE_CURRENT)
val scanner = bluetoothAdapter.bluetoothLeScanner
scanner.startScan(null, settings, pendingIntent)
移动设备型号和操作系统版本
诺基亚 5 - 安卓 8.1
即使我的应用程序被杀死/处于打盹模式/打盹模式后,当我看到第一个信标时,我应该怎么做才能启动我的前台服务?
谢谢