Skip to content

Commit

Permalink
Add PR Add BLEDevice.manufacturerData() arduino-libraries#53 from htt…
Browse files Browse the repository at this point in the history
…ps://github.com/makisin and my getAdvertisementData
  • Loading branch information
IanBUK committed May 9, 2021
1 parent 94ffe05 commit a45a0d3
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/BLEAdvertisingData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/BLEAdvertisingData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 38 additions & 0 deletions src/BLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "utility/ATT.h"
#include "utility/BLEUuid.h"
#include "utility/BLEManufacturerData.h"
#include "utility/HCI.h"

#include "remote/BLERemoteDevice.h"
Expand All @@ -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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -300,7 +305,7 @@ BLEDevice BLELocalDevice::central()
BLEDevice BLELocalDevice::available()
{
HCI.poll();

Serial.println("BLELocalDevice");
return GAP.available();
}

Expand Down
3 changes: 2 additions & 1 deletion src/local/BLELocalDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
37 changes: 37 additions & 0 deletions src/utility/BLEManufacturerData.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <string.h>

#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;
}
28 changes: 28 additions & 0 deletions src/utility/BLEManufacturerData.h
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>

class BLEManufacturerData
{
public:
static const char* manufacturerDataToString(const uint8_t* data, uint8_t length);
};

#endif

0 comments on commit a45a0d3

Please sign in to comment.