Advertisement
CountRogula

Bluetooth Scan & Send

Jun 9th, 2021
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SoftwareSerial BTserial(2, 3); // RX, TX
  2.  
  3. #define baud 9600
  4.  
  5. void setup()  
  6. {
  7.   // Open serial communications and wait for port to open:
  8.   Serial.begin(baud);
  9.   BTserial.begin(baud);
  10.  
  11.   at("AT"); // check if working, always returns OK
  12.   at("AT+RENEW"); // select master = central  
  13.   at("AT+ROLE1"); // select master = centra
  14.   at("AT+RESET"); // actually more a restart than a reset .. needed after ROLE
  15.   at("AT+SHOW1"); // include Bluetooth name in response
  16.   at("AT+SCAN5"); // set discobery time to 5 seconds
  17.   at("AT+IMME1"); // "work immediately", not sure what this does
  18.   //at("AT+FILT0"); // show all BLE devices, not only HM ones
  19.   delay(1000); // wait a bit, NECESSARY!!
  20.   BTserial.print("AT+DISC?"); // here comes the magic
  21.  while(!BTserial.find("OK+DISCS")) Serial.print(".");
  22.    Serial.println("OK+DISCS");
  23. }
  24.  
  25. void at(char* cmd) {
  26.   BTserial.write(cmd);
  27.   Serial.print(cmd);
  28.   while(!BTserial.find("OK")) Serial.print(".");
  29.   while (BTserial.available()) Serial.write(BTserial.read());
  30.   Serial.println(" .. OK");
  31. }
  32.  
  33.  
  34. void loop() // run over and over
  35. {
  36.   if (BTserial.available())
  37.     Serial.write(BTserial.read());
  38.   if (Serial.available())
  39.     BTserial.write(Serial.read());  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement