gpt4 book ai didi

安卓+Arduino蓝牙通讯

转载 作者:行者123 更新时间:2023-11-29 00:19:00 25 4
gpt4 key购买 nike

我有一个关于android蓝牙工作原理的问题。我有一个使用 HC-06 屏蔽连接到 arduino 的应用程序。在接收时,我在第一个字符后得到新行。假设我从 arduino 发送

12345

在 Android 中我在 ListView 中看到

1
2345

我相信这是关于 android 而不是 arduino,因为我将 arduino 连接到 PC 并且我收到了正确的数据。

case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(readMessage);
break;

任何关于如何调试的想法是它真的是 android 还是它的 arduino 失败了?如果您有兴趣,我会发布 arduino 代码:

void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

编辑关于“\n”的问题只出现在 arduino+HC-06 bluetooth shield 上。连接到 PC 时,Android 平板电脑会显示正确的数据。

最佳答案

答案是检查字符串结尾。

private void onBluetoothRead(byte[] buffer, int len) {

String output = new String(buffer, 0, len); // Add read buffer to new string
Log.i(LOGGER_TAG, String.format("Received: "+ output + " , " + "%d bytes", len));
outputTemp += output;
if (outputTemp.endsWith("\n")){
m_deviceOutput.append(outputTemp); // Add (not replace) string to TextView
StringTokenizer splitStr = new StringTokenizer(outputTemp, ","); // split string by comma
String numberOne = splitStr.nextToken().replaceAll("\\D+",""); // First split string
String numberTwo = splitStr.nextToken().replaceAll("\\D+",""); // Second split string
m_deviceOutputPrs.setText(numberOne);
m_deviceOutputSpeed.setText(numberTwo);
outputTemp = "";
}
}

关于安卓+Arduino蓝牙通讯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24322371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com