2015-06-15 86 views
0

我正在創建一個Android應用程序,使用它我將通過藍牙連接到樹莓派。 我能夠發送數據到Raspberry Pi的問題,並且它在終端上可見(我在Android中使用OutputStream發送數據),但無論Raspberry pi發送什麼,我都無法在我的InputStream中獲取它。通過藍牙在android中從樹莓派接收數據

我已閱讀有關使用listenrfcomm來獲取其他設備發送的數據,但同時使用createrfcomm,我也輸入以及輸出流。我很困惑什麼使用和如何使用。

注意:使用createrfcomm我能夠成功地將數據發送到Raspberry pi。只有來自Rasperry pi的數據接收纔是剩餘的部分。

請相應通知。

回答

1

它會更容易與您的代碼明確回答,但我發現API指南例如有用雖然略顯雜亂起初:

具有螺紋連接:

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

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

     // Get a BluetoothSocket to connect with the given BluetoothDevice 
     try { 
      // MY_UUID is the app's UUID string, also used by the server code 
      tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 
     } catch (IOException e) { } 
     mmSocket = tmp; 
    } 

    public void run() { 
     // Cancel discovery because it will slow down the connection 
     mBluetoothAdapter.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) { } 
      return; 
     } 

     // Do work to manage the connection (in a separate thread) 
     manageConnectedSocket(mmSocket); 
    } 

    /** Will cancel an in-progress connection, and close the socket */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { } 
    } 

}

和一個線程來監聽和完成工作:

private class ConnectedThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the input and output streams, using temp objects because 
     // member streams are final 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer); 
       // Send the obtained bytes to the UI activity 
       mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer) 
         .sendToTarget(); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

    /* Call this from the main activity to send data to the remote device */ 
    public void write(byte[] bytes) { 
     try { 
      mmOutStream.write(bytes); 
     } catch (IOException e) { } 
    } 

    /* Call this from the main activity to shutdown the connection */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { } 
    } 

}

我認爲,如果你可以發送你有BluetoothDevice類和BluetoothAdapter已經,並且可以創建並運行連接線程

mConnectThread = new ConnectThread(bluetoothAdapter.getRemoteDevice(deviceAddress)); 
mConnectThread.start(); 

在這個例子中字節的數據讀取,被髮送到UI線程與mHandler.obtainMessage。這行可以編輯,以適應你想要處理的數據。

例子來自http://developer.android.com/guide/topics/connectivity/bluetooth.html

+0

在你所提到的,我們使用createRfcommSocketToServiceRecord()的「ConnectThread」,但如果我有收到來自其他設備發送的數據,是否有必要,我必須使用listenRfcomm?我很困惑如何放置線程的執行,以便聽取以及發送任何消息。 –

+0

我剛剛更新了答案,希望能夠增加一些清晰度。讓我知道,如果它不清除事情。 – Nathan

+0

我的代碼中有ConnectThread和ConnectedThread,我已經提到我能夠通過藍牙從我的設備成功發送數據到Raspberry Pi。我只需要問,如果樹莓派正在發送一些數據,我如何通過藍牙讀取設備中的數據......? ConnectedThread mmInStream.read()是否會做這個部分,或者我需要設置一些'listenrfcomm'? 我希望我明白我現在向你要求的東西。 –

相關問題