我目前正在尝试从Arduino卡+ Seeed BLE Shield HM-11发送数据(主要是文本),但是我有。我可以毫无问题地从android蓝牙终端发送文本,但是从屏蔽罩接收手机上的数据时,它不起作用,也不会引发任何异常。
请注意,LED仅用于验证电话和护罩是否已连接。
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 3
#define TxD 4
#define PINLED 7
#define LEDON() digitalWrite(PINLED, HIGH)
#define LEDOFF() digitalWrite(PINLED, LOW)
#define DEBUG_ENABLED 1
SoftwareSerial Bluetooth(RxD, TxD);
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(PINLED, OUTPUT);
LEDOFF();
setupBlueToothConnection();
}
void loop()
{
char recvChar;
while(true)
{
if (Bluetooth.available())
{ //check if there's any data sent from the remote bluetooth shield
recvChar = Bluetooth.read();
Serial.print(recvChar);
if (recvChar == '1')
{
LEDON();
Bluetooth.write("Led ON");
}
else if (recvChar == '0')
{
LEDOFF();
}
}
}
}
/***************************************************************************
Function Name: setupBlueToothConnection
Description: initilizing bluetooth connction
Parameters:
Return:
***************************************************************************/
void setupBlueToothConnection()
{
Bluetooth.begin(9600);
Bluetooth.print("AT");
delay(400);
Bluetooth.print("AT+DEFAULT"); // Restore all setup value to factory setup
delay(2000);
Bluetooth.print("AT+NAMESeeedBTSlave"); // set the bluetooth name as "SeeedBTSlave" ,the length of bluetooth name must less than 12 characters.
delay(400);
Bluetooth.print("AT+PIN0000"); // set the pair code to connect
delay(400);
Bluetooth.print("AT+AUTH1"); //
delay(400);
Bluetooth.print("AT+NOTI1"); //
delay(400);
Bluetooth.flush();
}
我希望该代码在收到“ 1”但没有任何反应的情况下回答“ LED亮”
答案 0 :(得分:0)
尝试从loop()代码周围删除while(true){}包装。由于Arduino一遍又一遍地调用loop(),因此loop()函数充当while(true)循环。
拥有loop()函数永远不会返回-就像您的代码当前一样-阻止处理器执行其他操作,例如运行Bluetooth堆栈。我尚未编写Arduino蓝牙代码,但是使用Wifi时遇到了类似的问题:如果loop()花费的时间太长,则后台功能可能会失败。
校正后的loop()函数应如下所示:
void loop()
{
char recvChar;
if (Bluetooth.available())
{ //check if there's any data sent from the remote bluetooth shield
recvChar = Bluetooth.read();
Serial.print(recvChar);
if (recvChar == '1')
{
LEDON();
Bluetooth.write("Led ON");
}
else if (recvChar == '0')
{
LEDOFF();
}
}
}