Android-Bluetooth-Berechtigungen
Android 11 und niedrigere Zielversion
https://developer.android.com/guide/topics/connectivity/bluetooth/permissions
-
BLUETOOTH
: Berechtigung zum Zugriff auf den Bluetooth-Adapter, der zum Ausführen von Bluetooth-Vorgängen verwendet wird. -
BLUETOOTH_ADMIN
: Verwalten Sie die Berechtigungen des Bluetooth-Adapters, einschließlich Aktivieren/Deaktivieren von Bluetooth, Scannen von Geräten und Koppeln. -
ACCESS_FINE_LOCATION
OderACCESS_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. -
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. -
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
- Wenn Ihre App nach Bluetooth-Geräten sucht, z. B. Bluetooth Low Energy (BLE)-Peripheriegeräten, fügen Sie
BLUETOOTH_SCAN
die Berechtigung zum Manifest Ihrer App hinzu. - Wenn Ihre App das aktuelle Gerät für andere Bluetooth-Geräte erkennbar macht, fügen Sie
BLUETOOTH_ADVERTISE
die Berechtigung zum Manifest Ihrer App hinzu. - Wenn Ihre App mit gekoppelten Bluetooth-Geräten kommuniziert, fügen Sie
BLUETOOTH_CONNECT
die Berechtigung zum Manifest Ihrer App hinzu. - 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
<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();
}