原因
为提供更高的数据保护 Android6.0版本上增加了关于Wifi和蓝牙的权限
官网地址:https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
修改方法
在AndroidManifest 中添加权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
或者
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
注意
如果targetSdkVersion 大于22 需要在代码中进行权限获取,获取方法如下:
// onResume 中进行调用
private void checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
//TODO 提示权限已经被禁用 且不在提示
return;
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_PERMISSION_BT);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSION_BT:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//TODO 请求权限成功
} else {
//TODO 提示权限已经被禁用
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}