基于SDK的蓝牙聊天示例,我正在开发一个在android设备和arduino之间传输字符串的应用程序。 我有以下问题:
1-如果我使用此代码,则会丢失arduino发送的第一个字节:
// Keep listening to the InputStream while connected
while (true) {
try {
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MESSAGE_READ, bytes,-1, buffer).sendToTarget();
但是这种方式有效:
bytes = mmInStream.available();
if(bytes != 0) {
SystemClock.sleep(100); //pause and wait for rest of data.
bytes = mmInStream.available(); // how many bytes are ready to be read?
bytes = mmInStream.read(buffer, 0, bytes); // record how many bytes we actually read
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget();
}
请问有什么解释吗?
2-当Arduino从设备接收到字符串时,它将发送字符串“ OK”。 如何在我的应用程序中将其用作ACK(确认)?
我尝试过但没有成功:
String ack = ""; //global variable
sendstring("test string");// send a test string to arduino
SystemClock.sleep(100); //wait for arduino response
if(ack.equals("OK")) txtv.setText(" well received"); //well done
在处理程序中:
if(msg.what == Bluetooth.MESSAGE_READ){
String receivedstring = new String((byte[]) msg.obj, 0, msg.arg1);
ack = receivedstring ;
我没有收到ack =“ OK”,并且在文本视图中未显示“好评”!
非常感谢您的帮助
答案 0 :(得分:0)
嗨,我对蓝牙聊天不甚了解,但我可能对您的第一个问题有所了解。如果您还没有答案。
is()