我正在使用Bluetooth rfcomm連接。 Android示例中有一行我無法理解,但不幸的是,我無法在其他問題和資源中找到好的答案。無法理解Android藍牙示例中的mHandler.obtainMessage()
這裏是整個代碼:
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) { }
}
我無法理解這行:
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
mHandler
未在此代碼中定義,也MESSAGE_READ
我不能瞭解bytes
做什麼?
我認爲,正如評論中提到的那樣,它將收到的字節發送給我設置爲我的主要活動的活動。我可以在主Activity中製作Static TextView
而不是sendToTarget()來顯示收到的消息嗎?
mHandler可能是實例變量,在你的類的某個地方聲明。 MESSAGE_READ可能是一個常量,在類中定義或通過靜態導入。 – assylias