从 Android M 开始,如果您在位置设置中启用了蓝牙扫描选项(请参见屏幕截图),即使关闭了全局定位功能,也可以在后台扫描蓝牙设备。
为了扫描 BLE 设备,必须满足以下条件:
-
已授予
COARSE_LOCATION
或FINE_LOCATION
权限。
以及以下之一:
- 已启用全局位置选择器。
- 蓝牙扫描选项已启用(见屏幕截图)。
我可以检查权限是否已授予,位置选择器的状态是否正常。我无法做的是弄清楚如何检查蓝牙扫描
选项的状态?
非常感谢任何见解!
最佳答案
经过大量的查找,终于找到了一种了解状态的方法:
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 - 检测定位的 "Bluetooth scanning"是否开启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004377/