简体   繁体   中英

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. I am going off of this documentation https://developer.android.com/guide/topics/connectivity/bluetooth/permissions , but the wording confuses me. 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. 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. So would I be good with following the Android 12 target instructions or the Android 11 or Lower target instructions?

You would check for android version. 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

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

    <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};
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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