Arduino HM-10 BLE scan for bluetooth

Hi

I have an Arduino Nano with a HM-10 BLE and servo attached. I would like to declare multiple Bluetooth physical addresses in the code and have Arduino search for them. Once it finds one of them, it rotates the servo and if out of range it rotates servo back to the starting position.

How would i go about doing this?

Thanks in advance

EDIT: pasted the code that at the moment can rotate the servo when prompted

#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(0,1);
Servo servo;
#define  pos_ini 0

void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
servo.write(pos_ini);
servo.attach(7, 500, 2500);
}
void loop()
{
int i;

if (mySerial.available())
{
i=mySerial.read();
if(i=='u' || i== 'U')
{
 Serial.println("Unlocked");
 servo.write(100);
}
if(i=='l' || i=='L')
{
 Serial.println("Locked");
 servo.write(0);
}
}
}

you connect to your module using the Serial line and issue AT+ROLE1 and AT+IMME1 then you can scan using the AT+DISC? command. Parse the results to see what devices are within range

have a look at the HM-10 datasheet

This is very interesting. I'm trying to do something similar using a HC-05. Ideally, the module should scan for devices every 10 seconds, then if it finds my phone it will set an output pin to HIGH, otherwise if my phone is not within range it will set it to LOW. I'm new to Arduino programming and I would really appreciate if somebody could share some info or an example.

I'm unclear if "finding the phone" means the phone is connected.

Typically with the HC-05 classic bluetooth the phone is the master and finds and connects to the HC-05. Trying to set the phone as a slave and the HC-05 as a master is a very different situation.

If your intent is to see if the previously connected phone has gone out of range or off line, then see this thread for how to use the HC-05 State Pin to monitor a phone connection.
https://forum.arduino.cc/t/hc-05-state-pin/573388

Thanks for your reply and sorry if I wasn't clear. I'm not really looking to establish a connection. All I need is for the HC-05 to scan all devices and see if my phone is around without any exchange of information.

It would not work with an iPhone. Not sure it does with Android.

Bluetooth Classic uses the master/slave architecture, and the master device scans for slave devices. The phone is typically the master.

I do not think that an Android phone can be set up as a slave device to be scanned by an HC05 configured as master.

Perhaps it can be done with some custom Android programming, but it's not going to happen with the standard bluetooth classic implementation on the phone.

I'm reluctant to buy too deeply into this but I think it is a reasonable proposition, particularly as you just want to sniff for it, and I don't think it is necessary to set the phone as a slave. Rather than "scan all", I believe you need to "scan for specific", and all you need is the MAC address of the phone with the appropriate AT command(s) in a similar manner as one would with an HC-05.

I don't think being a master means anything more than being the one that initiates a connection, and specifically does not mean it cannot be a connectee - hence the auto-connect to your master phone when you start your car.

There is probably something from Martyn Currey about this, and maybe here

Thanks Nick, yeah I've read a lot and according to what I was able to find the difference between master and slave should be just about who can or cannot initiate the connection.

I had a look at the documentation you shared, however that's for HC-10, I'm trying to do the same but using HC-05 and although I found a lot of stuff I wasn't able to adapt any of them to my project.

I have seen a guy on this YouTube video doing something similar with HC-05 and the wifi module, where he scans for all devices and then transfers a list of all MAC addresses to the wifi module. His work is based on this page from Martyn Currey (I saw you mentioned him).

However, reading his code I can't figure out what is the part I need to remove since I don't need to send stuff to the wifi module. I know how to add an if statement to compare each address with my phone's one, but I can't figure out where and how exactly I should swap the wifi command with this.

An iPhone does not expose a slave API for BT classic and for WiFi smartPhones are being smart and to prevent tracking and protect privacy or specific attack, they return changing random MAC address until they are being authenticated and paired / connected.

So sniffing is not something you can rely on those days. (or may be with Android old versions and/or custom apps)


assuming the code you pointed at works and does the right thing parsing the output of the AT command, that would build the list of hit:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX

char lineBuffer[100];
char subBuffer[30];
int index = 0;
int index2 = 0;
int total = 0;
bool capture = false;
String dataCapture = "";

void delayAndRead() {
  delay(50);
  while (BTserial.available()) BTserial.read();
  delay(800);
}

void initHC05ToInq()
{
  BTserial.println("AT+CMODE=1");// Enable connect to any device
  delayAndRead();
  BTserial.println("AT+ROLE=1");// Set to master in order to enable scanning
  delayAndRead();
  BTserial.println("AT+INQM=1,10,24");//RSSI, Max 10 devices, ~30s
  delayAndRead();
  BTserial.println("AT+CLASS=0");// Disable COD filter
  delayAndRead();
  BTserial.println("AT+INIT");// Init.
  delayAndRead();
}

void setup()
{
  Serial.begin(115200);

  // HC-05 default serial speed for AT mode is 38400
  BTserial.begin(38400);

  // Wait for hardware to initialize
  delay(1000);

  // Set correct states for inq
  initHC05ToInq();

  // Start inq
  BTserial.println("AT+INQ");
}



void loop()
{
  // Keep reading from HC-05 and send to Arduino Serial Monitor
  if (BTserial.available()) {
    // Read character and append into buffer
    char c = BTserial.read();
    if (index < 99) lineBuffer[index++] = c;
    // When line ends
    if (c == '\n') {
      lineBuffer[index - 1] = '\0'; // Remove line end characters from buffer
      index = 0;                    // Reset buffer index for next line

      if (lineBuffer[0] == 'O' && lineBuffer[1] == 'K') { // Message starts with "OK", AT+INQ
        if (total > 0) Serial.println(dataCapture); // ****** THIS IS THE LIST OF MAC ADDRESSES ****** 
        // Restart INQ
        BTserial.println("AT+INQ");
        total = 0;
        dataCapture = "";
      } else {
        capture = false;
        index2 = 0;
        for (index = 0; index < 30; index++) {
          if (!capture) {
            if (lineBuffer[index] == ':')
            {
              capture = true;
            }
          }
          else
          {
            subBuffer[index2] = lineBuffer[index];
            if (lineBuffer[index] == ',')
            {
              subBuffer[index2] = 0;
              break;
            }
            index2++;
          }
        }
        index = 0;

        // Add this line buffer
        String str((char*)subBuffer);
        if (dataCapture.indexOf(str) <= 0) {
          if (total > 0) dataCapture += ",";           // If not first then add comma
          dataCapture += "\"";
          dataCapture += str;  //***** THIS IS THE NEWLY ADDED ENTRY
          dataCapture += "\"";
          // Keep count
          total++;
        }
      }
    }
  }
}

the full list is printed out with

if (total > 0) Serial.println(dataCapture); // ****** THIS IS THE LIST OF MAC ADDRESSES ****** 

each entry is added into the dataCapture String with this line

dataCapture += str;  //***** THIS IS THE NEWLY ADDED ENTRY

assuming the parsing is correct. I've not tested

the serial output should look like this:

"2845:98:680782","1D:D8:96578A"

if you found those 2 devices