BLE Server, problems with custom advertisement and manufacturer data

ruppie
Posts: 9
Joined: Sat Jan 20, 2018 9:34 am

BLE Server, problems with custom advertisement and manufacturer data

Postby ruppie » Fri Jul 10, 2020 5:51 pm

Hello,

I have prepared a Test BLE Server to rebuild a LEGO HUB, Test is for Advertising only
Examle code:
  1.  
  2. *
  3.     Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
  4.     Ported to Arduino ESP32 by Evandro Copercini
  5.     updates by chegewara
  6. */
  7. #include <Arduino.h>
  8.  
  9. #include <BLEDevice.h>
  10. #include <BLEUtils.h>
  11. #include <BLEServer.h>
  12.  
  13. // See the following for generating UUIDs:
  14. // https://www.uuidgenerator.net/
  15.  
  16. #define SERVICE_UUID        "00001623-1212-EFDE-1623-785FEABCD123"
  17. #define CHARACTERISTIC_UUID "00001624-1212-EFDE-1623-785FEABCD123"
  18. BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  19. //BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  20.  
  21. const char advLEGO[] = {0x02,0x01,0x06,0x11,0x07,0x23,0xD1,0xBC,0xEA,0x5F,0x78,0x23,0x16,0xDE,0xEF,
  22.                           0x12,0x12,0x23,0x16,0x00,0x00,0x09,0xFF,0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};                    
  23.  
  24.  char  ManufacturerData[8] = {0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};
  25.  
  26.  
  27.  
  28. void setup() {
  29.  
  30.  
  31.   Serial.begin(115200);
  32.   Serial.println("Starting BLE work!");
  33.  
  34.   BLEDevice::init("Long name works now");
  35.   BLEServer *pServer = BLEDevice::createServer();
  36.   BLEService *pService = pServer->createService(SERVICE_UUID);
  37.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  38.                                          CHARACTERISTIC_UUID,
  39.                                          BLECharacteristic::PROPERTY_READ |
  40.                                          BLECharacteristic::PROPERTY_WRITE
  41.                                        );
  42.  
  43.   pCharacteristic->setValue("Hello World says Neil");
  44.   pService->start();
  45.   // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  46.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  47.   pAdvertising->addServiceUUID(SERVICE_UUID);
  48.  
  49.    pAdvertising->setScanResponse(true);
  50.  
  51.  
  52. oAdvertisementData.setShortName("Lego Hub");
  53. oAdvertisementData.setManufacturerData(ManufacturerData);
  54. pAdvertising->setAdvertisementData(oAdvertisementData);
  55.  
  56.   pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  57.   pAdvertising->setMinPreferred(0x12);
  58.  
  59.   BLEDevice::startAdvertising();
  60.   Serial.println("Characteristic defined! Now you can read it in your phone!");
  61. }
  62.  
  63. void loop() {
  64.   // put your main code here, to run repeatedly:
  65.   delay(2000);
  66. }

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

ruppie
Posts: 9
Joined: Sat Jan 20, 2018 9:34 am

Re: BLE Server, problems with custom advertisement and manufacturer data

Postby ruppie » Fri Jul 10, 2020 6:05 pm

An addition:
My Advertisment data is like:

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};

When i change code like:

oAdvertisementData.addData(advLEGO);
oAdvertisementData.setManufacturerData(ManufacturerData);
pAdvertising->setAdvertisementData(oAdvertisementData);



i got another strange result :

Scanning ...<CR><LF>
[ADV 1244] Packet received from BC:DD:C2:CB:69:FA<LF>
PAYLOAD 29 bytes<LF>
09-08-4C-65-67-6F-20-48-75-62-02-01-06-11-07-23-D1-BC-EA-5F-78-23-16-DE-EF-12-12-23-16<CR><LF>
RSSI -33 dBm<LF>
ADV TYPE Connectable undirected<CR><LF>
SHORT NAME Lego Hub<LF>
128-Bit UUID #Ѽê_x#<SYN>Þï<DC2><DC2>#<SYN>00001623-1212-EFDE-1623-785FEABCD123<CR><LF>
LEGO BLE SERVICE UUID Found!<LF><CR>
<LF>
Connected<CR><LF>
Discovering LEGO HUB Service ... Found LEGO HUB Service <CR><LF>


Question:
Where is the mistake not getting target result like;

Bluefruit52 Central LEGO Scan Example<CR><LF>
------------------------------------<LF><CR>
<LF>
Scanning ...<CR><LF>
[ADV 473] 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 -28 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>

Marc

chegewara
Posts: 1211
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE Server, problems with custom advertisement and manufacturer data

Postby chegewara » Mon Jul 13, 2020 3:24 am

Here is error:

Code: Select all

char  ManufacturerData[8] = {0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};
char string is 0x0 terminated.
std::string is not.

Here is simple test code:

Code: Select all

#include <string>

char  ManufacturerData[8] = {0x97,0x03,0x00,0x80,0x06,0x00,0x61,0x00};
std::string test(ManufacturerData, 8);
void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);
delay(100);
Serial.print("char length: ");
Serial.println(strlen(ManufacturerData));
Serial.print("std::string length: ");
Serial.println(test.length());
}

void loop() {
  // put your main code here, to run repeatedly:

}
Output:

Code: Select all

char length: 2
std::string length: 8

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests