red symbol man
red symbol man

Reputation: 78

SecurityException when calling BluetoothAdapter.getDefaultAdapter

In onCreate() of my app I call BluetoothAdapter.getAddress(). One single device of hundreds, that are running this app, yields a java.lang.SecurityException:

java.lang.RuntimeException: Unable to start activity ComponentInfo{xx.yyy.myapp/xx.yyy.myapp.RecActivity}: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10095 nor current process has android.permission.BLUETOOTH_ADMIN. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5039) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10095 nor current process has android.permission.BLUETOOTH_ADMIN. at android.os.Parcel.readException(Parcel.java:1425) at android.os.Parcel.readException(Parcel.java:1379) at android.bluetooth.IBluetoothManager$Stub$Proxy.getAddress(IBluetoothManager.java:295) at android.bluetooth.BluetoothAdapter.getAddress(BluetoothAdapter.java:576) at xx.yyy.myapp.RecActivity.onCreate(Unknown Source) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) ... 11 more

As I can't debug on that device I would like to ask if the following solution is feasible or if there's a better way to handle the problem (this is the branch for less than JELLY_BEAN_MR2)?

BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
try {
    macAddress = ( bta != null ) ? bta.getAddress() : "";
} catch ( Exception e ) {
    macAddress = "";
}

Also, I wonder if the Android version on that device might have a bug as it seems to me that getAddress() in BluetoothAdapter.java doesn't require android.permission.BLUETOOTH_ADMIN?

Or is it possible that the user with this device has a specific root tool to lock bluetooth access from my app? And that this might be the reason for the Exception?

Or what might be the reason for the problem?

Upvotes: 2

Views: 791

Answers (1)

nkorth
nkorth

Reputation: 1694

Since it's bad practice to use catch(Exception e), use

} catch(SecurityException e){

to prevent the crash.

(As an answer now instead of a comment.)

Upvotes: 2

Related Questions