简体   繁体   English

如果我想使用蓝牙和目标 SDK 31 但我的最低 SDK 是 23,我需要在 Android 上获得什么权限?

[英]What permissions would I need on Android if I want to use Bluetooth and Target SDK 31 but my minimum SDK is 23?

Problem: I am wanting to make an application that requires Bluetooth functionality on Android and I'm not quite sure what permissions are required.问题:我想在 Android 上创建一个需要蓝牙功能的应用程序,但我不太确定需要哪些权限。 I am going off of this documentation https://developer.android.com/guide/topics/connectivity/bluetooth/permissions , but the wording confuses me.我将退出此文档https://developer.android.com/guide/topics/connectivity/bluetooth/permissions ,但措辞让我感到困惑。 In my app gradle file my targetSdk and compileSdk are both set to 31 which would be Android 12. My minSdk is set to 23 however which would be Android 6.0.在我的应用程序 gradle 文件中,我的targetSdkcompileSdk都设置为 31,这将是 Android 12。我的minSdk设置为 23,但是这将是 ZE84E30B9390CDB64DB6DB2C9.AB8 At the very least I know my application will need to be able to scan for Bluetooth devices, connect, and communicate with devices.至少我知道我的应用程序需要能够扫描蓝牙设备、连接设备并与设备通信。 My app will not derive location from the Bluetooth scans.我的应用程序不会从蓝牙扫描中获取位置。

For a bare minimum my permissions needed would look like this:对于最低限度,我需要的权限如下所示:


<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.
         If your app doesn't use Bluetooth scan results to derive physical
         location information, you can strongly assert that your app
         doesn't derive physical location. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app communicates with already-paired Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    ...
</manifest>

This comes from the linked webpage under "Target Android 12 or Higher" but I'm wondering if I also need to abide by the "Target Android 11 or Lower" section as well since my minimum Android SDK is Android 6.0. This comes from the linked webpage under "Target Android 12 or Higher" but I'm wondering if I also need to abide by the "Target Android 11 or Lower" section as well since my minimum Android SDK is Android 6.0. So would I be good with following the Android 12 target instructions or the Android 11 or Lower target instructions?那么,我是否适合遵循 Android 12 目标指令或 Android 11 或更低目标指令?

You would check for android version.您将检查 android 版本。 Then if Android is 12 or higher then do A and if it is 11 or lower do B. Use the link below to learn more about build versions然后,如果 Android 为 12 或更高,则执行 A,如果为 11 或更低,则执行 B。使用下面的链接了解有关构建版本的更多信息

https://developer.android.com/reference/android/os/Build.VERSION https://developer.android.com/reference/android/os/Build.VERSION

You can use permissions for Bluetooth as follows for targetSdk set to 31 and minSdk to 23您可以如下使用蓝牙权限,将targetSdk设置为 31,将minSdk设置为 23

    <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.BLUETOOTH_CONNECT" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:targetApi="s" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Then while requesting permissions,然后在请求权限时,

    private String[] getRequiredPermissions() {
        int targetSdkVersion = getApplicationInfo().targetSdkVersion;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && targetSdkVersion >= Build.VERSION_CODES.S) {
            return new String[]{Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT};
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && targetSdkVersion >= Build.VERSION_CODES.Q) {
            return new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
        } else return new String[]{Manifest.permission.ACCESS_COARSE_LOCATION};
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM