Detect if “Bluetooth scanning” for location is turned on

折月煮酒 提交于 2020-12-01 10:49:19

问题


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).

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

  • COARSE_LOCATION or FINE_LOCATION permission granted.

And one of the following:

  • 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!


回答1:


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
}

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.



来源:https://stackoverflow.com/questions/39004377/detect-if-bluetooth-scanning-for-location-is-turned-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!