2011-04-30 59 views
0

我正在嘗試與藍牙設備進行通信。我在設備上的信息表明:藍牙ASCII協議

「通信協議是ASCII,逗號分隔輸出值。消息以回車符和換行符結束。當使用終端仿真器保存爲文件時,這些結果可以是讀入Excel電子表格。「

如何發送和接收此設備?我曾嘗試使用InputStreamReader和OutputStreamWriter,但我不認爲這是工作。

編輯:

用於發送數據我想:

public void send(String s){ 
      try { 
       writer.write(s); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

其中

try { 
       tmpIn = socket.getInputStream(); 
       tmpOut = socket.getOutputStream(); 
      } catch (IOException e) { } 

      inStream = tmpIn; 
      writer = new OutputStreamWriter(tmpOut); 

您還可以看到那裏有我使用inStream中這是一個簡單的InputStream。我也嘗試過InputStreamReader,但我只是隨機獲得了一些字符。使用InputStream,無論發送什麼設備,我都只能讀取4個字節,所以我不確定發送是否正常。

我應該使用什麼?謝謝!

+0

請詳細說明。你嘗試過哪些代碼,以及遇到什麼問題?包含任何結果或錯誤消息。 – 2011-04-30 02:26:05

+0

我在我的問題中增加了更多細節,感謝您的關注! – Matt 2011-04-30 02:40:12

+0

如果存在允許指定編碼的編碼,則不要使用無編碼構造函數或方法。如果你沒有指定編碼,你將得到平臺默認編碼,這實質上是「隨機編碼」的委婉說法,導致依賴平臺的代碼。如果你確實是指ASCII碼(只有7位),你應該指定它:'新的OutputStreamWriter(tmpOut,「ASCII」)',但我懷疑它實際上是'ISO-8859-1'或其他一些8位編碼。 – 2011-05-03 15:33:06

回答

0

我正在跟進此情況,以防其他人遇到同樣的問題。我遇到的其中一個問題是,我試圖與之通信的設備期望特定順序的/ n和/ r,並且如果該錯誤是鎖定的,我不知道它是否正常工作。

這裏是他用於發送和接收的代碼,現在我已經在幾個設備上使用它,它似乎很好用。

/** 
* This thread runs during a connection with a remote device. 
* It handles all incoming and outgoing transmissions. 
*/ 
private class ConnectedThread extends Thread { 
    private final BluetoothSocket socket; 
    private final InputStream inStream; 
    private final OutputStream outStream; 
    private final DataInputStream datIn; 

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "create ConnectedThread"); 
     this.socket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the BluetoothSocket input and output streams 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "temp sockets not created", e); 
     } 

     inStream = tmpIn; 
     outStream = tmpOut; 
     datIn = new DataInputStream(inStream); 
    } 

    public void run() { 
     Log.i(TAG, "BEGIN ConnectedThread"); 
     Bundle data = new Bundle(); 

     // Keep listening to the InputStream while connected 
     while (true) { 
      Log.i(TAG, "Reading..."); 
      try { 
       // Read from the InputStream 
       String results; 
       Log.i(TAG, "Recieved:"); 
       results = datIn.readLine(); 
       Log.i(TAG, results); 

      // Send the obtained bytes to the UI Activity 
       data.putString("results", results); 
       Message m = handler.obtainMessage(); // get a new message from the handler 
       m.setData(data); // add the data to the message 
       m.what = MESSAGE_READ; 
       handler.sendMessage(m); 
      } catch (IOException e) { 
       Log.e(TAG, "disconnected", e); 
       handler.obtainMessage(MESSAGE_DISCONNECTED).sendToTarget(); 
       setState(STATE_NONE); 
       // Start the service over to restart listening mode 
       break; 
      } 
     } 
    } 

    /** 
    * Write to the connected OutStream. 
    * @param buffer The bytes to write 
    */ 
    public void write(byte[] buffer) { 
     try { 
      outStream.write(buffer); 
      Log.i(TAG, "Sending: " + new String(buffer)); 

     } catch (IOException e) { 
      Log.e(TAG, "Exception during write", e); 
     } 
    } 

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

/** 
* Write to the ConnectedThread in an unsynchronized manner 
* @param out The bytes to write 
* @see ConnectedThread#write(byte[]) 
*/ 
public void send(byte[] out) { 
    // Create temporary object 
    ConnectedThread r; 
    // Synchronize a copy of the ConnectedThread 
    synchronized (this) { 
     if (state != STATE_CONNECTED) return; 
     r = connectedThread; 
    } 
    // Perform the write unsynchronized 
    r.write(out); 
} 
0

您應該查看IO Streams上的Java文檔以製作完整的照片。

對於檢索,我假設您正在使用InputStream.read()方法,該方法一次讀取一個字節。要一次檢索幾個字節,您應該使用byte[]緩衝區。但那不是你的情況,只是FYI。

你的情況,你不需要使用InputStream方法,但InputStreamReader相反,因爲 Reader上的字符,而不是字節進行操作。正如您在協議描述的引用中所述,您有單獨的ASCII行。在這種情況下BufferedReader是方便的,因爲它有readLine()方法。

所以你可以

in = socket.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(in); 
    BufferedReader br = new BufferedReader(isr); 

然後

String line = br.readLine(); 

用於發送數據,你應該使用OutputStreamWriter。

請記住:請在使用後關閉流媒體!在finaly{}子句