【发布时间】:2018-09-03 15:35:35
【问题描述】:
我们正在开发一个应用程序,该应用程序使用蓝牙库通过 HC-05 模块与蓝牙中的 Arduino 进行通信。我们做了一个虚拟配置来测试延迟,而无需 Arduino 或应用程序进行任何计算,我们在请求和回答之间有大约 1 秒的巨大延迟......
协议看起来很简单:Android 发送字节 -2,如果接收到的字节为 -2,Arduino 发送 -6、-9,Android 一次又一次地回答。
安卓代码:
h = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case RECIEVE_MESSAGE: // if receive massage
byte[] readBuf = (byte[]) msg.obj;
for(int i=0;i < readBuf.length;i++)
{
if((int) readBuf[i] != 0) {
txtArduino.append(String.valueOf((int) readBuf[i]) + ", ");
}
}
byte[] msg = {-2};
mConnectedThread.writeByte(msg);
break;
}
};
};
Arduino 代码:
const int receveidBuffLen = 8*4;
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0)
{
byte buff[receveidBuffLen];
Serial.readBytes(buff, receveidBuffLen);
for(int i=0; i < receveidBuffLen;i++)
{
if(buff[i] == (byte) -2) // 254
{
byte message[2] = {(byte) -6, (byte) -9};
Serial.write(message, 2);
Serial.flush();
}
}
}
delay(3);
}
有谁知道延迟来自哪里?
我们更改了 HC05 波特率(从 9600 到 115 200):什么也没发生。我们将 HC05 更改为另一个:什么也没发生。我们之前使用了Blue2Serial库(蓝牙作为SPP),延迟是一样的......我们使用了另一个控制器(ESP8266),延迟仍然是1秒......
【问题讨论】:
标签: android bluetooth arduino delay