2012-03-29 34 views
8

Tôi đang đối mặt với cùng một vấn đề như trong câu hỏi trước (và chưa được trả lời), tôi đã do dự khi chỉnh sửa, vì tôi muốn thêm nhiều hơn thông tin. Vì vậy, đây là câu hỏi liên quan: Android bluetooth connection fails (Error: no bt sock found, scn 1).Lỗi kết nối bluetooth Android (không tìm thấy bít bt, scn 1) - vòng lặp vô hạn

Lỗi xảy ra khi cố gắng thực hiện kết nối Bluetooth. Tôi không luôn luôn xảy ra (tại sao?), Nhưng nó là khá thường xuyên. Tôi đang viết ứng dụng này cho Android 2.3 với Eclipse Indigo.

Đây là nhật ký tôi nhận được khi làm kết nối trước khi có lỗi (nó có vẻ như kết nối thành công trước khi lỗi xảy ra):

03-29 15:14:52.205: D/BLZ20_ASOCKWRP(22027): asocket_connect 
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): blz20_wrp_connect: s 53 
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_connect: fd (-1:53), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0 
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): __connect_prot_rfcomm: connecting to... 0440201b0800, rc chan 1 
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): __connect_prot_rfcomm: non blocking mode 
03-29 15:14:52.205: I/BTL_IFC(22027): send_ctrl_msg: [BTL_IFC CTRL] send BTLIF_BTS_RFC_CON_REQ (BTS) 23 pbytes (hdl 52) 
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_connect: success 
03-29 15:14:52.205: I/BLZ20_WRAPPER(22027): blz20_wrp_poll: nfds 2, timeout -1 ms 
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): blz20_wrp_poll: pending connect fd (-1:53), bta -1, rc 1, wflags 0x0, cflags 0x1, port 0 
03-29 15:14:52.205: D/BLZ20_WRAPPER(22027): btlif_wait_response: id(s) |BTLIF_BTS_RFC_CON_RSP|BTLIF_BTS_RFC_DISC_IND| 
03-29 15:14:52.920: I/BTL_IFC(22027): btl_ifc_ctrl_rx: [BTL_IFC CTRL] recv BTLIF_BTS_RFC_CON_RSP (BTS) 22 pbytes (hdl 52) 
03-29 15:14:52.920: I/BLZ20_WRAPPER(22027): btlif_ctrl_callback: btlif_ctrl_callback : msg id BTLIF_BTS_RFC_CON_RSP 
03-29 15:14:52.920: E/BLZ20_WRAPPER(22027): ##### ERROR : btlif_process_con_rsp: no bt sock found, scn 1##### 

Và đây là nhật ký tôi nhận được khi kết nối chỉ thất bại (nhưng không có lỗi)

03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_abort [44,45,46] 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: s 44, how 2 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: shutdown socket 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_write: wrote 1 bytes out of 1 on fd 46 
03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_destroy 
03-29 15:20:37.100: D/BLZ20_ASOCKWRP(22027): asocket_abort [44,45,46] 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: s 44, how 2 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_shutdown: shutdown socket 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_write: wrote 1 bytes out of 1 on fd 46 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 46 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close: std close (46) 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 45 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close: std close (45) 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): blz20_wrp_close: s 44 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): blz20_wrp_close: fd (-1:44), bta -1, rc 0, wflags 0x0, cflags 0x0, port 0 
03-29 15:20:37.100: I/BLZ20_WRAPPER(22027): __close_prot_rfcomm: fd 44 
03-29 15:20:37.100: I/BTL_IFC(22027): send_ctrl_msg: [BTL_IFC CTRL] send BTLIF_BTS_RFC_CLOSE (BTS) 8 pbytes (hdl 52) 
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: wrp_close_s_only [44] (44:-1) [] 
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: data socket closed 
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wsactive_del: delete wsock 44 from active list [ad439320] 
03-29 15:20:37.100: D/BTL_IFC_WRP(22027): wrp_close_s_only: wsock fully closed, return to pool 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): btsk_dump_list: fd (-1:53), bta -1, rc 1, wflags 0x900, cflags 0x0, port 0 
03-29 15:20:37.100: D/BLZ20_WRAPPER(22027): btsk_free: success 

trong khi gỡ lỗi tôi đã tìm ra rằng ConnectThread sử dụng để tạo kết nối giữ chạy trong trường hợp lỗi, có nghĩa là nó đã bước vào một vòng lặp vô hạn. Do đó nếu một Thread khác cố gắng hủy và hoàn thành ConnectThread, ứng dụng này sẽ bị lỗi.

Xem ở đây mã nguồn của ConnectThread này mà là một lớp bên trong của một Service (rất giống với BluetoothChat mẫu):

/** 
* This thread runs while attempting to make an outgoing connection 
* with a device. It runs straight through; the connection either 
* succeeds or fails. 
*/ 
private class ConnectThread extends Thread 
{ 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 

    public ConnectThread(BluetoothDevice device) 
    { 
     mmDevice = device; 
     // Use a temporary object that is later assigned to mmSocket, 
     // because mmSocket is final 
     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket to connect with the given BluetoothDevice 
     try 
     { 
      // SerialPortServiceClass_UUID is the app's UUID string, also 
      // used by the server code 
      tmp = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID); 
     } catch (IOException e) 
     { 
      if (D) Log.e(TAG, "create RFCOMM failed", e); 
      // here I do something to notify the UI that the connection has failed 

      // Reset the ConnectThread because the connection has failed 
      synchronized (DataReceiverService.this) 
      { 
       mConnectThread = null; 
      } 
     } 
     mmSocket = tmp; 
    } 

    public void run() 
    { 
     if (D) Log.i(TAG, "BEGIN mConnectThread"); 
     setName("ConnectThread"); 

     // Cancel discovery because it will slow down the connection 
     mBtAdapter.cancelDiscovery(); 

     try 
     { 
      // Connect the device through the socket. This will block 
      // until it succeeds or throws an exception 
      mmSocket.connect(); 
     } catch (IOException connectException) 
     { 
      // Unable to connect, close the socket and get out 
      try { 
       mmSocket.close(); 
      } catch (IOException closeException) 
      { 
       if (D) Log.e(TAG, "unable to close() socket during connection failure", closeException); 
      } 
      // here I do something to notify the UI that the connection has fqiled 
      return; 
     } 

     // Reset the ConnectThread because we're done 
     synchronized (DataReceiverService.this) 
     { 
      mConnectThread = null; 
     } 

     // Do work to manage the connection (in the ConnectedThread) 
     connected(mmSocket, mmDevice); 
    } 

    /** Will cancel an in-progress connection, and close the socket */ 
    public void cancel() 
    { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) 
     { 
      if (D) Log.e(TAG, "close() of connect socket failed", e); 
     } 
    } 
} 
+0

Xảy ra hoàn toàn ngẫu nhiên cho tôi trên một chiếc Samsung GT-I9100 (Android 2.3) - tuy nhiên khi nó bắt đầu không thành công, tôi phải khởi động lại điện thoại của mình để nó hoạt động trở lại. Tôi tự hỏi cái quái gì mà "BLZ20_WRAPPER" là? Đó có phải là triển khai Bluetooth dành riêng cho Samsung không? – AgentKnopf

Trả lời

0

việc kiểm tra này http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html

BluetoothDevice

đại diện cho một điều khiển từ xa Thiết bị Bluetooth. Sử dụng tính năng này để yêu cầu kết nối với thiết bị từ xa thông qua BluetoothSocket hoặc thông tin truy vấn về thiết bị như tên, địa chỉ, lớp và trạng thái liên kết của thiết bị.

Các vấn đề liên quan