Arduino HM-10 BLE 扫描仪开发

Ardunio HM-10 BLE Scanner development

我正在使用带有 HM-10 模块的 Ardunio uno。我正在尝试扫描我周围的所有信标,然后存储信标名称

#include <SPI.h>
#include <SoftwareSerial.h>

String inputTXT;

SoftwareSerial mySerial(10, 11); // RX, TX  


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 loop() {  

  delay(3000);


  mySerial.write("AT+DISI?");

  if (mySerial.available()) {
    inputTXT = mySerial.readString();

    Serial.println(inputTXT);

    inputTXT = ""; 
  }
}

我在每个循环中都重复此输出

OK+DISISOK+DISC:00000000:00000000000000000000000000000000:00000OK+DISISOK+DISCEOK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-065OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-066OK+DISC:00000000:00000000000000000000000000000000:0000000000:72363EC2C661:-084OK+DISC:4C000C0E:008D37DBECB6B76115D006C9B3FA1005:1B1C1E7B5B:76854777DBD7:-072OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F55DEDE396:-079OK+DISC:00000000:00000000000000000000000000000000:0000000000:4D023B8ED54D:-083OK+DISC:00000000:00000000000000000000000000000000:0000000000:6B5DB3EB2

所以我现在想在每次迭代的最后一个冒号之前保存 12 位数字字符串,例如“4CAA0DE091B7”,有人可以告诉我或建议我如何去做吗?

很遗憾,Arduino 没有正则表达式,需要手动设置。

#include <SPI.h>
#include <SoftwareSerial.h>

String inputTXT;

SoftwareSerial mySerial(10, 11); // RX, TX

void setup()
{
    //setup
    Serial.begin(9600);
    mySerial.begin(9600);
    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 loop()
{

    delay(3000);
    mySerial.write("AT+DISI?");
    if (mySerial.available())
    {
        inputTXT = mySerial.readString();
        int pos = 0;
        String result = "";
        const String regx = "00000000:00000000000000000000000000000000:0000000000:";
        const int regx_len = regx.length();
        while ((pos = inputTXT.indexOf(regx, pos)) != -1)
        {
            // substring from <starting point> to >starting point + 12>
            result = inputTXT.substring(pos + regx_len, pos + regx_len + 12);
            Serial.println(result);
            // move starter point to end of last result
            pos = pos + regx_len + 12;
        }
    }
}