1.权限
1.使用蓝牙需要BLUETOOTH权限
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2.Build.VERSION_CODES.M版本以后需要添加位置权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
3.Android6.0以后需要动态申请权限,动态申请权限这里不再赘述;说一下如果申请权限被拒绝,用户想再打开,引导用户到设置打开权限
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, Constant.REQUEST_LOCATION_PERMISSION);
2.位置信息
使用位置权限必须打开位置信息,所以要先检测位置信息有没有打开,如果没有打开则需要引导用户到设置中打开位置信息
1.检测位置信息是否打开
public static boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return locationManager.isLocationEnabled();
}
boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
2.引导用户打开位置信息
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, Constant.REQUEST_ENABLE_LOCATION);