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

时间:2016-08-17 19:01:20

标签: android bluetooth bluetooth-lowenergy android-bluetooth android-location

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

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

  • COARSE_LOCATIONFINE_LOCATION许可。

以下之一:

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

我可以检查是否授予了权限,并且位置选择器的状态就好了。我无法做到的是找出如何检查Bluetooth Scanning选项的状态?

非常感谢任何见解!

Marshmallow Bluetooth Scanning Option

1 个答案:

答案 0 :(得分:2)

经过大量搜索,我终于找到了一种了解状态的方法:

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
}

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

如果我找到将用户重定向到屏幕或禁用它的方法,我将更新此帖子。