2011-05-27 85 views
2

誰能告訴我這個問題是什麼?我試圖讓我的應用程序啓動一個Accept Thread,但是這個錯誤不斷髮生。Bindlisten在啓動Accept線程時失敗

任何人都可以幫忙嗎?

我使用Android 2.1版HTC Hero。

ERROR/BluetoothChatService(9585): java.io.IOException: Bindlisten failed 

android.bluetooth.BluetoothAdapter.listenUsingRfcommWithServiceRecord(BluetoothAdapter.java:749) 

根據here

public BluetoothServerSocket listenUsingRfcommWithServiceRecord (String name, UUID uuid) 

Throws IOException on error, for example Bluetooth not available, or insufficient permissions, or channel in use. 

這裏的一些代碼: 我已經使用BluetoothChat.java爲指導,制定以下代碼參考。所以一旦啓用藍牙,用戶將按下菜單按鈕,然後出現三個按鈕。用戶選擇server按鈕,如果他們想要啓動接受線程併成爲遊戲的主機。

public boolean onOptionsItemSelected(MenuItem item) { 
    Intent serverIntent = null; 
    switch (item.getItemId()) { 
    case R.id.scan: 
     // Launch the DeviceListActivity to see devices and do scan 
     serverIntent = new Intent(this, DeviceListActivity.class); 
     startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_SECURE); 
     return true; 
    case R.id.discoverable: 
     // Ensure this device is discoverable by others 
     ensureDiscoverable(); 
     return true; 
    case R.id.server: 
     // Ensure this device is discoverable by others 
     if (mGameService != null) 
     { 
      // Only if the state is STATE_NONE, do we know that we haven't started already 
      if (mGameService.getState() == BluetoothChatService.STATE_NONE) 
      { 
       // Start the Bluetooth chat services 
       mGameService.start(); 
      } 
     } 
     return true; 
    } 
    return false; 
} 

一旦用戶選擇的Server按鈕,就會調用下面的方法從另一個類:

public synchronized void start() { 
    if (D) Log.d(TAG, "start"); 

    // Cancel any thread attempting to make a connection 
    if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;} 

    // Cancel any thread currently running a connection 
    if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;} 

    setState(STATE_LISTEN); 

    // Start the thread to listen on a BluetoothServerSocket 
    if (mSecureAcceptThread == null) { 
     mSecureAcceptThread = new AcceptThread(); 
     mSecureAcceptThread.start(); 
    } 
} 

然後這是Accept Thread。使用調試器,我可以確認執行了Accept Thread。然而,它未能在第一try catch,它永遠不會執行該

tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID); 

接受以下主題:

private class AcceptThread extends Thread { 
    // The local server socket 
    private final BluetoothServerSocket mmServerSocket; 

    public AcceptThread() { 
     BluetoothServerSocket tmp = null; 

     // Create a new list ening server socket 
     try { 
       tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME,MY_UUID); 
      } 
     catch (IOException e) { 
      Log.e(TAG, "Socket Type: " + "listen() failed", e); 
     } 
     mmServerSocket = tmp; 
    } 

    public void run() { 
     if (D) Log.d(TAG, "Socket Type: " + 
       "BEGIN mAcceptThread" + this); 
     setName("AcceptThread"); 

     BluetoothSocket socket = null; 

     // Listen to the server socket if we're not connected 
     while (mState != STATE_CONNECTED) { 
      try { 
       // This is a blocking call and will only return on a 
       // successful connection or an exception 
       socket = mmServerSocket.accept(); 
      } catch (IOException e) { 
       Log.e(TAG, "Socket Type: " + "accept() failed", e); 
       break; 
      } 

      // If a connection was accepted 
      if (socket != null) { 
       synchronized (BluetoothChatService.this) { 
        switch (mState) { 
        case STATE_LISTEN: 
        case STATE_CONNECTING: 
         // Situation normal. Start the connected thread. 
         connected(socket, socket.getRemoteDevice()); 
         break; 
        case STATE_NONE: 
        case STATE_CONNECTED: 
         // Either not ready or already connected. Terminate new socket. 
         try { 
          socket.close(); 
         } catch (IOException e) { 
          Log.e(TAG, "Could not close unwanted socket", e); 
         } 
         break; 
        } 
       } 
      } 
     } 
     if (D) Log.i(TAG, "END mAcceptThread, socket Type: "); 

    } 

我只是做了一些調試,發生在Accept Thread以下錯誤。在socket = mmServerSocket.accept();一行。

[2011-05-27 13:52:21 - DeviceMonitor]Adb connection Error:An existing connection was forcibly closed by the remote host 
[2011-05-27 13:52:21 - Logcat]An existing connection was forcibly closed by the remote host 
java.io.IOException: An existing connection was forcibly closed by the remote host 
    at sun.nio.ch.SocketDispatcher.read0(Native Method) 
    at sun.nio.ch.SocketDispatcher.read(Unknown Source) 
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) 
    at sun.nio.ch.IOUtil.read(Unknown Source) 
    at sun.nio.ch.SocketChannelImpl.read(Unknown Source) 
    at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:395) 
    at com.android.ddmlib.Device.executeShellCommand(Device.java:284) 
    at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:527) 

[2011-05-27 13:52:23 - DeviceMonitor]Connection attempts: 1 
[2011-05-27 13:52:25 - DeviceMonitor]Connection attempts: 2 
[2011-05-27 13:52:27 - DeviceMonitor]Connection attempts: 3 
[2011-05-27 14:33:48 - Device]Error during Sync: timeout. 
[2011-05-27 16:49:43 - ddms]null 
java.lang.NullPointerException 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:572) 
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142) 
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65) 
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671) 
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317) 
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263) 

[2011-05-27 16:49:43 - ddms]null 
java.lang.NullPointerException 
    at com.android.ddmlib.Client.sendAndConsume(Client.java:572) 
    at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142) 
    at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65) 
    at com.android.ddmlib.Client.getJdwpPacket(Client.java:671) 
    at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317) 
    at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263) 
+0

部分代碼會很好... – 2011-05-27 13:49:18

+0

已添加一些代碼 – Navigatron 2011-05-27 14:04:24

+0

我發現另一個Stack Overflow問題,但被刪除。這傢伙有我遇到的同樣的問題。 [Question Link](http://www.androidportal.info/q23716/Bindlisten-Failed-Exception-while-connecting-two-android-device) 答案問他是否先創建了'RFCOMM'套接字,然而isn '客戶端創建的'RFCOMM'套接字,而不是服務器設備? – Navigatron 2011-05-27 14:37:46

回答

2

此代碼不適用於HTC Hero。 我已經在其他設備(如Samsung)上測試過它,並且發現不會發生同樣的錯誤。