6

我研究了android蓝牙示例。
我感到困惑的是,其他设备已经发送了连接请求,并且如果先前的连接存在,则服务器只是被阻塞了很长时间而没有被接受。

android 设备是否可以执行服务器套接字并接受许多连接而不仅仅是一个连接?是否有任何原因可能导致服务器被 accept() 阻塞,例如,不正确的 UUID 或类似的东西?

我做如下操作,

  1. 对于第一个连接,创建服务器套接字并等待连接
  2. 客户端 B 发送连接请求
  3. 服务器套接字接受
  4. 处理 i/o 流
  5. 使用同一个serversocket再次等待连接(我没有关闭serversocket)
  6. 客户端 C 发送连接请求
  7. 服务器套接字被阻塞,阻塞,阻塞,而不是接受......

因为第一次连接成功,第二次连接我没有更改UUID。所以我认为 UUID 不是关键。当第一个连接建立时,服务器套接字将等待另一个连接。可悲的是,它只是等待并被阻止,不接受作为第一个连接。

public void run() {
    if (D) Log.d(TAG, "BEGIN ServerSocketThread" + this);
    BluetoothSocket socket = null;
    while(true) {
        try {
            Log.i(TAG, "[ServerSocketThread] Enter while loop");
            socket = mmServerSocket.accept();                                     
            Log.i(TAG, "[ServerSocketThread] Got client socket");                    
        } catch (IOException e) {
            Log.e(TAG, "accept() failed", e);
            break;
        }

        if (socket!=null) {
            synchronized (BluetoothConnService.this) {                       
                Log.i(TAG, "[ServerSocketThread] "+socket.getRemoteDevice()+" is connected.");
                ConnectedThread tmpThread = new ConnectedThread(socket); 
                tmpThread.start();
                break;
            }
        }

    }   
    BluetoothConnService.this.startSession();
}

它是服务器套接字线程的一部分。每当我想建立第二个连接时,logcat 显示它将在日志“ [ServerSocketThread] Enter while loop ”之后停止。似乎操作在accept()上被阻止,但 serversocket 没有为mmServerSocket 抛出 IOException。接受()

如果我想建立第二个连接,我不知道为什么套接字既不被接受也不抛出异常,只是阻塞在那里。如果有任何可能的原因或任何建议,我将不胜感激。

这是我运行应用程序时的一些目录。我希望它有助于识别问题。

Part A : 连接成功,第一次连接

12-12 22:42:05.358: DEBUG/BluetoothSppPort(889): Creating a BluetoothSpp proxy object
12-12 22:42:05.378: DEBUG/BluetoothSppService(122): createPort called!
12-12 22:42:05.378: DEBUG/BluetoothSppService(122): createPort checking uuid
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort UUID=00001101-0000-1000-8000-00805f9b34fb auth=true encrypt=true
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort enforcing bluetooth perm
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort creating a jbtlspp object
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort checking if the btl spp object is valid
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort try to create an spp container
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort try to create security params
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort Set Security L2
12-12 22:42:05.388: DEBUG/BluetoothSppService(122): createPort spp port create
12-12 22:42:05.418: DEBUG/JBtlSpp(122): create: Entered
12-12 22:42:05.418: DEBUG/JBtlSpp(122): Calling NativeJBtlSpp_Create
12-12 22:42:05.418: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Entered
12-12 22:42:05.418: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Calling BTL_SPP_Remote_Create
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: BTL_SPP_Remote_Create returned 0, context:f
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Setting context value in jContext out parm
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create: Calling Java setValue(0xf) in context's class
12-12 22:42:05.598: DEBUG/JBtlProfileContext(122): setValue: setValue called, value:15
12-12 22:42:05.598: DEBUG/JBtlSppNative(122): create_spp_port_data: will use context struct 0 for the port 15
12-12 22:42:05.608: DEBUG/JBtlSppNative(122): create_spp_port_data: spp port context 0 added
12-12 22:42:05.608: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Create:Exiting Successfully
12-12 22:42:05.778: DEBUG/JBtlSpp(122): After NativeJBtlSpp_Create, status=SUCCESS, Context = 15
12-12 22:42:05.778: DEBUG/JBtlRbtlServices(122): addUser: Entered, userRefCount = 1
12-12 22:42:05.778: DEBUG/BluetoothSppService(122): port create returned status SUCCESS
12-12 22:42:05.778: DEBUG/JBtlSpp(122): enable: Entered
12-12 22:42:05.778: DEBUG/JBtlSpp(122): enable: UUID=00001101-0000-1000-8000-00805f9b34fb
12-12 22:42:05.778: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable: Entered
12-12 22:42:05.978: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable: BTL_SPP_Enable returned 0
12-12 22:42:05.978: DEBUG/JBtlSppNative(122): NativeJBtlSpp_Enable:Exiting
12-12 22:42:05.978: DEBUG/JBtlSpp(122): After NativeJBtlSpp_Enable, status=SUCCESS
12-12 22:42:05.978: DEBUG/JBtlSpp(122): enable: Exiting
12-12 22:42:05.978: DEBUG/BluetoothSppService(122): port enable returned status SUCCESS
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN_IND: Entered
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN_IND: Exiting
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: Entered
12-12 22:42:57.678: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: status: 0 context:15
12-12 22:42:57.708: DEBUG/JBtlSpp(122): nativeCb_open: Entered from 00:10:60:56:83:28
12-12 22:42:57.718: DEBUG/JBtlSpp(122): nativeCb_open: Calling callback
12-12 22:42:57.718: DEBUG/BluetoothSppService(122): connected called!
12-12 22:42:57.718: DEBUG/BluetoothSocket(889): SppPort connected()
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_OPEN: Exiting
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Entered
12-12 22:42:57.718: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Exiting

