简体   繁体   English

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

[英]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:我为 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();

This code works fine on an LG Optimus One (running Android 2.2).此代码在 LG Optimus One 上运行良好(运行 Android 2.2)。 However it crashes on a Samsung Galaxy S (also running Android 2.2) with the following call stack:然而,它在三星 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)

This crash occurs whilst the.connect() method is blocked.此崩溃发生在 .connect() 方法被阻止时。 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().在 BluetoothPairingDialog.java 第 343 行的 isDeviceKeyboard() 中发生 NullPointerException,该异常正在尝试在调用 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 (eg via GrepCode ).我一直试图在 web 上找到该文件的来源,但只找到了没有第 343 行的 BluetoothPairingDialog.java 的错误版本(例如通过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.我有一个单独的boolean connectSocket() {}方法,用于检查套接字是否可以打开,如果返回 false,则使用您发布的方法。 This took care of my crashes on Galaxy phones (both methods are needed).这解决了我在 Galaxy 手机上的崩溃问题(两种方法都需要)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM