From 55d5bf2c0dea092b8efbec9b444432a68a86c4f3 Mon Sep 17 00:00:00 2001 From: Tim Lossen Date: Sat, 26 Dec 2020 22:05:11 +0100 Subject: [PATCH 1/4] add access to raw advertisement data --- src/BLEDevice.cpp | 11 +++++++++++ src/BLEDevice.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index fa806104..4fb2562d 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -184,6 +184,17 @@ String BLEDevice::advertisedServiceUuid(int index) const return serviceUuid; } +int BLEDevice::advertisementData(uint8_t value[], int length) +{ + if (_eirDataLength > length) return 0; // Check that buffer size is sufficient + + if (_eirDataLength) { + memcpy(value, _eirData, _eirDataLength); + } + + return _eirDataLength; +} + int BLEDevice::rssi() { uint16_t handle = ATT.connectionHandle(_addressType, _address); diff --git a/src/BLEDevice.h b/src/BLEDevice.h index cbe79c71..508d46c1 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -59,6 +59,8 @@ class BLEDevice { String advertisedServiceUuid() const; String advertisedServiceUuid(int index) const; + int advertisementData(uint8_t value[], int length); + virtual int rssi(); bool connect(); From a9550cf9a27334db6e71cbddc6be0348274bd541 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Tue, 20 Apr 2021 17:24:22 +0200 Subject: [PATCH 2/4] Saturate length of retrieved advertisement data --- src/BLEDevice.cpp | 10 +++++----- src/BLEDevice.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index 4fb2562d..4ac0785f 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -184,15 +184,15 @@ String BLEDevice::advertisedServiceUuid(int index) const return serviceUuid; } -int BLEDevice::advertisementData(uint8_t value[], int length) +int BLEDevice::advertisementData(uint8_t value[], int length) const { - if (_eirDataLength > length) return 0; // Check that buffer size is sufficient + if (length > _eirDataLength) length = _eirDataLength; - if (_eirDataLength) { - memcpy(value, _eirData, _eirDataLength); + if (length) { + memcpy(value, _eirData, length); } - return _eirDataLength; + return length; } int BLEDevice::rssi() diff --git a/src/BLEDevice.h b/src/BLEDevice.h index 508d46c1..84fb8f15 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -59,7 +59,7 @@ class BLEDevice { String advertisedServiceUuid() const; String advertisedServiceUuid(int index) const; - int advertisementData(uint8_t value[], int length); + int advertisementData(uint8_t value[], int length) const; virtual int rssi(); From bd492be549e05f48679d4fa797f99e1370bb7eaa Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Tue, 20 Apr 2021 17:34:37 +0200 Subject: [PATCH 3/4] Add advertisement data auxiliary functions --- src/BLEDevice.cpp | 10 ++++++++++ src/BLEDevice.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index 4ac0785f..31fae719 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -184,6 +184,16 @@ String BLEDevice::advertisedServiceUuid(int index) const return serviceUuid; } +bool BLEDevice::hasAdvertisementData() const +{ + return (_eirDataLength > 0); +} + +int BLEDevice::advertisementDataLength() const +{ + return _eirDataLength; +} + int BLEDevice::advertisementData(uint8_t value[], int length) const { if (length > _eirDataLength) length = _eirDataLength; diff --git a/src/BLEDevice.h b/src/BLEDevice.h index 84fb8f15..054c49bc 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -59,6 +59,8 @@ class BLEDevice { String advertisedServiceUuid() const; String advertisedServiceUuid(int index) const; + bool hasAdvertisementData() const; + int advertisementDataLength() const; int advertisementData(uint8_t value[], int length) const; virtual int rssi(); From e080601da73f5ec8cf8a6d999783c0c1c5593945 Mon Sep 17 00:00:00 2001 From: Paolo Calao Date: Tue, 20 Apr 2021 17:53:05 +0200 Subject: [PATCH 4/4] Add retrieval of manufacturer data --- src/BLEDevice.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/BLEDevice.h | 4 ++++ 2 files changed, 47 insertions(+) diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index 31fae719..5f64c1d6 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -205,6 +205,49 @@ int BLEDevice::advertisementData(uint8_t value[], int length) const return length; } +bool BLEDevice::hasManufacturerData() const +{ + return (manufacturerDataLength() > 0); +} + +int BLEDevice::manufacturerDataLength() const +{ + int length = 0; + + for (int i = 0; i < _eirDataLength;) { + int eirLength = _eirData[i++]; + int eirType = _eirData[i++]; + + if (eirType == 0xFF) { + length = (eirLength - 1); + break; + } + + i += (eirLength - 1); + } + + return length; +} + +int BLEDevice::manufacturerData(uint8_t value[], int length) const +{ + for (int i = 0; i < _eirDataLength;) { + int eirLength = _eirData[i++]; + int eirType = _eirData[i++]; + + if (eirType == 0xFF) { + if (length > (eirLength - 1)) length = (eirLength - 1); + + memcpy(value, &_eirData[i], length); + break; + } + + i += (eirLength - 1); + } + + return length; +} + int BLEDevice::rssi() { uint16_t handle = ATT.connectionHandle(_addressType, _address); diff --git a/src/BLEDevice.h b/src/BLEDevice.h index 054c49bc..bf710744 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -63,6 +63,10 @@ class BLEDevice { int advertisementDataLength() const; int advertisementData(uint8_t value[], int length) const; + bool hasManufacturerData() const; + int manufacturerDataLength() const; + int manufacturerData(uint8_t value[], int length) const; + virtual int rssi(); bool connect();