Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if "Bluetooth scanning" for location is turned on

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

like image 410
jcady Avatar asked Aug 17 '16 19:08

jcady


People also ask

What is Bluetooth scanning location?

With Bluetooth scanning turned on, your device will search for Bluetooth-enabled devices such as the beacons used in malls and airports to get a speedy location fix. In many cases, the reach and dispersion of a Bluetooth beacon can be quicker than getting a GPS signal lock for triangulating your location.

Does Bluetooth use location?

Bluetooth technology offers different location methodologies, and it enables location-based solutions all the way from simple presence detection to reliable centimeter-level positioning and every degree of accuracy in between.

Should I turn on Bluetooth scanning?

However, leaving it on requires Bluetooth, which can open yourself up to hackers if you are not running the latest version of your operating system and updated apps. To be safe, it's better to turn off nearby device scanning and connect only when you are looking for a nearby device to connect to.

Can Bluetooth work without location?

To be able to connect to a scale you HAVE TO give the app Location permission. The fact is that most applications that use Bluetooth don't need access to Location at all.


1 Answers

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.

like image 53
NitroG42 Avatar answered Sep 19 '22 13:09

NitroG42