7

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!

Marshmallow Bluetooth Scanning Option

mbo
  • 4,611
  • 2
  • 34
  • 54
jcady
  • 3,850
  • 2
  • 21
  • 21
  • I'm very interested in how to detect the state, too. Because when this is turned on, our BLE connection makes trouble on some devices... – mbo Sep 27 '17 at 12:40

1 Answers1

5

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.

NitroG42
  • 5,336
  • 2
  • 28
  • 32
  • Does not work for me on on android Q. `"ble_scan_always_enabled"` cannot be found – apex39 Jul 02 '20 at 12:43
  • 1
    @apex39 you can also use "WifiManager.isScanAlwaysAvailable()" to get wifi scanning state. – wangqi060934 Mar 30 '21 at 09:41
  • For people getting `SettingNotFoundException` - try toggling WiFi & Bluetooth scanning on/off in Settings and run your code again. – Tomy Oct 20 '21 at 15:30