Android 蓝牙权限
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 的设备上安装时所需的蓝牙权限。
<manifest> <!-- Request legacy Bluetooth permissions on older devices. --> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> <!-- Needed only if your app looks for Bluetooth devices. You must add an attribute to this permission, or declare the ACCESS_FINE_LOCATION permission, depending on the results when you check location usage in your app. --> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <!-- Needed only if your app makes the device discoverable to Bluetooth devices. --> <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <!-- Needed only if your app communicates with already-paired Bluetooth devices. --> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> ...</manifest>
应用不推导物理位置
<manifest> <!-- Request legacy Bluetooth permissions on older devices. --> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> <!-- Include "neverForLocation" only if you can strongly assert that your app never derives physical location from Bluetooth scan results. --> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" /> <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- Not needed if you can strongly assert that your app never derives physical location from Bluetooth scan results and doesn't need location access for any other purpose. --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ...</manifest>
指明使用的蓝牙功能
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#features
经典蓝牙
<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
低功耗蓝牙
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
检查功能可用性
// 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://blog.csdn.net/weixin_35691921/article/details/131212145