我正在处理蓝牙 rfcomm 连接。Android Sample 中有一行我无法理解,不幸的是我在其他问题和资源中找不到好的答案。
这是整个代码:
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
做什么的?
我认为,正如评论中提到的,它将接收到的字节发送到我设置为主要活动的活动。我可以Static TextView
在我的主 Activity 中创建一个而不是 sendToTarget() 来显示收到的消息吗?