我試圖建立一個應用程序,使用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值的流,但相反,我只是這個隨機的東西。如何先得到一些可用的日誌輸出
「關於設備數據格式的不確定規格無法找到」 - 您希望我們甚至在沒有透露設備是什麼的情況下幫助您? – JimmyB
「我認爲我應該有一個浮動值的流」 - 很可能是這種情況。但是,你知道通過藍牙你只能得到*字節*的流。你必須看看那些*字節*來找出這些值是如何編碼的。簡單地將字節「轉換」爲字符串不會有任何幫助。至少給我們一些* raw *格式的示例消息,即字節;也包括每封郵件的*長度*! – JimmyB
我用設備名更新了問題。現在我使用readAndPublishRaw函數一個接一個地獲取字節,你知道一個更好的方法來獲取這些信息嗎? – Pievis