2016-06-28 85 views
0

在我正在开发的程序中,我有两个设备通过蓝牙相互通话。当应用程序第一次启动时,两个设备连接正常,但是当连接被切断并重新初始化(即服务器线程被终止然后重新创建)时,尽管BluetoothServerSocket已经接受连接,但它仍然失败。在服务器端,我有蓝牙ServerSocket不接受

void handleConnection() throws Exception { 
    bss = BluetoothAdapter.getDefaultAdapter().listenUsingRfcommWithServiceRecord(Constants.BITMAP_NAME, 
         UUID.fromString(Constants.BITMAP_UUID)); 
    running.set(true); 
    Logger.logE("accepting"); 
    BluetoothSocket socket = bss.accept(); 
    Logger.logE("accepted"); 
    context.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      if (connectionCallback != null) 
       connectionCallback.onConnectionMade(); 
     } 
    }); 
    //handle connection in a loop... 
    try { 
     is.close(); 
     is = null; 
    } catch (IOException ignored) { 
     Logger.exception(ignored); 
    } 
    try { 
     socket.close(); 
     socket = null; 
    } catch (IOException ignored) { 
     Logger.exception(ignored); 
    } 
} 

而且在客户端上我有

void handleConnection() throws Exception { 
    BluetoothSocket socket = connect. 
      createRfcommSocketToServiceRecord(UUID.fromString(Constants.BITMAP_UUID)); 
    Logger.logE("preparing to connect"); 
    socket.connect(); 
    Logger.logE("Connected"); 
    if (connectionCallback != null) { 
     context.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       connectionCallback.onConnectionMade(); 
      } 
     }); 
     Logger.logE("callback fired"); 
    } 
    //Handle the connection... 
    Logger.logE("closing client"); 
    try { 
     os.close(); 
     os = null; 
    } catch (Exception ex) { 
     Logger.exception(ex); 
    } 
    try { 
     socket.close(); 
     socket = null; 
    } catch (Exception ex) { 
     Logger.exception(ex); 
    } 
} 

重新启动服务器,并尝试重新连接,客户端超时与消息

java.io.IOException: read failed, socket might closed or timeout, read ret: -1 

后在socket.connect();行。在监视客户端时,我在Logcat中看到消息preparing to connect,而在监视服务器时看到消息accepting,但它从未超出这一点。

+0

您能否详细说明“何时连接被切断并重新初始化”以及在哪个语句抛出错误(java.io.IOException:读取失败,套接字可能关闭或超时,读取ret:-1)? – solosodium

+0

@solosodium为你更新了它。 – sjyn

回答

1

客户端代码似乎很好。我认为问题来自服务器。

如果你去到Android documentation蓝牙,你会看到蓝牙服务器的示例:

private class AcceptThread extends Thread { 
    private final BluetoothServerSocket mmServerSocket; 

    public AcceptThread() { 
     // Use a temporary object that is later assigned to mmServerSocket, 
     // because mmServerSocket is final 
     BluetoothServerSocket tmp = null; 
     try { 
      // MY_UUID is the app's UUID string, also used by the client code 
      tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); 
     } catch (IOException e) { } 
     mmServerSocket = tmp; 
    } 

    public void run() { 
     BluetoothSocket socket = null; 
     // Keep listening until exception occurs or a socket is returned 
     while (true) { 
      try { 
       socket = mmServerSocket.accept(); 
      } catch (IOException e) { 
       break; 
      } 
      // If a connection was accepted 
      if (socket != null) { 
       // Do work to manage the connection (in a separate thread) 
       manageConnectedSocket(socket); 
       mmServerSocket.close(); 
       break; 
      } 
     } 
    } 

    /** Will cancel the listening socket, and cause the thread to finish */ 
    public void cancel() { 
     try { 
      mmServerSocket.close(); 
     } catch (IOException e) { } 
    } 
} 

问题的关键是,你需要运行在一个线程的服务器,以保持所有传入连接的轨道。因为您需要传入的插座进行通信。

当你完成通信时记住关闭SOCKET也很重要。没有正确关闭的套接字可能会导致硬件连接悬停,而软件层不知道,这也可能是您当前问题的原因(由于缺乏实际通信的代码,这是不确定的)。有关更多信息,请参阅this

+0

你是100%的权利。我相信该插座没有正确关闭,这与您所描述的确实有关。 – sjyn