2016-01-11 32 views
0

我試圖建立一個應用程序,使用rfcomm讀取藍牙服務上發送的信息。 該設備是一個硬度測試儀(HT-6510A),有關設備數據格式的不幸規格無法找到我面臨一個奇怪的問題,我必須瞭解如何閱讀這些信息。瞭解藍牙rfcoom原始數據

01-11 17:47:28.940 11862-13447/joinstore.it.testhardness V/result: ��S 
01-11 17:47:29.581 11862-13447/joinstore.it.testhardness V/result: ��S 
01-11 17:47:30.211 11862-13447/joinstore.it.testhardness V/result: ��S 
01-11 17:47:30.872 11862-13447/joinstore.it.testhardness V/result: ��S 
01-11 17:47:31.513 11862-13447/joinstore.it.testhardness V/result: ��S 
01-11 17:47:32.143 11862-13447/joinstore.it.testhardness V/result: ��T 
01-11 17:47:32.794 11862-13447/joinstore.it.testhardness V/result: ��T 

這是我從設備接收數據,我不認爲有什麼問題與實現塔只是利用這個線程穩定一個RFCOMM連接後。

//After connection, handle data transfer 
    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() { 
//   readAndPublishRaw(); 
      readAndPublishString(); 
      Log.v("result", "Reading data ended."); 
      setStatusText(-1); 
     } 

     void readAndPublishRaw(){ 
      byte[] buffer = new byte[1024]; // buffer store for the stream 
      int bytes; // bytes returned from read() 
      Log.v("result", "Start reading..."); 
      // 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 
        Log.v("result", bytes + ""); 
       } catch (IOException e) { 
        break; 
       } 
      } 
     } 

     void readAndPublishString(){ 
      //String method, not useful in this case? 
      try { 
       BufferedReader r = new BufferedReader(new InputStreamReader(mmInStream)); 
       StringBuilder total = new StringBuilder(); 
       String line; 
       Log.v("result", "Start reading..."); 
       while ((line = r.readLine()) != null) { 
        total.append(line); 
        Log.v("result", line); 
       } 
       Log.v("result", total.toString()); 
       //TODO publish read string to the view 
      } catch (Exception e) { 
       // 
       try { 
        mmSocket.close(); 
       }catch (Exception ex){} 
       Log.v(TAG, "exception reading data from service"); 
      } 
     } 

     /* 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(); 
       setStatusText(-1); 
      } catch (IOException e) { 
      } 
     } 
    } 

你們可以告訴我有關如何正確解析這些原始數據的任何信息嗎?我想我應該有一個float值的流,但相反,我只是這個隨機的東西。如何先得到一些可用的日誌輸出

+1

「關於設備數據格式的不確定規格無法找到」 - 您希望我們甚至在沒有透露設備是什麼的情況下幫助您? – JimmyB

+1

「我認爲我應該有一個浮動值的流」 - 很可能是這種情況。但是,你知道通過藍牙你只能得到*字節*的流。你必須看看那些*字節*來找出這些值是如何編碼的。簡單地將字節「轉換」爲字符串不會有任何幫助。至少給我們一些* raw *格式的示例消息,即字節;也包括每封郵件的*長度*! – JimmyB

+0

我用設備名更新了問題。現在我使用readAndPublishRaw函數一個接一個地獲取字節,你知道一個更好的方法來獲取這些信息嗎? – Pievis

回答

2

建議:

void readAndPublishRaw(){ 
     byte[] buffer = new byte[1024]; // buffer store for the stream 
     int bytes; // bytes returned from read() 
     Log.v("result", "Start reading..."); 
     // 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 

       final StringBuilder sb = new StringBuilder(); 
       sb.append("Received ").append(bytes).append(" bytes: "); 
       for (int i = 0; i < bytes; i++) { 
       sb.append(Integer.toHexString(((int)buffer[i]) & 0xff)).append(", "); 
       } 

       Log.v("result", sb.toString()); 
      } catch (IOException e) { 
       break; 
      } 
     } 
    } 

下一步應校準數據,即記其輸入/顯示值產生什麼樣的原始數據。從那裏,你可能或不可以推斷出實際的編碼。

您的設備可能包含或可能不包含除數據實際測量值外的其他信息,例如,表示電量不足。這將不得不被分解以獲得原始測量值。

如果一個數據值包含多個字節,則需要確定字節順序(小或大端)。

小設備的浮點數據通常以固定點表示形式表示。有時候,特別是。如果需要負數也與一個偏移量增加,從而使

realValue = rawValue * a + c

如果您發現ac你很好。因此,一旦您只能將兩個不同的realValue和相應的rawValue與上述校準聯繫起來,您就有足夠的數據來解決ac的等式。

具有更多「衝牀」的設備,例如嵌入式Linux設備,也可以使用常規的IEEE floating point data。 - 不是說小的嵌入式設備不能使用IEEE,但浮點數通常超過了所需的數值,而且浮點仿真代碼的複雜度較高(CPU的內存爲&)。