ESP32-C6 Bluetooth scanner example

I am trying to follow this page Bluetooth Usage | Seeed Studio Wiki to make bluetooth work with my Xiao ESP32-C6. The very first example of the page, bluetooth scanner, already does not compile and gives the error: “‘init’ is not a member of ‘BLEDevice’”.

I guess this is because I have not added the required libraries to the Arduino IDE. The page does not state which are nor where are the required libraries. Can someone point it to me?

See File–>Examples–>BLE–>Scan
and try this…

void loop() {
  // put your main code here, to run repeatedly:
//  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  BLEScanResults *foundDevices = pBLEScan->start(scanTime, false);
  Serial.print("Devices found: ");
//  Serial.println(foundDevices.getCount());
  Serial.println(foundDevices->getCount());
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(10000);
}

Hi there,
I get that error if I have ArduinoBLE LIB on the system , if I uninstall it the error goes away and it compiles and runs Perfectly.
HTH
GL :slight_smile: PJ

Latest BSP 3.0.4 is what I tested with,
Here is the serial output from scanning:

Advertised Device: Name: iTrack, Address: d2:b1:90:70:38:6d, manufacturer data: 4b4d326d387090b1d264, serviceUUID: 00001802-0000-1000-8000-00805f9b34fb, rssi: -73 
Advertised Device: Name: , Address: 74:d1:13:9f:95:ac, manufacturer data: 4c000719010f2002f98f01000440563b9e1f74a3de0533a8a056e67de4, rssi: -10 
Devices found: 6
Scan done!
Advertised Device: Name: , Address: 44:19:3c:1c:59:84, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -76, serviceData: J#6LA52�8�E�v�f��}�-��l�T 
Advertised Device: Name: , Address: 40:b8:9a:7c:74:32, manufacturer data: 2d01020001107f00ec1e26c14a308ed77030f97b63ef982a6793f8a5, rssi: -93 
Advertised Device: Name: , Address: 4c:7f:05:0c:51:43, serviceUUID: 0000fef3-0000-1000-8000-00805f9b34fb, rssi: -92, serviceData: J#YLBT2��]��#���{g�9 
Advertised Device: Name: iTrack, Address: d2:b1:90:70:38:6d, manufacturer data: 4b4d326d387090b1d264, serviceUUID: 00001802-0000-1000-8000-00805f9b34fb, rssi: -85 
Advertised Device: Name: , Address: 6d:9a:85:dd:c9:93, manufacturer data: 4c001006731d2f437d80, txPower: 12, rssi: -97 
Advertised Device: Name: iTrack, Address: c5:dd:b0:ac:6b:51, manufacturer data: 4b4d32516bacb0ddc577, serviceUUID: 00001802-0000-1000-8000-00805f9b34fb, rssi: -68 
Advertised Device: Name: , Address: f6:a8:3c:14:6e:f5, manufacturer data: 4c0012020003, rssi: -98 
Advertised Device: Name: , Address: 74:d1:13:9f:95:ac, manufacturer data: 4c000719010f2002f98f01000440563b9e1f74a3de0533a8a056e67de4, rssi: -10 
Advertised Dev

:+1:

Hi there,
I like this for compactness,
Uses a vector to store the info if it has a name, other wise it’s skipped.
here is the code,

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

// Struct to store BLE device name and RSSI
struct BLEDeviceInfo {
  String deviceName;
  int rssi;
};

// Vector to store devices
std::vector<BLEDeviceInfo> devices;

// Scan time in seconds
int scanTime = 30;
BLEScan *pBLEScan;

// Custom callback class to handle found devices
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    if (advertisedDevice.haveName()) {  // Only store if the device has a name
      BLEDeviceInfo dev;
      dev.deviceName = advertisedDevice.getName().c_str();
      dev.rssi = advertisedDevice.getRSSI();
      devices.push_back(dev);  // Store device name and RSSI in vector
    }
  }
};

void setup() {
  Serial.begin(9600);
  delay (2000);
   Serial.println();
  Serial.println("Power ON \n ");  // Let's BEGIN!!
  Serial.println("Test program compiled on " __DATE__ " at " __TIME__);
  Serial.println();
  Serial.println("Processor came out of reset.");
  Serial.println("Scanning...");

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan();  // Create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);    // Active scan uses more power, but gets results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);          // Less or equal to setInterval value
}

void loop() {
  // Clear devices vector
  devices.clear();

  // Start BLE scan
  pBLEScan->start(scanTime, false);

  // Sort devices by RSSI in descending order
  std::sort(devices.begin(), devices.end(), [](const BLEDeviceInfo &a, const BLEDeviceInfo &b) {
    return a.rssi > b.rssi;
  });

  // Print sorted devices with names and RSSI
  Serial.println("Devices found (sorted by RSSI):");
  for (const auto &device : devices) {
    Serial.printf("Device Name: %s, RSSI: %d\n", device.deviceName.c_str(), device.rssi);
  }

  Serial.println("Scan done!");
  pBLEScan->clearResults();  // Clear results from the BLE scan buffer to release memory

  delay(2000);
}

and her is the output,

Power ON 
 
Test program compiled on Sep 19 2024 at 00:47:46

Processor came out of reset.
Scanning...
Devices found (sorted by RSSI):
Device Name: iTrack, RSSI: -65
Device Name: iTrack, RSSI: -68
Scan done!

HTH
GL :slight_smile: PJ :v:

FQBN: esp32:esp32:XIAO_ESP32C6
Using board ‘XIAO_ESP32C6’ from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4
Using core ‘esp32’ from platform in folder: C:\Users\Dude\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.4

Guys, this → “See File–>Examples–>BLE–>Scan” is the second step. I want to know the first step, which library I have to install, and where to find it?

Have you seen the link below?

Getting Started with Seeed Studio XIAO ESP32C6 | Seeed Studio Wiki
Section “Software Preparation”

Sorry but that is not the problem. I have followed the guide you posted and am able to compile and run every example. My question is where (and which) to find the bluetooth library?

Hi there, Which is it? I showed you why the “init” error. The BLE libraries are in the boards file unless you want something like NIMBLE or a different stack or SD.
look at the compiler output and see what it is showing you as far as a path goes.
GL :slight_smile: PJ :v:

Is this the information you need to know?
C:\Users\username\AppData\Local\Arduino15\packages\esp32\hardware\esp32\3.0.2\libraries\BLE