我目前正在尝试从我的 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 ON”,但什么也没发生
最佳答案
尝试从 loop() 代码周围移除 while (true) {} 包装器。因为 Arduino 一遍又一遍地调用 loop(),loop() 函数充当 while (true) 循环。
让您的 loop() 函数永不返回 - 正如您的代码当前所做的那样 - 会阻止处理器执行其他操作,例如运行蓝牙堆栈。我没有编写 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();
}
}
}
关于android - 有没有一种特殊的方法可以通过BLE通过arduino发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428653/