【问题标题】:Detect if "Bluetooth scanning" for location is turned on检测位置的“蓝牙扫描”是否打开
【发布时间】:2016-12-24 13:38:51
【问题描述】:

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

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

  • 已授予 COARSE_LOCATIONFINE_LOCATION 权限。

以及以下之一:

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

我可以检查权限是否被授予以及位置选择器的状态是否正常。我没能做的是弄清楚如何检查Bluetooth Scanning 选项的状态?

非常感谢任何见解!

【问题讨论】:

  • 我对如何检测状态也很感兴趣。因为当它打开时,我们的 BLE 连接会在某些设备上出现问题......

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


【解决方案1】:

找了好久,终于找到了知道状态的方法:

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扫描状态。 它不需要任何权限。

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

【讨论】:

  • 在 android Q 上不适合我。"ble_scan_always_enabled" 找不到
  • @apex39 你也可以使用“WifiManager.isScanAlwaysAvailable()”来获取wifi扫描状态。
  • 对于收到 SettingNotFoundException 的人 - 尝试在“设置”中打开/关闭 WiFi 和蓝牙扫描,然后再次运行您的代码。
猜你喜欢
  • 2021-12-26
  • 2015-03-03
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 2019-07-30
  • 2021-04-16
相关资源
最近更新 更多