Android 检查蓝牙是否处于休眠模式

Android check is bluetooth in doze mode

美好的一天。 我想知道蓝牙扫描现在是否不可用(打瞌睡模式锁定?)。

现在我用这个方法检查:

fun isInDozeMode(context: Context) : Boolean {
    val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && powerManager.isDeviceIdleMode
}

但是这个方法 returns 即使我刚刚收到蓝牙 (iBeacon) 事件也是如此。

检测应用何时因打瞌睡 enters/exits 空闲模式

OS 将发送 DEVICE_IDLE_MODE_CHANGED 广播,以便我们可以跟踪应用程序何时进入或退出空闲模式。

注意:设备偶尔开始或结束10分钟空闲维护时会发送此广播window。

参见:https://developer.android.com/reference/android/os/PowerManager.html#ACTION_DEVICE_IDLE_MODE_CHANGED)

蓝牙不受打盹模式的直接影响:

Doze does not affect the ability to do bluetooth scans or run Handler-based timers, but it does affect the ability to use the Android AlarmManager system, which some beacon-based systems use to schedule bluetooth scans. For apps based on the Android Beacon Library, the disabling of Alarms does not cause problems in scanning for beacons because it only uses Alarms as a backup should Handler-based timers fail. Tests on a Nexus 9 running the third preview release of Android M (build number MPA44l) show that beacon detection continues normally under Doze.

查看我的博客post here. While that blog post was written for Android 6.0, subsequent doze changes added in 7.0 also do not affect Bluetooth scanning. See here.