NimBLE-Arduino 1.4.2
Loading...
Searching...
No Matches
NimBLEAdvertisedDevice.h
1/*
2 * NimBLEAdvertisedDevice.h
3 *
4 * Created: on Jan 24 2020
5 * Author H2zero
6 *
7 * Originally:
8 *
9 * BLEAdvertisedDevice.h
10 *
11 * Created on: Jul 3, 2017
12 * Author: kolban
13 */
14
15#ifndef COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
16#define COMPONENTS_NIMBLEADVERTISEDDEVICE_H_
17#include "nimconfig.h"
18#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
19
20#include "NimBLEAddress.h"
21#include "NimBLEScan.h"
22#include "NimBLEUUID.h"
23
24#if defined(CONFIG_NIMBLE_CPP_IDF)
25#include "host/ble_hs_adv.h"
26#else
27#include "nimble/nimble/host/include/host/ble_hs_adv.h"
28#endif
29
30#include <map>
31#include <vector>
32#include <time.h>
33
34
35class NimBLEScan;
43public:
45
47 uint8_t getAdvType();
48 uint8_t getAdvFlags();
49 uint16_t getAppearance();
50 uint16_t getAdvInterval();
51 uint16_t getMinInterval();
52 uint16_t getMaxInterval();
54 std::string getManufacturerData(uint8_t index = 0);
55 std::string getURI();
56 std::string getPayloadByType(uint16_t type);
57
66 template<typename T>
67 T getManufacturerData(bool skipSizeCheck = false) {
68 std::string data = getManufacturerData();
69 if(!skipSizeCheck && data.size() < sizeof(T)) return T();
70 const char *pData = data.data();
71 return *((T *)pData);
72 }
73
74 std::string getName();
75 int getRSSI();
77 uint8_t getServiceDataCount();
78 std::string getServiceData(uint8_t index = 0);
79 std::string getServiceData(const NimBLEUUID &uuid);
80
90 template<typename T>
91 T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) {
92 std::string data = getServiceData(index);
93 if(!skipSizeCheck && data.size() < sizeof(T)) return T();
94 const char *pData = data.data();
95 return *((T *)pData);
96 }
97
107 template<typename T>
108 T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck = false) {
109 std::string data = getServiceData(uuid);
110 if(!skipSizeCheck && data.size() < sizeof(T)) return T();
111 const char *pData = data.data();
112 return *((T *)pData);
113 }
114
115 NimBLEUUID getServiceDataUUID(uint8_t index = 0);
116 NimBLEUUID getServiceUUID(uint8_t index = 0);
117 uint8_t getServiceUUIDCount();
118 NimBLEAddress getTargetAddress(uint8_t index = 0);
119 uint8_t getTargetAddressCount();
120 int8_t getTXPower();
121 uint8_t* getPayload();
122 uint8_t getAdvLength();
123 size_t getPayloadLength();
124 uint8_t getAddressType();
125 time_t getTimestamp();
126 bool isAdvertisingService(const NimBLEUUID &uuid);
127 bool haveAppearance();
129 bool haveName();
130 bool haveRSSI();
131 bool haveServiceData();
132 bool haveServiceUUID();
133 bool haveTXPower();
134 bool haveConnParams();
135 bool haveAdvInterval();
136 bool haveTargetAddress();
137 bool haveURI();
138 bool haveType(uint16_t type);
139 std::string toString();
140 bool isConnectable();
142#if CONFIG_BT_NIMBLE_EXT_ADV
143 uint8_t getSetId();
144 uint8_t getPrimaryPhy();
145 uint8_t getSecondaryPhy();
146 uint16_t getPeriodicInterval();
147#endif
148
149private:
150 friend class NimBLEScan;
151
152 void setAddress(NimBLEAddress address);
153 void setAdvType(uint8_t advType, bool isLegacyAdv);
154 void setPayload(const uint8_t *payload, uint8_t length, bool append);
155 void setRSSI(int rssi);
156#if CONFIG_BT_NIMBLE_EXT_ADV
157 void setSetId(uint8_t sid) { m_sid = sid; }
158 void setPrimaryPhy(uint8_t phy) { m_primPhy = phy; }
159 void setSecondaryPhy(uint8_t phy) { m_secPhy = phy; }
160 void setPeriodicInterval(uint16_t itvl) { m_periodicItvl = itvl; }
161#endif
162 uint8_t findAdvField(uint8_t type, uint8_t index = 0, size_t * data_loc = nullptr);
163 size_t findServiceData(uint8_t index, uint8_t* bytes);
164
165 NimBLEAddress m_address = NimBLEAddress("");
166 uint8_t m_advType;
167 int m_rssi;
168 time_t m_timestamp;
169 bool m_callbackSent;
170 uint8_t m_advLength;
171#if CONFIG_BT_NIMBLE_EXT_ADV
172 bool m_isLegacyAdv;
173 uint8_t m_sid;
174 uint8_t m_primPhy;
175 uint8_t m_secPhy;
176 uint16_t m_periodicItvl;
177#endif
178
179 std::vector<uint8_t> m_payload;
180};
181
190public:
198 virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
199};
200
201#endif /* CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_OBSERVER */
202#endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
A callback handler for callbacks associated device scanning.
Definition: NimBLEAdvertisedDevice.h:189
virtual void onResult(NimBLEAdvertisedDevice *advertisedDevice)=0
Called when a new scan result is detected.
A representation of a BLE advertised device found by a scan.
Definition: NimBLEAdvertisedDevice.h:42
bool haveTargetAddress()
Does the advertisement contain a target address?
Definition: NimBLEAdvertisedDevice.cpp:590
uint8_t getServiceDataCount()
Get the count of advertised service data UUIDS.
Definition: NimBLEAdvertisedDevice.cpp:419
time_t getTimestamp()
Get the timeStamp of when the device last advertised.
Definition: NimBLEAdvertisedDevice.cpp:877
uint8_t getSecondaryPhy()
Get the primary PHY used by this advertisement.
Definition: NimBLEAdvertisedDevice.cpp:670
uint16_t getAppearance()
Get the appearance.
Definition: NimBLEAdvertisedDevice.cpp:92
bool haveConnParams()
Does this advertisement have preferred connection parameters?
Definition: NimBLEAdvertisedDevice.cpp:537
uint16_t getMinInterval()
Get the preferred min connection interval.
Definition: NimBLEAdvertisedDevice.cpp:128
bool isLegacyAdvertisement()
Check if this advertisement is a legacy or extended type.
Definition: NimBLEAdvertisedDevice.cpp:911
bool isAdvertisingService(const NimBLEUUID &uuid)
Check advertised services for existence of the required UUID.
Definition: NimBLEAdvertisedDevice.cpp:503
uint16_t getMaxInterval()
Get the preferred max connection interval.
Definition: NimBLEAdvertisedDevice.cpp:146
NimBLEUUID getServiceDataUUID(uint8_t index=0)
Get the UUID of the service data at the index.
Definition: NimBLEAdvertisedDevice.cpp:363
bool isConnectable()
Check if this device is advertising as connectable.
Definition: NimBLEAdvertisedDevice.cpp:895
NimBLEAddress getTargetAddress(uint8_t index=0)
Get the target address at the index.
Definition: NimBLEAdvertisedDevice.cpp:282
bool haveServiceUUID()
Does this advertisement have a service UUID value?
Definition: NimBLEAdvertisedDevice.cpp:628
uint8_t getServiceUUIDCount()
Get the number of services advertised.
Definition: NimBLEAdvertisedDevice.cpp:484
bool haveRSSI()
Does this advertisement have a signal strength value?
Definition: NimBLEAdvertisedDevice.cpp:610
uint8_t getSetId()
Get the set ID of the extended advertisement.
Definition: NimBLEAdvertisedDevice.cpp:647
bool haveURI()
Does this advertisement have a URI?
Definition: NimBLEAdvertisedDevice.cpp:573
bool haveType(uint16_t type)
Does this advertisement have a adv type type?
Definition: NimBLEAdvertisedDevice.cpp:581
bool haveName()
Does this advertisement have a name value?
Definition: NimBLEAdvertisedDevice.cpp:600
uint8_t getAdvFlags()
Get the advertisement flags.
Definition: NimBLEAdvertisedDevice.cpp:72
T getServiceData(uint8_t index=0, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:91
std::string getPayloadByType(uint16_t type)
Get the data from any type available in the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:211
uint16_t getAdvInterval()
Get the advertisement interval.
Definition: NimBLEAdvertisedDevice.cpp:110
uint8_t getPrimaryPhy()
Get the primary PHY used by this advertisement.
Definition: NimBLEAdvertisedDevice.cpp:658
T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:108
uint8_t getAdvType()
Get the advertisement type.
Definition: NimBLEAdvertisedDevice.cpp:60
NimBLEAddress getAddress()
Get the address of the advertising device.
Definition: NimBLEAdvertisedDevice.cpp:46
bool haveAdvInterval()
Does this advertisement have have the advertising interval?
Definition: NimBLEAdvertisedDevice.cpp:546
std::string getManufacturerData(uint8_t index=0)
Get the manufacturer data.
Definition: NimBLEAdvertisedDevice.cpp:165
NimBLEUUID getServiceUUID(uint8_t index=0)
Get the Service UUID.
Definition: NimBLEAdvertisedDevice.cpp:435
NimBLEAdvertisedDevice()
Constructor.
Definition: NimBLEAdvertisedDevice.cpp:31
bool haveManufacturerData()
Does this advertisement have manufacturer data?
Definition: NimBLEAdvertisedDevice.cpp:564
std::string toString()
Create a string representation of this device.
Definition: NimBLEAdvertisedDevice.cpp:784
size_t getPayloadLength()
Get the length of the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:886
bool haveAppearance()
Does this advertisement have an appearance value?
Definition: NimBLEAdvertisedDevice.cpp:555
int getRSSI()
Get the RSSI.
Definition: NimBLEAdvertisedDevice.cpp:249
NimBLEScan * getScan()
Get the scan object that created this advertised device.
Definition: NimBLEAdvertisedDevice.cpp:258
std::string getURI()
Get the URI from the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:193
uint16_t getPeriodicInterval()
Get the periodic interval of the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:679
uint8_t getTargetAddressCount()
Get the number of target addresses.
Definition: NimBLEAdvertisedDevice.cpp:267
std::string getName()
Get the advertised name.
Definition: NimBLEAdvertisedDevice.cpp:229
uint8_t getAdvLength()
Get the length of the advertisement data in the payload.
Definition: NimBLEAdvertisedDevice.cpp:855
T getManufacturerData(bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:67
uint8_t getManufacturerDataCount()
Get the count of manufacturer data sets.
Definition: NimBLEAdvertisedDevice.cpp:184
std::string getServiceData(uint8_t index=0)
Get the service data.
Definition: NimBLEAdvertisedDevice.cpp:314
bool haveTXPower()
Does this advertisement have a transmission power value?
Definition: NimBLEAdvertisedDevice.cpp:637
uint8_t * getPayload()
Get the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:830
bool haveServiceData()
Does this advertisement have a service data value?
Definition: NimBLEAdvertisedDevice.cpp:619
uint8_t getAddressType()
Get the advertised device address type.
Definition: NimBLEAdvertisedDevice.cpp:868
int8_t getTXPower()
Get the TX Power.
Definition: NimBLEAdvertisedDevice.cpp:519
Perform and manage BLE scans.
Definition: NimBLEScan.h:63
A model of a BLE UUID.
Definition: NimBLEUUID.h:37