检测“蓝牙扫描"是否存在.位置已开启 [英] Detect if "Bluetooth scanning" for location is turned on

查看:157
本文介绍了检测“蓝牙扫描"是否存在.位置已开启的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since Android M it is possible to scan for Bluetooth devices in the background even if the global location is turned off if you have enabled the Bluetooth scanning option in location settings (see screenshot).

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

In order to scan for BLE devices, the following conditions must be met:

    授予
  • COARSE_LOCATION FINE_LOCATION 权限.
  • COARSE_LOCATION or FINE_LOCATION permission granted.

以及以下之一:

  • 已启用全局位置选择器.
  • 启用了
  • 蓝牙扫描选项(请参见屏幕截图).
  • Global Location selector enabled.
  • Bluetooth scanning option enabled (see screenshot).

我可以检查是否已授予许可,并且位置选择器的状态也很好.我没办法做的是如何检查蓝牙扫描选项的状态?

I can check that the permission is granted and the state of the location selector just fine. What I haven't been able to do is figure out how to check the state of the Bluetooth Scanning option?

任何见解都将受到赞赏!

Any insights are much appreciated!

推荐答案

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

After a lot of search, I finally found a way to know the state:

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

You can also use "wifi_scan_always_enabled" to get wifi scanning state. It doesn't require any permissions.

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

I will update this post if I find a way to redirect the user to the screen or to disable it.

这篇关于检测“蓝牙扫描"是否存在.位置已开启的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