mercredi 21 janvier 2015

Accessing @hide method from BluetoothAdapter

I recently posted a question asking on how to set discoverability to "infinity" on Android programmatically (Android: Set Bluetooth Discoverability Unbounded) but since I haven't had much success at solving the problem now I am attacking from another angle.


I recently read that @hide methods can be accessed programmatically using 'Reflection'. By taking a quick glance at the BluetoothAdapter.class one can see the following two methods:



/** @hide */
public int getDiscoverableTimeout() {
if (getState() != STATE_ON) return -1;
try {
synchronized(mManagerCallback) {
if (mService != null) return mService.getDiscoverableTimeout();
}
} catch (RemoteException e) {Log.e(TAG, "", e);}
return -1;
}

/** @hide */
public void setDiscoverableTimeout(int timeout) {
if (getState() != STATE_ON) return;
try {
synchronized(mManagerCallback) {
if (mService != null) mService.setDiscoverableTimeout(timeout);
}
} catch (RemoteException e) {Log.e(TAG, "", e);}
}


I got really excited because I thought that they would be my ultimate solution to this ongoing problem, so I wrote the following code:



//ADAPTER
BluetoothAdapter mBluetoothAdapter = null;
Object aBluetoothAdapter;

int letussee=0;

// Intent request codes
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
private static final int REQUEST_ENABLE_DSC = 3;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
finish();
return;
}

try {
Method m = mBluetoothAdapter.getClass().getDeclaredMethod("setDiscoverableTimeout", new Class[] {int.class});
m.setAccessible(true);
Method d = mBluetoothAdapter.getClass().getDeclaredMethod("getDiscoverableTimeout", null);
d.setAccessible(true);
m.invoke(null, new Object[]{0});
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "POOP 1", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "POOP 2", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "POOP 3", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "POOP 4", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}

}


Unfortunately, after running the code the app crashed and I got stuck once more. I don't really know where to move form here so that I can once and for all use those methods and set my bluetooth discoverable for ever...


Here is LogCat:



01-21 23:13:02.041: W/ActivityThread(4672): Application com.example.downtoone is waiting for the debugger on port 8100...
01-21 23:13:02.051: I/System.out(4672): Sending WAIT chunk
01-21 23:13:02.051: I/dalvikvm(4672): Debugger is active
01-21 23:13:02.251: I/System.out(4672): Debugger has connected
01-21 23:13:02.251: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:02.451: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:02.651: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:02.852: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:03.052: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:03.242: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:03.442: I/System.out(4672): waiting for debugger to settle...
01-21 23:13:03.642: I/System.out(4672): debugger has settled (1448)
01-21 23:13:04.914: D/dalvikvm(4672): threadid=1: still suspended after undo (sc=1 dc=1)
01-21 23:13:14.733: D/libEGL(4672): loaded /system/lib/egl/libEGL_mali.so
01-21 23:13:14.753: D/libEGL(4672): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-21 23:13:14.753: D/libEGL(4672): loaded /system/lib/egl/libGLESv2_mali.so
01-21 23:13:14.793: D/OpenGLRenderer(4672): Enabling debug mode 0
01-21 23:13:42.350: W/jdwp(4672): Debugger is telling the VM to exit with code=1
01-21 23:13:42.350: I/dalvikvm(4672): GC lifetime allocation: 38875 bytes
01-21 23:14:46.524: W/ActivityThread(5120): Application com.example.downtoone is waiting for the debugger on port 8100...
01-21 23:14:46.544: I/System.out(5120): Sending WAIT chunk
01-21 23:14:46.544: I/dalvikvm(5120): Debugger is active
01-21 23:14:46.744: I/System.out(5120): Debugger has connected
01-21 23:14:46.744: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:46.945: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:47.145: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:47.345: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:47.545: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:47.745: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:47.946: I/System.out(5120): waiting for debugger to settle...
01-21 23:14:48.146: I/System.out(5120): debugger has settled (1453)
01-21 23:14:49.377: D/dalvikvm(5120): threadid=1: still suspended after undo (sc=1 dc=1)
01-21 23:17:58.226: D/AndroidRuntime(5120): Shutting down VM
01-21 23:17:58.226: W/dalvikvm(5120): threadid=1: thread exiting with uncaught exception (group=0x40e912a0)
01-21 23:17:58.266: E/AndroidRuntime(5120): FATAL EXCEPTION: main
01-21 23:17:58.266: E/AndroidRuntime(5120): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.downtoone/com.example.downtoone.MainActivity}: java.lang.NullPointerException: expected receiver of type android.bluetooth.BluetoothAdapter, but got null
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread.access$700(ActivityThread.java:134)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.os.Looper.loop(Looper.java:137)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread.main(ActivityThread.java:4867)
01-21 23:17:58.266: E/AndroidRuntime(5120): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:17:58.266: E/AndroidRuntime(5120): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:17:58.266: E/AndroidRuntime(5120): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
01-21 23:17:58.266: E/AndroidRuntime(5120): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
01-21 23:17:58.266: E/AndroidRuntime(5120): at dalvik.system.NativeStart.main(Native Method)
01-21 23:17:58.266: E/AndroidRuntime(5120): Caused by: java.lang.NullPointerException: expected receiver of type android.bluetooth.BluetoothAdapter, but got null
01-21 23:17:58.266: E/AndroidRuntime(5120): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 23:17:58.266: E/AndroidRuntime(5120): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 23:17:58.266: E/AndroidRuntime(5120): at com.example.downtoone.MainActivity.onCreate(MainActivity.java:54)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.Activity.performCreate(Activity.java:5047)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-21 23:17:58.266: E/AndroidRuntime(5120): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
01-21 23:17:58.266: E/AndroidRuntime(5120): ... 11 more


Thanks fellas






Aucun commentaire:

Enregistrer un commentaire