2

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?

red symbol man
  • 78
  • 1
  • 15
  • Seems most likely that they have a root tool. Permission locking tools are becoming more popular and are included in many custom roms. – nkorth Jun 30 '15 at 12:43
  • Do you know if my work around would catch the SecurityException and avoid the RuntimeException so that the app won't crash but continue to run? – red symbol man Jun 30 '15 at 14:32
  • [It's bad practice to write `catch(Exception e)` in most cases.](http://stackoverflow.com/questions/1742940/why-not-catch-general-exceptions) Here, the `RuntimeException` is thrown separately by `ActivityThread` only if you don't catch the `SecurityException`, so it should be enough to just `catch(SecurityException e)`. – nkorth Jun 30 '15 at 14:36
  • Thank you. As I can judge this is a significant part of the answer I was seeking! – red symbol man Jun 30 '15 at 15:16

1 Answers1

2

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.)

Community
  • 1
  • 1
nkorth
  • 1,684
  • 1
  • 12
  • 28