2011-05-07 70 views
0

如果有人能帮助我,我将不胜感激。我试图连接到客户提供给我的一些自定义蓝牙硬件。我虔诚地遵循了API文档,并且正在使用以下代码(主要是从官方文档复制并缝制在一起)与蓝牙硬件建立套接字连接,用户从我的Android应用程序的列表视图中选择该套接字作为设备。Android:蓝牙 - 无法建立连接,IOException“通过对等方重置连接”

private class CommunicationThread extends Thread { 
    private final BluetoothSocket mmSocket; 

    public CommunicationThread(BluetoothDevice device) { 
     super(); 
     // use a temporary object that is later assigned to mmSocket, 
     // because mmSocket is final 
     BluetoothSocket tmp = null; 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     // get a BluetoothSocket to connect with the given BluetoothDevice 
     try { 
      // create rf communication socket using a unique identifier for this app 
      tmp = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
     } catch (Exception e) { 
      Log.d("An exception occurred during bluetooth rf communication server socket creation","NDB",e); 
     } 
     mmSocket = tmp; 

     // cancel discovery because it will slow down the connection 
     mBluetoothAdapter.cancelDiscovery(); 
    } 

    public void run() { 
     try { 
      // connect the device through the socket. This will block 
      // until it succeeds or throws an exception 
      mmSocket.connect(); 
     } catch (IOException e) { 
      Log.d("An exception occurred during bluetooth socket connection","NDB",e); 
      // unable to connect; close the socket and get out 
      try { 
       mmSocket.close(); 
      } catch (IOException closeExc) { 
       Log.d("An exception occurred while attempting to close bluetooth socket","NDB",closeExc); 
      } 
      return; 
     } 

     // do work to manage the connection 
     manageConnectedSocket(); 
    } 

    private void manageConnectedSocket() { 
     InputStream tmpIn = null; 

     // get the input steram, use temp object because 
     // member stream is final 
     try { 
      tmpIn = mmSocket.getInputStream(); 
     } catch (IOException e) { 
      Log.d("An exception occurred during bluetooth io stream creation","NDB",e); 
     } 

     final InputStream mmInStream = tmpIn; 

     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
       // read from the InputStream 
       bytes = mmInStream.read(buffer); 
       // send the obtained bytes to the message handler 
       bluetoothHandler.obtainMessage(/*MESSAGE_READ*/1, bytes, -1, buffer).sendToTarget(); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    /** Cancels an in-progress connection, and closes the socket */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { } 
    } 
} 

的问题是,在试图使用内mmSocket.connect()几秒钟调用块run()连接到硬件然后引发与描述java.io.IOException: Connection reset by peer异常。我在下面粘贴了相关的LogCat输出,并附上了相关的线路供您参考。

05-07 13:29:08.675: INFO/ActivityThread(1370): Receiving broadcast android.bleutooth.device.action.UUID seq=-1 to [email protected]38 
05-07 13:29:08.675: ERROR/ActivityThread(1370): start dispatch OnReceive message,mRegistered=true mCurOrdered=false intent=Intent { act=android.bleutooth.device.action.UUID (has extras) } receiver = [email protected]98 
05-07 13:29:08.675: VERBOSE/BluetoothEventRedirector(1370): Received android.bleutooth.device.action.UUID 
05-07 13:29:08.675: ERROR/xubo(1370): mBroadcastReceiver receive msg = android.bleutooth.device.action.UUID 
05-07 13:29:08.685: ERROR/ActivityThread(1370): exit dispatch OnReceive message,mRegistered=true mCurOrdered=false 
05-07 13:29:15.455: DEBUG/An exception occurred during bluetooth socket connection(9827): NDB 
05-07 13:29:15.455: DEBUG/An exception occurred during bluetooth socket connection(9827): java.io.IOException: Connection reset by peer 
05-07 13:29:15.455: DEBUG/An exception occurred during bluetooth socket connection(9827):  at android.bluetooth.BluetoothSocket.connectNative(Native Method) 
05-07 13:29:15.455: DEBUG/An exception occurred during bluetooth socket connection(9827):  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:204) 
05-07 13:29:15.455: DEBUG/An exception occurred during bluetooth socket connection(9827):  at com.ndb.proj.Communicator$CommunicationThread.run(Communicator.java:157) 
05-07 13:29:18.595: DEBUG/bluez/src/device.c(8031): /org/bluez/8031/hci0/dev_00_06_66_05_70_E7: canceling authentication request 
05-07 13:29:18.595: ERROR/BluetoothEventLoop.cpp(1204): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/8031/hci0/dev_00_06_66_05_70_E7 
05-07 13:29:18.595: DEBUG/BluetoothEventLoop(1204): Device property changed:00:06:66:05:70:E7property:Connected 
05-07 13:29:18.605: INFO/ActivityThread(1204): Receiving broadcast android.bluetooth.device.action.ACL_DISCONNECTED seq=-1 to [email protected]18 
05-07 13:29:18.605: ERROR/ActivityThread(1204): start dispatch OnReceive message,mRegistered=true mCurOrdered=false intent=Intent { act=android.bluetooth.device.action.ACL_DISCONNECTED (has extras) } receiver = [email protected] 
05-07 13:29:18.615: ERROR/ActivityThread(1204): exit dispatch OnReceive message,mRegistered=true mCurOrdered=false 
05-07 13:29:18.615: INFO/ActivityThread(1370): Receiving broadcast android.bluetooth.device.action.PAIRING_CANCEL seq=-1 to [email protected]38 
05-07 13:29:18.625: ERROR/ActivityThread(1370): start dispatch OnReceive message,mRegistered=true mCurOrdered=false intent=Intent { act=android.bluetooth.device.action.PAIRING_CANCEL } receiver = [email protected]98 
05-07 13:29:18.625: VERBOSE/BluetoothEventRedirector(1370): Received android.bluetooth.device.action.PAIRING_CANCEL 
05-07 13:29:18.625: ERROR/xubo(1370): mBroadcastReceiver receive msg = android.bluetooth.device.action.PAIRING_CANCEL 
05-07 13:29:18.625: ERROR/ActivityThread(1370): exit dispatch OnReceive message,mRegistered=true mCurOrdered=false 

客户已通知'配对代码'是1234.但是,我的android手机与设备配对没有指定此。我已经通过进入蓝牙设置进行了验证,并且设备实际上报告为已配对。

任何人都可以建议发生了什么问题吗?

回答

2

所以原来问题是没有输入配对代码(杜!)。我用编程方式与设备配对,该设备没有请求任何配对代码输入,因此配对不合适。

定盘进入Settings -> Wireless & networks -> Bluetooth settings,长按“配对”设备并选择Unpair,然后该产生的Bluetooth pairing request窗口与文本框,在其中我键入的PIN(配对代码)在设备上的单压。

完成此操作后,配对成功,上述代码完美工作。

2

该死,我正要发布一个答案,我看你有固定的吧:)

的关键是在这条线:

05-07 13:29:18.595: DEBUG/bluez/src/device.c(8031): /org/bluez/8031/hci0/dev_00_06_66_05_70_E7: canceling authentication request 

这表明蓝牙配对需要发生,但没有发生。

+0

感谢您的信息;-) – KomodoDave 2011-05-20 21:12:20