2010-10-13 194 views
5

我想創建一個簡單的程序,掃描藍牙耳機(我正在測試一個PS3耳機),然後連接到它。我正在使用bluetooth chat program example中的代碼。但是我無法將它連接到任何東西。當它到達connect()時它給我一個I/O異常。連接/配對藍牙耳機和Android

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

此線程在嘗試與設備建立傳出連接時運行。它貫穿始終;連接成功或失敗。

private class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 

    public ConnectThread(BluetoothDevice device) { 
     mmDevice = device; 
     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket for a connection with the given BluetoothDevice 
     try { 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { 
      Log.e(TAG, "create() failed", e); 
     } 
     if (tmp == null){ 
      Log.i(TAG, "tmp is NULL"); 
     } 
     mmSocket = tmp; 
    } 

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

     // Always cancel discovery because it will slow down a connection 
     mAdapter.cancelDiscovery(); 
     Log.i(TAG, "discovery is cancelled"); 

     // Make a connection to the BluetoothSocket 
     try { 
      // This is a blocking call and will only return on a successful connection or an exception 
      mmSocket.connect(); //Doesn't like something here??? 
     } catch (IOException e) { 
      Log.e(TAG, "IO error, ", e); 
      connectionFailed(); 
      // Close the socket 
      try { 
       mmSocket.close(); 
      } catch (IOException e2) { 
       Log.e(TAG, "unable to close() socket during connection failure", e2); 
      } 
      // Start the service over to restart listening mode 
      newTBluetoothService.this.start(); 
      return; 
     } 

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

     // Start the connected thread 
     connected(mmSocket, mmDevice); 
    } 

    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
      Log.e(TAG, "close() of connect socket failed", e); 
     } 
    } 
} 

這裏是logcat的:


10-13 16:07:45.544: ERROR/BluetoothService.cpp(1259): 
stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 

10-13 16:07:45.544: DEBUG/newBluetoothService(2821): connect to: 00:22:A6:07:12:2B 

10-13 16:07:45.544: DEBUG/newBluetoothService(2821): setState() 0 -> 2 

10-13 16:07:45.552: INFO/newBluetoothService(2821): BEGIN mConnectThread 

10-13 16:07:45.567: ERROR/BluetoothService.cpp(1259): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 

10-13 16:07:45.567: INFO/newBluetoothService(2821): discovery is cancelled 

10-13 16:07:45.575: ERROR/BluetoothEventLoop.cpp(1259): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Device already exists) 

10-13 16:07:45.583: INFO/BluetoothNew(2821): MESSAGE_STATE_CHANGE: 2 

10-13 16:07:46.700: ERROR/BluetoothEventLoop.cpp(1259): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/2872/hci0/dev_00_22_A6_07_12_2B 

10-13 16:07:47.036: DEBUG/BluetoothService(1259): updateDeviceServiceChannelCache(00:22:A6:07:12:2B) 

10-13 16:07:47.059: DEBUG/BluetoothService(1259):  uuid(system): 0000111e-0000-1000-8000-00805f9b34fb 1 

10-13 16:07:47.067: DEBUG/BluetoothService(1259):  uuid(system): 00001108-0000-1000-8000-00805f9b34fb 2 

10-13 16:07:47.075: VERBOSE/BluetoothEventRedirector(1786): Received android.bleutooth.device.action.UUID 

10-13 16:07:47.075: DEBUG/BluetoothService(1259): Cleaning up failed UUID channel lookup: 00:22:A6:07:12:2B 00001101-0000-1000-8000-00805f9b34fb 

10-13 16:07:47.083: ERROR/newBluetoothService(2821): IO error, 

10-13 16:07:47.083: ERROR/newBluetoothService(2821): java.io.IOException: Service discovery failed 

10-13 16:07:47.083: ERROR/newBluetoothService(2821):  at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:377) 

10-13 16:07:47.083: ERROR/newBluetoothService(2821):  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:201) 

10-13 16:07:47.083: ERROR/newBluetoothService(2821):  at com.nmtransfer.bluetooth.newBluetoothService$ConnectThread.run(newBluetoothService.java:347) 

10-13 16:07:47.083: INFO/newBluetoothService(2821): Connection Failed 

10-13 16:07:47.083: DEBUG/newBluetoothService(2821): setState() 2 -> 1 

10-13 16:07:47.083: DEBUG/newBluetoothService(2821): start 

10-13 16:07:47.083: INFO/BluetoothNew(2821): MESSAGE_STATE_CHANGE: 1 

10-13 16:07:47.098: DEBUG/BluetoothService(1259): new handle 1000d 

10-13 16:07:47.106: DEBUG/newBluetoothService(2821): setState() 1 -> 1 

10-13 16:07:47.106: DEBUG/newBluetoothService(2821): BEGIN mAcceptThreadThread[Thread- 
13,5,main] 

10-13 16:07:47.114: INFO/BluetoothNew(2821): MESSAGE_STATE_CHANGE: 1 

10-13 16:07:51.036: ERROR/BluetoothEventLoop.cpp(1259): event_filter: Received signal org.bluez.Device:PropertyChanged from /org/bluez/2872/hci0/dev_00_22_A6_07_12_2B 

10-13 16:07:53.583: VERBOSE/BluetoothEventRedirector(1786): Received android.bleutooth.device.action.UUID 

任何幫助深表感謝。

謝謝 克里斯

+0

我正在使用2.2更新版和PS3藍牙耳機在Droid X上進行測試。它正在配對,但不會連接。當我進入藍牙設置時,我可以手動連接耳機。 – 2010-10-14 17:02:27

回答

1

我想你不應該指定一個特定的UUID作爲連接PARAM除非你知道這是你的耳機提供確切的GUID。 我不確定。但我希望它能幫助你。

祝你好運!

花式

2

@CHRIS嘿,我其實只是想通了這個問題。您用connectThread敲打頭部,但是您需要使用UUID(「00001108-0000-1000-8000-00805F9B34FB」)作爲耳機配置文件。我使用的是Jabra耳機,適用於我。讓我知道你是否有疑問! 謝謝!

+0

某些藍牙耳機可能不提供此配置文件UUID,儘管它是用於此目的的基本配置文件。但是,隨着車載套件使用的「更高」級別配置文件增加了一些有趣的功能(重撥最後的號碼等),您應該首先嚐試使用UUID爲「0000111E-0000-1000-8000-00805F9B34FB」的HandsFreeProfile(HFP)。 – Schlangi 2013-07-17 11:56:12

1

我正在研究類似的問題以及... - 如何連接耳機配置文件的APP。 然後,我在這裏看到了這樣的回答: https://stackoverflow.com/a/5711736/769556

基本上這個耳機的水平輪廓連接是通過 的Android內部完成(有沒有應用級訪問建立 連接)

左右。 .. @ Chris Mattmiller你有沒有找到不同的答案?