我正在尝试创建ble扫描仪,但只希望显示设备地址
设备地址是输出中:-0
之前的12个字符。
在下面显示的代码中,我尝试通过12个字节的列表运行输出,并停止在:-0
处记录数据的列表,因此它将在此之前记录12个字节
#include <SoftwareSerial.h>
String inputTXT;
SoftwareSerial mySerial(10, 11); // RX, TX
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//setup
mySerial.write("AT");
delay(100);
mySerial.write("AT+ROLE1"); // Master mode
delay(100);
mySerial.write("AT+IMME1"); //wait for a connection command before connecting
delay(100);
mySerial.write("AT+RESET");
delay(50);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ":-0";
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}}
void loop() {
delay(3000);
mySerial.write("AT+DISI?");
recvWithEndMarker(); //Put in at end
showNewData();
if (mySerial.available()) {
inputTXT = mySerial.readString();
Serial.println(inputTXT);
// inputTXT = "";
}
}
串行输出是这样的:
OK+DISCEOK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:6DB9F38898FF:-081OK+DISC:00000000:00000000000000000000000000000000:0000000000:485E425C5B3A:-075OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-069OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-070OK+DISC:00000000:00000000000000000000000000000000:0000000000:47C4C908CFB4:-073OK+DISC:
OK+DISCEOK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:485E425C5B3A:-060OK+DISC:00000000:00000000000000000000000000000000:0000000000:47C4C908CFB4:-066OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-071OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-069OK+DISC:00000000:00000000000000000000000000000000:0000000000:6DB9F38898FF:-077OK+DISC:
OK+DISC:00000000:00000000000000000000000000000000:0000000000:DFOK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:DF12A8020D32:-051OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-069OK+DISC:00000000:00000000000000000000000000000000:0000000000:485E425C5B3A:-063OK+DISC:00000000:00000000000000000000000000000000:0000000000:47C4C908CFB4:-082OK+DISC:00000000:00000000000000
OK+DISCEOK+DISISOK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-069OK+DISC:00000000:00000000000000000000000000000000:0000000000:47C4C908CFB4:-067OK+DISC:00000000:00000000000000000000000000000000:0000000000:DF12A8020D32:-051OK+DISC:00000000:00000000000000000000000000000000:0000000000:485E425C5B3A:-065OK+DISC:00000000:00000000000000000000000000000000:0000000000:6DB9F38898FF:-078OK+DISC:
我编写的代码似乎并没有影响输出,我是在正确的位置获取输出,例如:
DF12A8020D32
38F9D379C9E5
对此,任何帮助将不胜感激!