美文网首页
Android 搜索蓝牙相关设备权限声明及适配

Android 搜索蓝牙相关设备权限声明及适配

作者: 小强开学前 | 来源:发表于2023-06-05 14:52 被阅读0次

    确实需要获取位置信息的,还是需要声明Location相关权限

    1. Android 12 及以上,仅需配置BLUETOOTH_SCAN,并声明android:usesPermissionFlags="neverForLocation"

    2. Android 9(API 28) 以上,需要声明BLUETOOTH,BLUETOOTH_ADMINACCESS_FINE_LOCATION,参考官方文档

    3. Android 9(API 28) 及以下,可以声明ACCESS_COARSE_LOCATION或者ACCESS_FINE_LOCATION,推荐ACCESS_COARSE_LOCATION

    综合起来,AndroidManifest文件为:

        <!-- 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"/>
    <!--    安卓9及以下可以使用模糊定位-->
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
            android:maxSdkVersion="28"/>
    <!--    安卓10,11需要使用精准定位-->
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
            tools:targetApi="q"
            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. -->
    <!--    安卓12及以上,不需要定位-->
        <uses-permission android:name="android.permission.BLUETOOTH_SCAN"
            android:usesPermissionFlags="neverForLocation"
            tools:targetApi="s" />
    
        <!-- Needed only if your app communicates with already-paired Bluetooth
             devices. -->
        <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    

    相关文章

      网友评论

          本文标题:Android 搜索蓝牙相关设备权限声明及适配

          本文链接:https://www.haomeiwen.com/subject/oxtwedtx.html