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:

Code: Untitled.c Select all



*
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

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: 2505
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: No registered users and 2 guests