与SPP蓝牙设备配对导致三星Galaxy S(Android 2.2)崩溃

时间:2011-05-30 13:40:45

标签: java android bluetooth

我为Android编写了一个试图使用一些简单代码连接到串行蓝牙设备的应用程序:

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();

此代码适用于LG Optimus One(运行Android 2.2)。然而,它在使用以下调用堆栈的三星Galaxy S(也运行Android 2.2)上崩溃:

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)

阻止.connect()方法时发生此崩溃。在BluePairingDialog.java第343行的isDeviceKeyboard()中发生NullPointerException,它正在尝试在调用connect()期间显示蓝牙配对对话框。我一直试图在网上找到该文件的来源,但只发现了没有第343行的不正确版本的BluetoothPairingDialog.java(例如通过GrepCode)。

任何人都可以向我指出正确的来源,甚至更好,建议我如何解决这次崩溃?我不相信我对配对对话框的显示有任何控制(出于安全原因)......

1 个答案:

答案 0 :(得分:1)

尝试以下方法获取套接字:

        // 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

我有一个单独的boolean connectSocket() {}方法,用于检查套接字是否可以打开,如果返回false,则使用您发布的方法。这照顾了我在Galaxy手机上的崩溃(需要两种方法)。