检测位置的"蓝牙扫描"是否已打开

jca*_*ady 6 android bluetooth android-location bluetooth-lowenergy android-bluetooth

从Android M开始,如果您在位置设置中启用了蓝牙扫描选项,即使全局位置已关闭,也可以在后台扫描蓝牙设备(请参见屏幕截图).

要扫描BLE设备,必须满足以下条件:

  • COARSE_LOCATIONFINE_LOCATION许可.

以下之一:

  • 启用全局位置选择器.
  • 启用蓝牙扫描选项(见屏幕截图).

我可以检查授予权限和位置选择器的状态就好了.我无法做到的是弄清楚如何检查Bluetooth Scanning选项的状态?

任何见解都非常感谢!

棉花糖蓝牙扫描选项

Nit*_*G42 6

经过一番查找,终于找到了一种了解状态的方法:

try {
    ContentResolver resolver = getApplicationContext().getContentResolver();
    int result = Settings.Global.getInt(resolver,"ble_scan_always_enabled");
    if(result == 1) {
        // Bluetooth scanning is on
    } else {
        // Bluetooth scanning is off
    }
} catch(SettingNotFoundException e) {
    // Settings doesn't exist, on Android < 6
}
Run Code Online (Sandbox Code Playgroud)

您还可以用来"wifi_scan_always_enabled"获取 wifi 扫描状态。它不需要任何权限。

如果我找到一种将用户重定向到屏幕或禁用它的方法,我将更新这篇文章。