2014-03-31 104 views
0

我正在編寫一個應用程序,它將接收來自另一個設備的串行數據(不是Android,如果它很重要,它將是Bluesmirf Silver模塊,但現在我只在我的筆記本電腦藍牙適配器上嘗試) 。這裏是我使用的代碼:Android:BluetoothSocket連接拋出IOException

public class ListenThread extends Thread { 
    private BluetoothSocket socket; 
    private InputStream istream; 
    private Handler handler; 
    private BluetoothAdapter adapter; 

    public ListenThread(BluetoothAdapter adapter, Handler handler) { 
     Log.v(TAG, "creating ListenThread"); 
     this.adapter = adapter; 
     this.handler = handler; 

     if (adapter.isDiscovering()) { 
      adapter.cancelDiscovery(); 
     } 
    } 

    public void run() { 
     if (running == false) { 
      // Thread is cancelled 
      Log.v(TAG, "run(): running == false"); 
      return; 
     } 

     if (adapter == null) { 
      // No BT adapter supplied 
      Log.v(TAG, "run(): adapter == null"); 
      return; 
     } 

     if (btDevice == null) { 
      // No btDevice is paired 
      Log.v(TAG, "run(): btDevice == null"); 
      return; 
     } 

     try { 

      socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));  
      adapter.cancelDiscovery(); 
      socket.connect(); 
      Log.v(TAG, "run(): socket connected"); 
     } catch (IOException e) { 
      Log.v(TAG, "run(): socket not connected"); 
      Log.v(TAG, "IOException: " + e.toString()); 
      e.printStackTrace(); 
      socket = null; 
     } 

     if (socket == null) { 
      // Connection to device failed 
      Log.v(TAG, "run(): socket is null"); 
      return; 
     } 

     InputStream tmpIn = null; 
     try { 
      tmpIn = socket.getInputStream(); 
     } catch (IOException e) { 
      // getInputStream always returns InputStream 
      // but operations will throw IOException until 
      // the stream is ready 
     } 
     istream = tmpIn; 

     StringBuffer sb = new StringBuffer(); 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from the read(); 
     String message; 
     int idx; 
     HashMap<String, String> hm; 
     String[] chunks; 

     Log.v(TAG, "run(): listening loop starting"); 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = istream.read(); 
       sb.append(new String(buffer, 0, bytes)); 
       while ((idx = sb.indexOf("\r\n\r\n")) > -1) { 
        message = sb.substring(0, idx); 
        sb.replace(0, idx + 4, ""); 
        hm = new HashMap<String, String>(); 
        for (String line : message.split("\n")) { 
         chunks = line.trim().split("=", 2); 
         if (chunks.length != 2) continue; 
         hm.put(chunks[0], chunks[1]); 
        } 
        handler.obtainMessage(0x2a, hm).sendToTarget(); 
       } 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

我總是在socket.connect()行中得到一個IOException。我試圖使該連接,他們的非工作的幾種方法,但每個投擲了不同IO例外:

方法1:

socket=BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress()).createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 

方法2:

socket = device.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 

方法3:

socket = device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 

我也試過這樣:

Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); 
socket = (BluetoothSocket) m.invoke(device, 1); 

我總是得到服務發現失敗或連接拒絕。這就是我如何管理ListenThread:

public void startListening() { 
    Log.v(TAG, "starting to listen"); 
    running = true;  
} 

public void stopListening() { 
    Log.v(TAG, "stoping listening"); 
    running = false; 
} 

我搜索谷歌,但所有我發現是連接插座的不同的方法,但正如我所說,他們沒有工作。任何想法?非常感謝!

編輯:

這是Log.v(TAG, 「IOException異常:」 + e.toString());和e.printStackTrace();打印:

IOException: java.io.IOException: Service discovery failed 
    java.io.IOException: Service discovery failed 
    W/System.err(8604):  at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:397) 
    W/System.err(8604):  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:207) 
    W/System.err(8604):  at com.ginger.kanarci_android.communication.BluetoothService$ListenThread.run(BluetoothService.java:133) 
+0

你可以發佈logcat文件嗎? – Tukajo

+0

IOException:java.io.IOException:服務發現失敗 java.io.IOException:服務發現失敗 W/System.err(8604):\t at android.bluetooth.BluetoothSocket $ SdpHelper.doSdp(BluetoothSocket.java:397) W/System.err(8604):\t at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:207) W/System.err(8604):\t at com.ginger.kanarci_android.communication.BluetoothService $ ListenThread。運行(BluetoothService.java:133) –

+0

使它成爲您的原始問題的編輯併發布完整的stacktrace(logcat):) – Tukajo

回答

1

所以我終於能夠獨佔這一點。唯一的問題是UUID。在我發佈的代碼中,我使用了衆所周知的SPP UUID,但筆記本電腦的藍牙適配器有不同的UUID,這就是爲什麼我認爲這兩者無法連接。

當我配置Bluesmirf模塊(默認具有SSP UUID)並試圖從應用程序連接時,它完美地工作。

+0

本週早些時候,我用en-41進行了同樣的嘗試。顯然他們有一個專用於android的特定UUID。即使在通過它的串口定製UUID之後,與我寫的android地址沒有修復它一樣。我不得不在他們的數據表中使用他們的建議。 – Iancovici

相關問題