Pairing with SPP bluetooth device crashes Samsung Galaxy S (Android 2.2)
I've written an app for Android that attempts to connect to a serial bluetooth device, using some simple code:
UUID wellKnownSerialUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("ff:ff:ff:ff:ff:ff"); // fake address :)
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(wellKnownSerialUuid);
socket.connect();
socket.close();
This code works fine on an LG Optimus One (running Android 2.2). However it crashes on a Samsung Galaxy S (also runni开发者_开发知识库ng Android 2.2) with the following call stack:
FATAL EXCEPTION: main
ComponentInfo{com.android.settings/com.android.settings.bluetooth.BluetoothPairingDialog}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.android.settings.bluetooth.BluetoothPairingDialog.isDeviceKeyboard(BluetoothPairingDialog.java:343)
at com.android.settings.bluetooth.BluetoothPairingDialog.createView(BluetoothPairingDialog.java:222)
at com.android.settings.bluetooth.BluetoothPairingDialog.createUserEntryDialog(BluetoothPairingDialog.java:191)
at com.android.settings.bluetooth.BluetoothPairingDialog.onCreate(BluetoothPairingDialog.java:139)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
This crash occurs whilst the .connect() method is blocked. A NullPointerException is occurring in isDeviceKeyboard() in BluetoothPairingDialog.java line 343, which is in the process of trying to display the bluetooth pairing dialog during the call to connect(). I've been trying to find the source for that file on the web, but only turned up incorrect versions of BluetoothPairingDialog.java that don't have a line 343 (e.g. via GrepCode).
Can anyone point me to the correct source, or even better, suggest how I can work around this crash? I don't believe I have any control over the display of the pairing dialog (for security reasons)...
Try the following to get the socket:
// First method to create Bluetooth socket
Method m = null;
try {
m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// Second method to create Bluetooth socket
// If the previous was unsuccessful, use yours here
I have a separate boolean connectSocket() {}
method that checks if the socket can be opened, if it returns false, then the method you posted is used. This took care of my crashes on Galaxy phones (both methods are needed).
精彩评论