ESP32 BLE scan problem!!!

angelvilchez
Posts: 3
Joined: Sun Sep 30, 2018 3:53 pm

ESP32 BLE scan problem!!!

Postby angelvilchez » Sun Sep 30, 2018 3:55 pm

Hello, I´m testing my new ESP32 dev board. I´m trying of to modify BLE Scan example... This sketch return a list of devices avaibles... I would like launch a action (pinout) if ESP32 detect a concret Device. I have tryed some ways but all times return errors... Someone could help me for fixing it?

This is the code:

Code: Select all

/*
   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
   Ported to Arduino ESP32 by Evandro Copercini
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 30; //In seconds

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  BLEScanResults foundDevices = pBLEScan->start(scanTime);
  Serial.print("Devices found: ");
char X = (foundDevices.getCount());
 Serial.println(X);
//Serial.println(foundDevices.getCount());
  if (X == "Parking"){
    Serial.print("we found Parking"");
    }else{
      Serial.print("Don´t match");
      }
  // put your main code here, to run repeatedly:
  
}

void loop() {


  delay(2000);
}
Thanks!!

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE scan problem!!!

Postby chegewara » Wed Oct 03, 2018 5:35 pm

What errors did you get, what are trying to do?
Here you get count char X = (foundDevices.getCount()); and here you are trying to compare with literal string if (X == "Parking")

angelvilchez
Posts: 3
Joined: Sun Sep 30, 2018 3:53 pm

Re: ESP32 BLE scan problem!!!

Postby angelvilchez » Sat Oct 06, 2018 6:22 am

Sorry... That´s bad. I need to know how can I detect if my Ble Device is between the list scanned. In that case, change pin state. Thanks

ripplingwater
Posts: 2
Joined: Wed Oct 10, 2018 3:18 am

Re: ESP32 BLE scan problem!!!

Postby ripplingwater » Wed Oct 10, 2018 3:35 am

Inside here you may be able to read the device name ( short name, local name etc , that I am not sure ) .... and then you can know if it's the device you are looking for

Code: Select all

    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
It will have the Name, Address, Manufacturer data etc.

angelvilchez
Posts: 3
Joined: Sun Sep 30, 2018 3:53 pm

Re: ESP32 BLE scan problem!!!

Postby angelvilchez » Thu Oct 11, 2018 5:24 pm

ok, thanks... But, How can I into (for example) the name in a string for comparing with a const string?

chegewara
Posts: 2362
Joined: Wed Jun 14, 2017 9:00 pm

Re: ESP32 BLE scan problem!!!

Postby chegewara » Fri Oct 12, 2018 12:52 pm

You can use any function from those:
https://github.com/nkolban/esp32-snippe ... .h#L32-L41
to find device you need, but its advised to add befor test if device is advertising packet you want to compare:
https://github.com/nkolban/esp32-snippe ... .h#L45-L52

Who is online

Users browsing this forum: No registered users and 7 guests