diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index fa806104..ca11f3b3 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -19,6 +19,7 @@ #include "utility/ATT.h" #include "utility/BLEUuid.h" +#include "utility/BLEServiceData.h" #include "utility/HCI.h" #include "remote/BLERemoteDevice.h" @@ -85,6 +86,11 @@ bool BLEDevice::hasLocalName() const return (localName().length() > 0); } +bool BLEDevice::hasServiceData() const +{ + return (serviceData().length() > 0); +} + bool BLEDevice::hasAdvertisedServiceUuid() const { return hasAdvertisedServiceUuid(0); @@ -146,6 +152,27 @@ String BLEDevice::localName() const return localName; } +String BLEDevice::serviceData() const +{ + String serviceData = ""; + + for (int i = 0; i < _eirDataLength;) { + int eirLength = _eirData[i++]; + int eirType = _eirData[i++]; + + //Serial.println(eirType); + + if (eirType == 0x16) { + serviceData = BLEServiceData::serviceDataToString(&_eirData[i], eirLength); + break; + } + + i += (eirLength - 1); + } + + return serviceData; +} + String BLEDevice::advertisedServiceUuid() const { return advertisedServiceUuid(0); diff --git a/src/BLEDevice.h b/src/BLEDevice.h index cbe79c71..486f604d 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -50,12 +50,14 @@ class BLEDevice { virtual String address() const; bool hasLocalName() const; - + bool hasServiceData() const; + bool hasAdvertisedServiceUuid() const; bool hasAdvertisedServiceUuid(int index) const; int advertisedServiceUuidCount() const; String localName() const; + String serviceData() const; String advertisedServiceUuid() const; String advertisedServiceUuid(int index) const; diff --git a/src/utility/BLEServiceData.cpp b/src/utility/BLEServiceData.cpp new file mode 100644 index 00000000..2828c46b --- /dev/null +++ b/src/utility/BLEServiceData.cpp @@ -0,0 +1,40 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include +#include + +#include "BLEServiceData.h" + +const char* BLEServiceData::serviceDataToString(const uint8_t* data, uint8_t length) +{ + static char serviceData[32 * 2 + 1]; + char* c = serviceData; + + for (int i = 0; i < length - 1; i++) { + uint8_t b = data[i]; + + utoa(b >> 4, c++, 16); + utoa(b & 0x0f, c++, 16); + } + + *c = '\0'; + + return serviceData; +} diff --git a/src/utility/BLEServiceData.h b/src/utility/BLEServiceData.h new file mode 100644 index 00000000..da0d5f68 --- /dev/null +++ b/src/utility/BLEServiceData.h @@ -0,0 +1,31 @@ +/* + This file is part of the ArduinoBLE library. + Copyright (c) 2018 Arduino SA. All rights reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _BLE_SERVICE_DATA_H_ +#define _BLE_SERVICE_DATA_H_ + +#include + +class BLEServiceData +{ +public: + static const char* serviceDataToString(const uint8_t* data, uint8_t length); +}; + +#endif