Android 蓝牙权限(更新到 Android 12)

Android 11 及以下目标版本

https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

  1. BLUETOOTH:访问蓝牙适配器的权限,用于执行蓝牙操作。

  2. BLUETOOTH_ADMIN:管理蓝牙适配器的权限,包括启用/禁用蓝牙、扫描设备和进行配对等操作。

  3. ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION:访问设备位置的权限。在 Android 6.0 及以上版本中,需要获取位置权限才能扫描附近的蓝牙设备。

  4. ACCESS_BACKGROUND_LOCATION:在后台访问设备位置的权限。该权限通常在后台扫描蓝牙设备时使用。

  5. 需要手机开启定位服务

    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

  1. 如果您的应用查找蓝牙设备(如蓝牙低功耗 (BLE) 外围设备),请向应用的清单中添加 BLUETOOTH_SCAN 权限。
  2. 如果您的应用使当前设备可被其他蓝牙设备检测到,请向应用的清单中添加 BLUETOOTH_ADVERTISE 权限。
  3. 如果您的应用与已配对的蓝牙设备通信,请向应用的清单中添加 BLUETOOTH_CONNECT 权限。
  4. 对于旧版蓝牙相关的权限声明,请将 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>

应用不推导物理位置

https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions?hl=zh-cn#not-derive-physical-location

<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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清风徐来辽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值