ESP32 bluetooth low energy connection to Computer

I have been trying for days to connect my ESP32 S3 to a my computer (MacBook Pro M1/macOS Sequoia) via bluetooth low energy, but it simply does not work. Could anybody help me with an example or instructions. Those are the codes that I am currently using:

ESP32 code:

#include <BLEDevice.h>

#include <BLEServer.h>

#include <BLEUtils.h>

#include <BLE2902.h>

// Define UUIDs for the service and characteristic

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"

#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

BLEServer* pServer = NULL;

BLECharacteristic* pCharacteristic = NULL;

bool deviceConnected = false;

int counter = 0;

class MyServerCallbacks: public BLEServerCallbacks {

void onConnect(BLEServer* pServer) {

deviceConnected = true;

Serial.println("Device connected");

};

void onDisconnect(BLEServer* pServer) {

deviceConnected = false;

Serial.println("Device disconnected");

// Restart advertising when disconnected

BLEDevice::startAdvertising();

}

};

void setup() {

Serial.begin(115200);

Serial.println("Starting BLE Counter Service...");

// Create the BLE Device

BLEDevice::init("ESP32-Counter");

// Create the BLE Server

pServer = BLEDevice::createServer();

pServer->setCallbacks(new MyServerCallbacks());

// Create the BLE Service

BLEService *pService = pServer->createService(SERVICE_UUID);

// Create a BLE Characteristic

pCharacteristic = pService->createCharacteristic(

CHARACTERISTIC_UUID,

BLECharacteristic::PROPERTY_READ |

BLECharacteristic::PROPERTY_WRITE |

BLECharacteristic::PROPERTY_NOTIFY

);

// Create a BLE Descriptor

pCharacteristic->addDescriptor(new BLE2902());

// Start the service

pService->start();

// Start advertising

BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();

pAdvertising->addServiceUUID(SERVICE_UUID);

pAdvertising->setScanResponse(true);

pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue

pAdvertising->setMinPreferred(0x12);

BLEDevice::startAdvertising();

Serial.println("BLE service started, waiting for clients...");

}

void loop() {

if (deviceConnected) {

counter++;

// Cycle through numbers 1-10

if (counter > 10) {

counter = 1;

}

// Convert counter to a string

char counterStr[3];

sprintf(counterStr, "%d", counter);

// Set the value and notify

pCharacteristic->setValue(counterStr);

pCharacteristic->notify();

Serial.print("Sending counter value: ");

Serial.println(counter);

// Wait 1 second before sending the next value

delay(1000);

}

}

Computer code:

import asyncio

from bleak import BleakScanner, BleakClient

SERVICE_UUID = "4fafc201-1fb5-459e-8fcc-c5c9c331914b"

CHARACTERISTIC_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8"

ESP32_DEVICE_NAME = "ESP32-Counter"

async def notification_handler(sender, data):

value = int(data.decode())

print(f"Received value: {value}")

async def find_esp32():

print("Scanning for ESP32 device...")

devices = await BleakScanner.discover()

for device in devices:

if [device.name](http://device.name/) == ESP32_DEVICE_NAME:

print(f"Found ESP32 device: {device.name} [{device.address}]")

return device

return None

async def connect_to_esp32():

device = await find_esp32()

if not device:

print(f"Could not find {ESP32_DEVICE_NAME}. Make sure it's powered on and advertising.")

return

try:

async with BleakClient(device) as client:

print(f"Connected to {device.name}")

await client.start_notify(

CHARACTERISTIC_UUID,

notification_handler

)

print("Listening for counter values. Press Ctrl+C to exit.")

while True:

await asyncio.sleep(1)

except Exception as e:

print(f"Error: {e}")

finally:

print("Disconnected from ESP32")

if __name__ == "__main__":

asyncio.run(connect_to_esp32())

Can you find the esp32 with the normal Mac Bluetooth?

Not on Settings>Bluetooth.
Is there any other way?

I am not a Bluetooth expert but my understanding is there are two kinds,, classic and BLE (Bluetooth Low Energy). Maybe the esp32 is one of those but your Mac the other.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.