如果Android蓝牙服务器套接字处理多个连接,它将在accept()上被阻止

时间:2010-12-13 03:03:19

标签: android bluetooth serversocket

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

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

我按如下方式进行操作,

  1. 首次连接时,创建服务器套接字并等待连接
  2. 客户端B发送连接请求
  3. 服务器套接字接受
  4. 处理i / o流
  5. 使用相同的serversocket再次等待连接(我不关闭serversocket)
  6. 客户端C发送连接请求
  7. 服务器套接字被阻止,阻止,阻止,而不是被接受......
  8. 因为第一次连接成功,我不会在第二次连接时更改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 while循环后停止。”似乎操作被阻止 accept(),但serversocket没有抛出 mmServerSocket.accept()的IOException。

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

    运行应用程序时,这是一些catlog。我希望找出问题是有帮助的。

    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部分:它是第二个连接,它允许在不接受的情况下阻止serversocket。

    看起来它没有像部分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部分。所以我可以再次建立一个连接。

    创建 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部分一样,可以建立连接。我怀疑服务器套接字被阻止而没有接受的原因是这些操作是由较低级别的android平台处理的。但我不知道如何获得或如何执行此服务,例如 BluetoothSppService JBtlSppNative JBtlBmg 等。

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

1 个答案:

答案 0 :(得分:1)

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

来自Google:

  

与TCP / IP不同,RFCOMM只允许每个通道连接一个客户端   一段时间,所以在大多数情况下,调用close()是有意义的   接受连接的套接字后立即蓝牙服务器。