当针对 Android 12 (targetSdkVersion 32) 时,在 Android 12 之前的设备上使用蓝牙时,我是否应该请求位置权限

问题描述 投票:0回答:2

我的应用程序从前台服务扫描并连接到 BLE 设备。我想升级 SDK 并面向 Android 12 (

targetSdkVersion 32
) 并支持旧版本的 Android。

Android 12 引入了有关蓝牙权限的更改。

Android 文档说

自 Android 12 起:BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT 和 BLUETOOTH_SCAN 权限是运行时权限。

  1. 如果您的应用程序不使用蓝牙扫描结果来获取物理位置,您可以强烈断言您的应用程序从未使用蓝牙权限来获取物理位置。为此,请完成以下步骤:

android:usesPermissionFlags
属性添加到您的
BLUETOOTH_SCAN
权限声明中,并将该属性的值设置为
neverForLocation

  1. 如果您的应用不需要位置信息,请从应用清单中删除
    ACCESS_FINE_LOCATION
    权限。
<manifest>
    <!-- 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" />

    <!-- 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>

我了解当应用程序在 Android 12 上运行时,应用程序不应请求位置权限。我的问题是:

  1. 旧设备怎么办?我的应用程序在 Android 12 之前版本上运行时是否应该请求运行时位置权限?
  2. 清单中存在
    ACCESS_FINE_LOCATION
    ACCESS_COARSE_LOCATION
    ACCESS_BACKGROUND_LOCATION
    权限会怎样?
  3. 在服务中使用蓝牙会怎样? Android 文档 还指出,如果应用程序在服务中使用蓝牙,则该应用程序应请求
    ACCESS_BACKGROUND_LOCATION
    。应用程序是否应该请求后台位置访问权限?我很困惑。
android bluetooth location android-permissions
2个回答
8
投票

广告 1. 我的应用程序在 Android 12 之前版本上运行时是否应该请求运行时位置权限?

是的。在 Android 12 之前的版本上运行时,应用程序应请求运行时位置权限

广告 2. 清单中存在

ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
ACCESS_BACKGROUND_LOCATION
权限会怎样?

这些条目应出现在清单中。另外使用

android:maxSdkVersion
标记它们。 清单应类似于:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="package.com">
    <!-- https://developer.android.com/guide/topics/connectivity/bluetooth/permissions -->
    ...
    <!-- 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"/>

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
        android:maxSdkVersion="28"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
        android:maxSdkVersion="30"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
        android:maxSdkVersion="30"/>

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    ...
</manifest>

广告 3. 当从前台服务使用蓝牙时,应用程序是否应该请求

ACCESS_BACKGROUND_LOCATION
权限?

是的。应用程序应该请求它。

根据 Android 版本,应用程序应请求:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
   // `BLUETOOTH_SCAN` and `BLUETOOTH_CONNECT` on Android 12 and newer
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
   // `ACCESS_FINE_LOCATION` and `ACCESS_BACKGROUND_LOCATION` on Android 10..<12
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
   // `ACCESS_COARSE_LOCATION` and `ACCESS_FINE_LOCATION` on Android 7..<10
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   // `ACCESS_COARSE_LOCATION` on Android 6
} else {
   // None for previous versions
}

0
投票

如果您不需要需要使用

address
提供的
BLE devices
字段,则无需请求位置权限或要求用户启用位置。

只需将usesPermissionFlags 添加到您的蓝牙扫描权限即可:

<uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />

您无需额外权限即可扫描附近的设备

© www.soinside.com 2019 - 2024. All rights reserved.