esp32 list bluetooth devices

Esp32 Bluetooth Devices: Complete List

By James Thompson
AD

Embarking on a journey to interact with Bluetooth devices using your ESP32? You've come to the right place. This guide will walk you through the process of listing Bluetooth devices around your ESP32, ensuring a smooth and engaging experience.

The ESP32, a prominent figure in the realm of low-power, single-core microcontrollers, packs a punch with its built-in Bluetooth low energy (BLE) capabilities. Let's harness this power to explore the Bluetooth ecosystem around us.

Setting Up Your ESP32 for Bluetooth

The first step involves configuring your ESP32 to recognize and list nearby Bluetooth devices. We'll use the Arduino IDE, a popular choice for ESP32 programming.

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
DIY Pocket Size ESP32 AI Voice Assistant With Xiaozhi
DIY Pocket Size ESP32 AI Voice Assistant With Xiaozhi
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes

Before we dive into the code, ensure you have the ESP32 board definition and corresponding libraries installed in your Arduino IDE. You can find these in the Boards Manager and Library Manager respectively.

Including Necessary Libraries

Your code begins with including the required libraries. You'll need 'BluetoothSerial.h' for Bluetooth functionalities and 'esp_log.h' for logging purposes. Here's how you initiate them:

10 Useful ESP32 Concepts That Take You from Noob to Pro!
10 Useful ESP32 Concepts That Take You from Noob to Pro!
an electronic device is shown in this poster, with instructions on how to use it
an electronic device is shown in this poster, with instructions on how to use it
ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32
ESP32 Bluetooth Tutorial | How to Use Inbuilt Bluetooth of ESP32

`#include ` `#include `

Initializing the Bluetooth Serial

After including the libraries, initialize the BluetoothSerial object. This will enable your ESP32 to serve as a BLE server or connect to other BLE devices. Here's the snippet:

AD

`BluetoothSerial SerialBT;`

Anatomy esp32
Anatomy esp32
ESP-NOW vs Bluetooth (ESP32) ⚔️📡
ESP-NOW vs Bluetooth (ESP32) ⚔️📡
esp 32 is a machine that can make you an engineer
esp 32 is a machine that can make you an engineer
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Bluetooth + Manual Home automation - IotCircuitHub
ESP32 Bluetooth + Manual Home automation - IotCircuitHub
ESP32 or ESP8266? Choose the Right Board
ESP32 or ESP8266? Choose the Right Board

Scanning for Bluetooth Devices

With the basics covered, let's delve into scanning for nearby Bluetooth devices. The ESP32 supports both active and passive scanning, but for listing devices, passive scanning is more energy-efficient.

Passive scanning involves the ESP32 listening for advertisements from other BLE devices. It's like waiting in a library for someone to walk in and announce their presence. Here's how you implement passive scanning:

AD

Setting Up the Scan Parameters

Before starting the scan, set up the parameters. For passive scanning, set the window duration (ms) and interval (ms) as desired. Here's an example:

How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
ESP32 USB-C WiFi Bluetooth Board for IoT & Robotics
ESP32 USB-C WiFi Bluetooth Board for IoT & Robotics
Mp3Player
Mp3Player
ESP32 or ESP8266? Choose the Right Board
ESP32 or ESP8266? Choose the Right Board
ESP32 Cheat Sheet for Beginners
ESP32 Cheat Sheet for Beginners
ESP32 Development Board WiFi+Bluetooth Ultra-Low Power Consumption Dual Core ESP-32 ESP-32S ESP 32
ESP32 Development Board WiFi+Bluetooth Ultra-Low Power Consumption Dual Core ESP-32 ESP-32S ESP 32

`uint16_t windowWidth = 30; // Window width in ms` `uint16_t interval = 50; // Interval in ms between each window`

Starting the Scan

With the parameters set, start the BLE scan. Pass the windowWidth and interval parameters to the 'scan_for_adv_data()' function. Once started, the ESP32 will listen for advertisements and store the device information. Here's the code:

AD

`bool scan_result = BluetoothSerial::scanFor AdsData_t *p.adv_data, uint16_t len, esp_addrf unreal_mac, ScanStatuszar, interval);`

Processing the Scan Results

After the scan, process the results. For each device, extract the device name and MAC address. Print or store this information as needed. Here's a simple way to do this:

`for (int i = 0; i < scan_result; i++){` `Serial.print("Device Name: ");` `Serial.println((char*)p.adv_data);` `Serial.print("MAC Address: ");` `uint8_t mac[6];` `unreal_mac.toBytes(mac);` `Serial.println(mac);` `}`

The final step is to stop the Bluetooth serial service when it's no longer needed. This can be done with `SerialBT.end();`.

Your ESP32 is now equipped to list nearby Bluetooth devices, making it an excellent starting point for IoT projects involving Bluetooth communications. Happy exploring!

AD