B 部分:这是第二个连接,它让服务器套接字被阻塞而不被接受。

看起来它没有像A部分那样运行BluetoothSppService。但是BluetoothSppService是andorid框架的底层,因此我不知道如何处理。

12-12 22:43:29.158: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Entered
12-12 22:43:29.168: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: context: 1, errCode: 0
12-12 22:43:29.168: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Calling Java Link Connect Indication callback
12-12 22:43:29.178: DEBUG/JBtlBmg(122): nativeLinkConnectInd
12-12 22:43:29.218: DEBUG/BluetoothService(122): Callback - linkConnectInd, btErrCode = NO_ERROR, bdAddr = C0:E4:22:18:ED:C1
12-12 22:43:29.258: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Entered
12-12 22:43:29.258: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Calling NativeJBtlBmg_GetKnownDeviceInfo
12-12 22:43:29.258: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo: Entered
12-12 22:43:29.258: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo: Calling BTL_BMG_GetKnownDeviceInfo
12-12 22:43:29.268: DEBUG/JBtlBmgJniKnownDeviceInfo(122): setValues: Entered
12-12 22:43:29.268: DEBUG/(122): NativeJBtlBmg_GetKnownDeviceInfo:Exiting
12-12 22:43:29.268: DEBUG/JBtlBmg(122): getKnownDeviceInfo: After NativeJBtlBmg_GetKnownDeviceInfo, status=SUCCESS
12-12 22:43:29.278: DEBUG/JBtlBmg(122): getKnownDeviceInfo: Exiting
12-12 22:43:29.278: DEBUG/BluetoothService(122): onRemoteDeviceConnected, device C0:E4:22:18:ED:C1 is Paired
12-12 22:43:29.278: DEBUG/BluetoothService(122): Sending ACTION_ACL_CONNECTED intent, address = C0:E4:22:18:ED:C1
12-12 22:43:29.278: DEBUG/BluetoothA2dpService(122): Received intent with action: android.bluetooth.device.action.ACL_CONNECTED
12-12 22:43:29.298: DEBUG/(122): signal_BTEVENT_LINK_CONNECT_IND: Exiting
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: Entered
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: context: 1, errCode: 19
12-12 22:43:29.988: DEBUG/(122): signal_BTEVENT_LINK_DISCONNECT: Calling Java Link Disconnect callback
12-12 22:43:29.988: DEBUG/JBtlBmg(122): nativeLinkDisconnect
12-12 22:43:29.998: DEBUG/BluetoothService(122): Callback - linkDisconnect, btErrCode = USER_TERMINATED, bdAddr = C0:E4:22:18:ED:C1, status = NO_ERROR
12-12 22:43:30.018: DEBUG/BluetoothService(122): Sending ACTION_ACL_DISCONNECTED intent, address = C0:E4:22:18:ED:C1

C部分:如果我断开之前存在的连接,它将按如下方式运行。然后运行A部分。这样我就可以再次建立一个conntion。

创建一个BluetoothSpp代理对象,并执行BluetoothSppService

12-12 22:45:45.758: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Entered
12-12 22:45:45.758: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_MODEM_STATUS_IND: Exiting
12-12 22:45:45.788: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSE_IND: Entered
12-12 22:45:45.788: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSE_IND: Exiting
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: Entered
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: status: 0 context:15
12-12 22:45:45.798: DEBUG/JBtlSpp(122): nativeCb_closed: Entered
12-12 22:45:45.798: DEBUG/BluetoothSppService(122): close called!
12-12 22:45:45.798: DEBUG/JBtlSppNative(122): signal_SPP_EVENT_CLOSED: Exiting
12-12 22:45:46.158: DEBUG/JBtlSppNative(122): NativeJBtlSpp_ReadNative:Exiting with 1

看起来logcat应该像A部分一样,可以建立连接。我怀疑服务器socket没有接受就被阻塞的原因是这些操作是由较低级别的android平台处理的。但我不知道如何获取或如何执行此服务,例如BluetoothSppServiceJBtlSppNativeJBtlBmg等。

你有这方面的经验吗?
如果是的话,如果您能与我分享,我将不胜感激。或者给我一些关于如何让设备可以接受另一个连接的提示或建议,如果之前的连接已经存在。

4

1 回答 1

1

抱歉,与 TCP 套接字不同,蓝牙欢迎服务器套接字只能接受一个客户端。

来自谷歌:

与 TCP/IP 不同,RFCOMM 一次只允许每个通道连接一个客户端,因此在大多数情况下,在接受连接的套接字后立即在 BluetoothServerSocket 上调用 close() 是有意义的。

于 2013-09-18T05:20:39.050 回答