2011-05-30 73 views
2

我寫的Android試圖連接到串行藍牙設備的應用程序,使用一些簡單的代碼:配對與SPP的藍牙設備崩潰,三星Galaxy S(Android 2.2的)

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()方法被阻止。 BluetoothPairingDialog.java行343中的isDeviceKeyboard()中發生NullPointerException,該行正在嘗試在connect()調用期間顯示藍牙配對對話框。我一直試圖在網絡上找到該文件的源代碼,但只發現沒有第343行的BluetoothPairingDialog.java的錯誤版本(例如,通過GrepCode)。

任何人都可以指出我的正確來源,甚至更好,建議我如何解決這個崩潰?我不相信我有過配對對話框(出於安全原因)的顯示任何控制...

回答

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 

我有一個單獨的​​方法檢查套接字是否可以打開,如果返回false,則使用您發佈的方法。這照顧了我在Galaxy手機上的崩潰(這兩種方法都是需要的)。

+0

這似乎不起作用 – SamFisher83 2011-11-15 21:11:49