Android 蓝牙权限(更新到 Android 12)
•
移动开发
Android 蓝牙权限
- Android 11 及以下目标版本
- Android 12 中的新蓝牙权限
- 应用不推导物理位置
- 指明使用的蓝牙功能
- 检查功能可用性
Android 11 及以下目标版本
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
-
BLUETOOTH:访问蓝牙适配器的权限,用于执行蓝牙操作。
-
BLUETOOTH_ADMIN:管理蓝牙适配器的权限,包括启用/禁用蓝牙、扫描设备和进行配对等操作。
-
ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION:访问设备位置的权限。在 Android 6.0 及以上版本中,需要获取位置权限才能扫描附近的蓝牙设备。
-
ACCESS_BACKGROUND_LOCATION:在后台访问设备位置的权限。该权限通常在后台扫描蓝牙设备时使用。
-
需要手机开启定位服务
public boolean notNeedGps(Activity activity) {
if (NullUtils.surviveActivity(activity)) {
LocationManager manager = (LocationManager) activity.getSystemService(LOCATION_SERVICE);
boolean isGPS = false;
if (manager != null) {
isGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
if (!isGPS) {
ConfirmDialog.Builder builder = new ConfirmDialog.Builder(activity);
builder.setTitle(activity.getString(R.string.tip))
.setPositiveText("启用")
.setContent("MeFengon需要启用手机定位服务\n否则手机导航和定位等功能将不可用")
.setOnConfirmDialogClickListener(new ConfirmDialog.OnConfirmDialogClickListener() {
@Override
public void onPositiveClick() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
activity.startActivity(intent);
}
@Override
public void onNegativeClick() {
}
});
builder.build().show();
}
return isGPS;
} else {
return true;
}
}
Android 12 中的新蓝牙权限
https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions?hl=zh-cn
- 如果您的应用查找蓝牙设备(如蓝牙低功耗 (BLE) 外围设备),请向应用的清单中添加 BLUETOOTH_SCAN 权限。
- 如果您的应用使当前设备可被其他蓝牙设备检测到,请向应用的清单中添加 BLUETOOTH_ADVERTISE 权限。
- 如果您的应用与已配对的蓝牙设备通信,请向应用的清单中添加 BLUETOOTH_CONNECT 权限。
- 对于旧版蓝牙相关的权限声明,请将 android:maxSdkVersion 设为 30。此应用兼容性步骤有助于系统仅向您的应用授予在搭载 Android 12 的设备上安装时所需的蓝牙权限。
...
应用不推导物理位置
https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions?hl=zh-cn#not-derive-physical-location
...
指明使用的蓝牙功能
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#features
经典蓝牙
低功耗蓝牙
检查功能可用性
// Use this check to determine whether Bluetooth classic is supported on the device.
// Then you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
Toast.makeText(this, R.string.bluetooth_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/f910e119c6.html
