Android-Bluetooth-Berechtigungen (aktualisiert auf Android 12)

Android 11 und niedrigere Zielversion

https://developer.android.com/guide/topics/connectivity/bluetooth/permissions

  1. BLUETOOTH: Berechtigung zum Zugriff auf den Bluetooth-Adapter, der zum Ausführen von Bluetooth-Vorgängen verwendet wird.

  2. BLUETOOTH_ADMIN: Verwalten Sie die Berechtigungen des Bluetooth-Adapters, einschließlich Aktivieren/Deaktivieren von Bluetooth, Scannen von Geräten und Koppeln.

  3. ACCESS_FINE_LOCATIONOder ACCESS_COARSE_LOCATION: Berechtigung zum Zugriff auf den Gerätestandort. In Android 6.0 und höher ist eine Standortberechtigung erforderlich, um Bluetooth-Geräte in der Nähe zu scannen.

  4. ACCESS_BACKGROUND_LOCATION: Berechtigung zum Zugriff auf den Gerätestandort im Hintergrund. Diese Berechtigung wird normalerweise verwendet, wenn im Hintergrund nach Bluetooth-Geräten gesucht wird.

  5. Erfordert die Aktivierung mobiler Ortungsdienste

    public boolean notNeedGps(Activity activity) {
    
    
        if (NullUtils.surviveActivity(activity)) {
    
    
            LocationManager manager = (LocationManager) activity.getSystemService(LOCATION_SERVICE);
            boolean isGPS = false;
            if (manager != null) {
    
    
                isGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            }
            if (!isGPS) {
    
    
                ConfirmDialog.Builder builder = new ConfirmDialog.Builder(activity);
                builder.setTitle(activity.getString(R.string.tip))
                .setPositiveText("启用")
                .setContent("MeFengon需要启用手机定位服务\n否则手机导航和定位等功能将不可用")
                .setOnConfirmDialogClickListener(new ConfirmDialog.OnConfirmDialogClickListener() {
    
    
                    @Override
                    public void onPositiveClick() {
    
    
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onNegativeClick() {
    
    

                    }
                });
                builder.build().show();
            }
            return isGPS;
        } else {
    
    
            return true;
        }
    }

Neue Bluetooth-Berechtigungen in Android 12

https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions?hl=zh-cn

  1. Wenn Ihre App nach Bluetooth-Geräten sucht, z. B. Bluetooth Low Energy (BLE)-Peripheriegeräten, fügen Sie BLUETOOTH_SCANdie Berechtigung zum Manifest Ihrer App hinzu.
  2. Wenn Ihre App das aktuelle Gerät für andere Bluetooth-Geräte erkennbar macht, fügen Sie BLUETOOTH_ADVERTISEdie Berechtigung zum Manifest Ihrer App hinzu.
  3. Wenn Ihre App mit gekoppelten Bluetooth-Geräten kommuniziert, fügen Sie BLUETOOTH_CONNECTdie Berechtigung zum Manifest Ihrer App hinzu.
  4. Informationen zu älteren Bluetooth-bezogenen Berechtigungserklärungen finden Sie im android:maxSdkVersion 设为 30. Dieser App-Kompatibilitätsschritt hilft dem System, Ihrer App nur die Bluetooth-Berechtigungen zu gewähren, die sie für die Installation auf einem Gerät mit Android 12 benötigt.
<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.
         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. -->
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

    <!-- Needed only if your app makes the device discoverable to Bluetooth
         devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

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

Die App ermittelt keinen physischen Standort

https://developer.android.google.cn/about/versions/12/features/bluetooth-permissions?hl=zh-cn#not-derive-physical-location

<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" />

    <!-- 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" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

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

Zeigt die verwendete Bluetooth-Funktion an

https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#features

klassisches Bluetooth

<uses-feature android:name="android.hardware.bluetooth" android:required="true"/>

Bluetooth Low Energy

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

Überprüfen Sie die Verfügbarkeit der Funktionen

// Use this check to determine whether Bluetooth classic is supported on the device.
// Then you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
    
    
    Toast.makeText(this, R.string.bluetooth_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
    
    
    Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
    finish();
}

おすすめ

転載: blog.csdn.net/weixin_35691921/article/details/131212145