Unused BluetoothAdapter causes SecurityException
I somehow triggered a Bluetooth-related permission error in F-Droid Basic which occurred in this code in AppDetailsActivity
:
@SuppressWarnings("unused")
protected BluetoothAdapter bluetoothAdapter;
// ...
bluetoothAdapter = getBluetoothAdapter();
// ...
private BluetoothAdapter getBluetoothAdapter() {
// to use the new, recommended way of getting the adapter
// http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
return ContextCompat.getSystemService(this, BluetoothManager.class).getAdapter();
}
As the annotation suggests, the field is unused, so there doesn't seem to be a reason for this code to exist at all. It seems to have been used by shareApkBluetooth
, which was commented out in commit 402cbcc3. It was moved to AppDetailsActivity
in commit 148d1cdc from FDroidApp
, where it also had the @SuppressWarnings("unused")
annotation.
If there is some reason for this code to exist, it could perhaps be delegated to BluetoothClient
so that it's disabled in F-Droid Basic (which doesn't use Bluetooth, I think?) and doesn't cause permission issues.
Anyway, here is the stacktrace. Some line numbers might be off, as I was testing some unrelated changes.
Application does not have bluetooth permission, registering is failed
java.lang.SecurityException: Need BLUETOOTH permission: Neither user 10368 nor current process has android.permission.BLUETOOTH.
at android.os.Parcel.createExceptionOrNull(Parcel.java:2386)
at android.os.Parcel.createException(Parcel.java:2370)
at android.os.Parcel.readException(Parcel.java:2353)
at android.os.Parcel.readException(Parcel.java:2295)
at android.bluetooth.IBluetoothManager$Stub$Proxy.registerStateChangeCallback(IBluetoothManager.java:890)
at android.bluetooth.BluetoothAdapter.<init>(BluetoothAdapter.java:1032)
at android.bluetooth.BluetoothAdapter.getDefaultAdapter(BluetoothAdapter.java:1012)
at android.bluetooth.BluetoothManager.<init>(BluetoothManager.java:90)
at android.app.SystemServiceRegistry$11.createService(SystemServiceRegistry.java:497)
at android.app.SystemServiceRegistry$11.createService(SystemServiceRegistry.java:494)
at android.app.SystemServiceRegistry$CachedServiceFetcher.getService(SystemServiceRegistry.java:2350)
at android.app.SystemServiceRegistry.getSystemService(SystemServiceRegistry.java:2030)
at android.app.ContextImpl.getSystemService(ContextImpl.java:2003)
at androidx.appcompat.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:180)
at android.view.ContextThemeWrapper.getSystemService(ContextThemeWrapper.java:188)
at android.app.Activity.getSystemService(Activity.java:6896)
at android.content.Context.getSystemService(Context.java:3736)
at androidx.core.content.ContextCompat$Api23Impl.getSystemService(ContextCompat.java:1039)
at androidx.core.content.ContextCompat.getSystemService(ContextCompat.java:768)
at org.fdroid.fdroid.views.AppDetailsActivity.getBluetoothAdapter(AppDetailsActivity.java:775)
at org.fdroid.fdroid.views.AppDetailsActivity.onCreate(AppDetailsActivity.java:132)
at android.app.Activity.performCreate(Activity.java:8207)
at android.app.Activity.performCreate(Activity.java:8191)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4022)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2336)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: android.os.RemoteException: Remote stack trace:
at android.app.ContextImpl.enforce(ContextImpl.java:2115)
at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:2143)
at com.android.server.BluetoothManagerService.registerStateChangeCallback(BluetoothManagerService.java:913)
at android.bluetooth.IBluetoothManager$Stub.onTransact(IBluetoothManager.java:438)
at android.os.Binder.execTransactInternal(Binder.java:1190)