I have prepared a Test BLE Server to rebuild a LEGO HUB, Test is for Advertising only
Examle code:
- *
- Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
- Ported to Arduino ESP32 by Evandro Copercini
- updates by chegewara
- */
- #include <Arduino.h>
- #include <BLEDevice.h>
- #include <BLEUtils.h>
- #include <BLEServer.h>
- // See the following for generating UUIDs:
- // https://www.uuidgenerator.net/
- #define SERVICE_UUID "00001623-1212-EFDE-1623-785FEABCD123"
- #define CHARACTERISTIC_UUID "00001624-1212-EFDE-1623-785FEABCD123"
- BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
- //BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
- const char advLEGO[] = {0x02,0x01,0x06,0x11,0x07,0x23,0xD1,0xBC,0xEA,0x5F,0x78,0x23,0x16,0xDE,0xEF,
- 0x12,0x12,0x23,0x16,0x00,0x00,0x09,0xFF,0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};
- char ManufacturerData[8] = {0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};
- void setup() {
- Serial.begin(115200);
- Serial.println("Starting BLE work!");
- BLEDevice::init("Long name works now");
- BLEServer *pServer = BLEDevice::createServer();
- BLEService *pService = pServer->createService(SERVICE_UUID);
- BLECharacteristic *pCharacteristic = pService->createCharacteristic(
- CHARACTERISTIC_UUID,
- BLECharacteristic::PROPERTY_READ |
- BLECharacteristic::PROPERTY_WRITE
- );
- pCharacteristic->setValue("Hello World says Neil");
- pService->start();
- // BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
- pAdvertising->addServiceUUID(SERVICE_UUID);
- pAdvertising->setScanResponse(true);
- oAdvertisementData.setShortName("Lego Hub");
- oAdvertisementData.setManufacturerData(ManufacturerData);
- pAdvertising->setAdvertisementData(oAdvertisementData);
- pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
- pAdvertising->setMinPreferred(0x12);
- BLEDevice::startAdvertising();
- Serial.println("Characteristic defined! Now you can read it in your phone!");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- delay(2000);
- }
Goal ist that scan and connect result from BLE Client is as follows like with original LEGO HUB:
Scanning ...<CR><LF>
[ADV 596] Packet received from 90:84:2B:4A:3A:0C<LF>
PAYLOAD 31 bytes<LF>
02-01-06-11-07-23-D1-BC-EA-5F-78-23-16-DE-EF-12-12-23-16-00-00-09-FF-97-03-00-80-06-00-41-00<CR><LF>
RSSI -29 dBm<LF>
ADV TYPE Connectable undirected<CR><LF>
128-Bit UUID #Ѽê_x#<SYN>Þï<DC2><DC2>#<SYN>00001623-1212-EFDE-1623-785FEABCD123<CR><LF>
LEGO BLE SERVICE UUID Found!<LF>
MAN SPEC DATA 97-03-00-80-06-00-41-00<CR><LF>
<CR><LF>
Connected<CR><LF>
Discovering LEGO HUB Service ... Found LEGO HUB Service <CR><LF>
Discovering characteristic ... Found LEGO Data Characteristic<CR><LF>
The result of my code from Above is like :
Scanning ...<CR><LF>
[ADV 655] Packet received from BC:DD:C2:CB:69:FA<LF>
PAYLOAD 14 bytes<LF>
09-08-4C-65-67-6F-20-48-75-62-03-FF-97-03<CR><LF>
RSSI -31 dBm<LF>
ADV TYPE Connectable undirected<CR><LF>
SHORT NAME Lego Hub<LF>
MAN SPEC DATA 97-03<CR><LF>
<CR><LF>
Connected<CR><LF>
Discovering LEGO HUB Service ... Found LEGO HUB Service <CR><LF>
Discovering characteristic ... Found LEGO Data Characteristic<CR><LF>
Question is Why ?
Thanks in advance
MArc