diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9bea4330 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.DS_Store diff --git a/src/BLEAdvertisingData.cpp b/src/BLEAdvertisingData.cpp index 80105916..f727c8d8 100644 --- a/src/BLEAdvertisingData.cpp +++ b/src/BLEAdvertisingData.cpp @@ -142,6 +142,11 @@ bool BLEAdvertisingData::setManufacturerData(const uint8_t manufacturerData[], i return success; } +const unsigned char* BLEAdvertisingData::getManufacturerData() +{ + return _manufacturerData; +} + bool BLEAdvertisingData::setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength) { int previousLength = 0; diff --git a/src/BLEAdvertisingData.h b/src/BLEAdvertisingData.h index dc736053..0ffff889 100644 --- a/src/BLEAdvertisingData.h +++ b/src/BLEAdvertisingData.h @@ -70,7 +70,8 @@ class BLEAdvertisingData { bool setRawData(const uint8_t* data, int length); bool setRawData(const BLEAdvertisingRawData& data); bool setFlags(uint8_t flags); - + const unsigned char* getManufacturerData(); + protected: friend class BLELocalDevice; bool updateData(); diff --git a/src/BLEDevice.cpp b/src/BLEDevice.cpp index fa806104..01569000 100644 --- a/src/BLEDevice.cpp +++ b/src/BLEDevice.cpp @@ -19,6 +19,7 @@ #include "utility/ATT.h" #include "utility/BLEUuid.h" +#include "utility/BLEManufacturerData.h" #include "utility/HCI.h" #include "remote/BLERemoteDevice.h" @@ -33,6 +34,18 @@ BLEDevice::BLEDevice() : memset(_address, 0x00, sizeof(_address)); } +int BLEDevice::getAdvertisement(uint8_t value[], int length) +{ + if (_eirDataLength > length) return 0; // Check that buffer size is sufficient + + if (_eirDataLength) { + memcpy(value, _eirData, _eirDataLength); + } + + return _eirDataLength; +} + + BLEDevice::BLEDevice(uint8_t addressType, uint8_t address[6]) : _addressType(addressType), _advertisementTypeMask(0), @@ -85,6 +98,11 @@ bool BLEDevice::hasLocalName() const return (localName().length() > 0); } +bool BLEDevice::hasManufacturerData() const +{ + return (manufacturerData().length() > 0); +} + bool BLEDevice::hasAdvertisedServiceUuid() const { return hasAdvertisedServiceUuid(0); @@ -146,6 +164,26 @@ String BLEDevice::localName() const return localName; } +String BLEDevice::manufacturerData() const +{ + String manufacturerData = ""; + + for (int i = 0; i < _eirDataLength;) { + int eirLength = _eirData[i++]; + int eirType = _eirData[i++]; + + if (eirType == 0xFF) { + manufacturerData = BLEManufacturerData::manufacturerDataToString(&_eirData[i], eirLength); + break; + } + + i += (eirLength - 1); + } + + return manufacturerData; +} + + String BLEDevice::advertisedServiceUuid() const { return advertisedServiceUuid(0); diff --git a/src/BLEDevice.h b/src/BLEDevice.h index cbe79c71..365408a6 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -50,15 +50,17 @@ class BLEDevice { virtual String address() const; bool hasLocalName() const; - + bool hasManufacturerData() const; + bool hasAdvertisedServiceUuid() const; bool hasAdvertisedServiceUuid(int index) const; int advertisedServiceUuidCount() const; String localName() const; + String manufacturerData() const; String advertisedServiceUuid() const; String advertisedServiceUuid(int index) const; - + int getAdvertisement(uint8_t value[], int length); virtual int rssi(); bool connect(); diff --git a/src/local/BLELocalDevice.cpp b/src/local/BLELocalDevice.cpp index 45d975a5..9e0928a6 100644 --- a/src/local/BLELocalDevice.cpp +++ b/src/local/BLELocalDevice.cpp @@ -204,6 +204,11 @@ bool BLELocalDevice::setManufacturerData(const uint8_t manufacturerData[], int m return _advertisingData.setManufacturerData(manufacturerData, manufacturerDataLength); } +const unsigned char* BLELocalDevice::getManufacturerData() +{ + return _advertisingData.getManufacturerData(); +} + bool BLELocalDevice::setManufacturerData(const uint16_t companyId, const uint8_t manufacturerData[], int manufacturerDataLength) { return _advertisingData.setManufacturerData(companyId, manufacturerData, manufacturerDataLength); diff --git a/src/local/BLELocalDevice.h b/src/local/BLELocalDevice.h index 9ebb0bc2..4af4cd2a 100644 --- a/src/local/BLELocalDevice.h +++ b/src/local/BLELocalDevice.h @@ -80,7 +80,8 @@ class BLELocalDevice { virtual void debug(Stream& stream); virtual void noDebug(); - + const unsigned char* getManufacturerData(); + protected: virtual BLEAdvertisingData& getAdvertisingData(); virtual BLEAdvertisingData& getScanResponseData(); diff --git a/src/utility/BLEManufacturerData.cpp b/src/utility/BLEManufacturerData.cpp new file mode 100644 index 00000000..3b2bf999 --- /dev/null +++ b/src/utility/BLEManufacturerData.cpp @@ -0,0 +1,37 @@ +/* + 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 "BLEManufacturerData.h" + +const char* BLEManufacturerData::manufacturerDataToString(const uint8_t* data, uint8_t length) +{ + static char manufacturerData[32 * 2 + 1]; + char* c = manufacturerData; + + 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 manufacturerData; +} \ No newline at end of file diff --git a/src/utility/BLEManufacturerData.h b/src/utility/BLEManufacturerData.h new file mode 100644 index 00000000..ed196364 --- /dev/null +++ b/src/utility/BLEManufacturerData.h @@ -0,0 +1,28 @@ +/* + 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_MANUFACTURER_DATA_H_ +#define _BLE_MANUFACTURER_DATA_H_ + +#include + +class BLEManufacturerData +{ +public: + static const char* manufacturerDataToString(const uint8_t* data, uint8_t length); +}; + +#endif \ No newline at end of file