This shows you the differences between two versions of the page.
iothings:laboratoare:2022:lab7 [2025/09/28 17:43] dan.tudose [Sparrow BLE Service] |
iothings:laboratoare:2022:lab7 [2025/09/28 17:46] (current) dan.tudose [UUID] |
||
---|---|---|---|
Line 72: | Line 72: | ||
In essence, the UUID serves the purpose of uniquely identifying information. For example, it can distinguish a specific service provided by a Bluetooth device. | In essence, the UUID serves the purpose of uniquely identifying information. For example, it can distinguish a specific service provided by a Bluetooth device. | ||
+ | ===== Advertise on BLE ===== | ||
+ | |||
+ | Build the example below, which advertises the board on BLE. Install on your phone an app that scans nearby Bluetooth devices, such as [[https://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en&pli=1| nRF Connect]]. Check if your device is in the list. | ||
+ | |||
+ | <code C main.cpp> | ||
+ | #include <Arduino.h> | ||
+ | #include <NimBLEDevice.h> | ||
+ | |||
+ | static const char* DEVICE_NAME = "ESP32-C6 Demo"; | ||
+ | static NimBLEUUID SERVICE_UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E"); | ||
+ | static NimBLEUUID CHAR_UUID ("6E400002-B5A3-F393-E0A9-E50E24DCCA9E"); | ||
+ | |||
+ | NimBLEServer* gServer = nullptr; | ||
+ | NimBLEService* gService = nullptr; | ||
+ | NimBLECharacteristic* gChar = nullptr; | ||
+ | |||
+ | void startBLE() { | ||
+ | NimBLEDevice::init(DEVICE_NAME); | ||
+ | |||
+ | gServer = NimBLEDevice::createServer(); | ||
+ | gService = gServer->createService(SERVICE_UUID); | ||
+ | |||
+ | gChar = gService->createCharacteristic( | ||
+ | CHAR_UUID, | ||
+ | NIMBLE_PROPERTY::READ | ||
+ | ); | ||
+ | gChar->setValue("Hello from ESP32-C6!"); | ||
+ | gService->start(); | ||
+ | |||
+ | NimBLEAdvertising* adv = NimBLEDevice::getAdvertising(); | ||
+ | |||
+ | // Advertise our service UUID | ||
+ | adv->addServiceUUID(SERVICE_UUID); | ||
+ | |||
+ | // (v2.x) Build advertising + scan-response payloads explicitly | ||
+ | NimBLEAdvertisementData advData; | ||
+ | advData.setFlags(0x06); // LE General Discoverable + BR/EDR Not Supported | ||
+ | |||
+ | NimBLEAdvertisementData scanData; | ||
+ | scanData.setName(DEVICE_NAME); // put the name in scan response | ||
+ | // you can also add manufacturer data here if you want: | ||
+ | // std::string mfg = "\x34\x12C6"; scanData.setManufacturerData(mfg); | ||
+ | |||
+ | adv->setAdvertisementData(advData); | ||
+ | adv->setScanResponseData(scanData); | ||
+ | |||
+ | // Appearance is still supported | ||
+ | adv->setAppearance(0x0200); // Generic Tag | ||
+ | |||
+ | NimBLEDevice::startAdvertising(); | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | while (!Serial) { delay(10); } | ||
+ | startBLE(); | ||
+ | Serial.println("Advertising as ESP32-C6 Demo. Open nRF Connect -> Scan."); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | delay(1000); | ||
+ | } | ||
+ | |||
+ | </code> | ||
+ | |||
+ | {{:iothings:laboratoare:lab1-ble-scanner.jpg?300|}} | ||
===== Project Overview ===== | ===== Project Overview ===== |