roj*_*and 4 android location bluetooth android-permissions
我的应用程序从前台服务扫描并连接到 BLE 设备。我想升级 SDK 并面向 Android 12 ( targetSdkVersion 32) 并支持旧版本的 Android。
Android 12 引入了有关蓝牙权限的更改。
从 Android 12 开始:BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT 和 BLUETOOTH_SCAN 权限是运行时权限。
- 如果您的应用程序不使用蓝牙扫描结果来获取物理位置,则您可以强烈断言您的应用程序从未使用蓝牙权限来获取物理位置。为此,请完成以下步骤:
将该
android:usesPermissionFlags属性添加到您的BLUETOOTH_SCAN权限声明中,并将该属性的值设置为neverForLocation。
- 如果您的应用不需要位置信息,请
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>
Run Code Online (Sandbox Code Playgroud)
我了解当应用程序在 Android 12 上运行时,应用程序不应请求位置权限。我的问题是:
ACCESS_FINE_LOCATION?ACCESS_COARSE_LOCATIONACCESS_BACKGROUND_LOCATIONACCESS_BACKGROUND_LOCATION. 应用程序是否应该请求后台位置访问权限?我很困惑。广告 1. 在 Android 12 之前的版本上运行时,我的应用是否应该请求运行时位置权限?
是的。在 Android 12 之前的版本上运行时,应用程序应请求运行时位置权限
广告 2. 清单中的 和 权限是什么?ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATIONACCESS_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>
Run Code Online (Sandbox Code Playgroud)
广告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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2281 次 |
| 最近记录: |