esp-nimble-cpp / NimBLE-Arduino  1.3.1
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 "sdkconfig.h"
18 #if defined(CONFIG_BT_ENABLED)
19 
20 #include "nimconfig.h"
21 #if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
22 
23 #include "NimBLEAddress.h"
24 #include "NimBLEScan.h"
25 #include "NimBLEUUID.h"
26 
27 #include "host/ble_hs_adv.h"
28 
29 #include <map>
30 #include <vector>
31 
32 
33 class NimBLEScan;
41 public:
43 
45  uint8_t getAdvType();
46  uint16_t getAppearance();
47  uint16_t getAdvInterval();
48  uint16_t getMinInterval();
49  uint16_t getMaxInterval();
50  std::string getManufacturerData();
51  std::string getURI();
52 
61  template<typename T>
62  T getManufacturerData(bool skipSizeCheck = false) {
63  std::string data = getManufacturerData();
64  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
65  const char *pData = data.data();
66  return *((T *)pData);
67  }
68 
69  std::string getName();
70  int getRSSI();
72  size_t getServiceDataCount();
73  std::string getServiceData(uint8_t index = 0);
74  std::string getServiceData(const NimBLEUUID &uuid);
75 
85  template<typename T>
86  T getServiceData(uint8_t index = 0, bool skipSizeCheck = false) {
87  std::string data = getServiceData(index);
88  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
89  const char *pData = data.data();
90  return *((T *)pData);
91  }
92 
102  template<typename T>
103  T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck = false) {
104  std::string data = getServiceData(uuid);
105  if(!skipSizeCheck && data.size() < sizeof(T)) return T();
106  const char *pData = data.data();
107  return *((T *)pData);
108  }
109 
110  NimBLEUUID getServiceDataUUID(uint8_t index = 0);
111  NimBLEUUID getServiceUUID(uint8_t index = 0);
112  size_t getServiceUUIDCount();
113  NimBLEAddress getTargetAddress(uint8_t index = 0);
114  size_t getTargetAddressCount();
115  int8_t getTXPower();
116  uint8_t* getPayload();
117  uint8_t getAdvLength();
118  size_t getPayloadLength();
119  uint8_t getAddressType();
120  time_t getTimestamp();
121  bool isAdvertisingService(const NimBLEUUID &uuid);
122  bool haveAppearance();
123  bool haveManufacturerData();
124  bool haveName();
125  bool haveRSSI();
126  bool haveServiceData();
127  bool haveServiceUUID();
128  bool haveTXPower();
129  bool haveConnParams();
130  bool haveAdvInterval();
131  bool haveTargetAddress();
132  bool haveURI();
133  std::string toString();
134 
135 private:
136  friend class NimBLEScan;
137 
138  void setAddress(NimBLEAddress address);
139  void setAdvType(uint8_t advType);
140  void setPayload(const uint8_t *payload, uint8_t length, bool append);
141  void setRSSI(int rssi);
142  uint8_t findAdvField(uint8_t type, uint8_t index = 0, uint8_t *data_loc = nullptr);
143  uint8_t findServiceData(uint8_t index, uint8_t* bytes);
144 
145  NimBLEAddress m_address = NimBLEAddress("");
146  uint8_t m_advType;
147  int m_rssi;
148  time_t m_timestamp;
149  bool m_callbackSent;
150  uint8_t m_advLength;
151 
152  std::vector<uint8_t> m_payload;
153 };
154 
163 public:
164  virtual ~NimBLEAdvertisedDeviceCallbacks() {}
171  virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) = 0;
172 };
173 
174 #endif // #if defined( CONFIG_BT_NIMBLE_ROLE_CENTRAL)
175 #endif /* CONFIG_BT_ENABLED */
176 #endif /* COMPONENTS_NIMBLEADVERTISEDDEVICE_H_ */
A BLE device address.
Definition: NimBLEAddress.h:34
A callback handler for callbacks associated device scanning.
Definition: NimBLEAdvertisedDevice.h:162
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:40
bool haveTargetAddress()
Does the advertisement contain a target address?
Definition: NimBLEAdvertisedDevice.cpp:534
time_t getTimestamp()
Get the timeStamp of when the device last advertised.
Definition: NimBLEAdvertisedDevice.cpp:773
uint16_t getAppearance()
Get the appearance.
Definition: NimBLEAdvertisedDevice.cpp:73
bool haveConnParams()
Does this advertisement have preferred connection parameters?
Definition: NimBLEAdvertisedDevice.cpp:489
uint16_t getMinInterval()
Get the preferred min connection interval.
Definition: NimBLEAdvertisedDevice.cpp:109
bool isAdvertisingService(const NimBLEUUID &uuid)
Check advertised services for existance of the required UUID.
Definition: NimBLEAdvertisedDevice.cpp:455
uint16_t getMaxInterval()
Get the preferred max connection interval.
Definition: NimBLEAdvertisedDevice.cpp:127
NimBLEUUID getServiceDataUUID(uint8_t index=0)
Get the UUID of the serice data at the index.
Definition: NimBLEAdvertisedDevice.cpp:315
NimBLEAddress getTargetAddress(uint8_t index=0)
Get the target address at the index.
Definition: NimBLEAdvertisedDevice.cpp:234
bool haveServiceUUID()
Does this advertisement have a service UUID value?
Definition: NimBLEAdvertisedDevice.cpp:572
size_t getServiceDataCount()
Get the count of advertised service data UUIDS.
Definition: NimBLEAdvertisedDevice.cpp:371
bool haveRSSI()
Does this advertisement have a signal strength value?
Definition: NimBLEAdvertisedDevice.cpp:554
size_t getServiceUUIDCount()
Get the number of services advertised.
Definition: NimBLEAdvertisedDevice.cpp:436
bool haveURI()
Does this advertisement have a URI?
Definition: NimBLEAdvertisedDevice.cpp:525
bool haveName()
Does this advertisement have a name value?
Definition: NimBLEAdvertisedDevice.cpp:544
T getServiceData(uint8_t index=0, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:86
size_t getTargetAddressCount()
Get the number of target addresses.
Definition: NimBLEAdvertisedDevice.cpp:219
uint16_t getAdvInterval()
Get the advertisement interval.
Definition: NimBLEAdvertisedDevice.cpp:91
T getServiceData(const NimBLEUUID &uuid, bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:103
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:498
NimBLEUUID getServiceUUID(uint8_t index=0)
Get the Service UUID.
Definition: NimBLEAdvertisedDevice.cpp:387
NimBLEAdvertisedDevice()
Constructor.
Definition: NimBLEAdvertisedDevice.cpp:31
bool haveManufacturerData()
Does this advertisement have manufacturer data?
Definition: NimBLEAdvertisedDevice.cpp:516
std::string toString()
Create a string representation of this device.
Definition: NimBLEAdvertisedDevice.cpp:680
size_t getPayloadLength()
Get the length of the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:782
bool haveAppearance()
Does this advertisement have an appearance value?
Definition: NimBLEAdvertisedDevice.cpp:507
int getRSSI()
Get the RSSI.
Definition: NimBLEAdvertisedDevice.cpp:201
NimBLEScan * getScan()
Get the scan object that created this advertised device.
Definition: NimBLEAdvertisedDevice.cpp:210
std::string getURI()
Get the URI from the advertisement.
Definition: NimBLEAdvertisedDevice.cpp:163
std::string getName()
Get the advertised name.
Definition: NimBLEAdvertisedDevice.cpp:181
uint8_t getAdvLength()
Get the length of the advertisement data in the payload.
Definition: NimBLEAdvertisedDevice.cpp:751
T getManufacturerData(bool skipSizeCheck=false)
A template to convert the service data to <type>.
Definition: NimBLEAdvertisedDevice.h:62
std::string getServiceData(uint8_t index=0)
Get the service data.
Definition: NimBLEAdvertisedDevice.cpp:266
bool haveTXPower()
Does this advertisement have a transmission power value?
Definition: NimBLEAdvertisedDevice.cpp:581
std::string getManufacturerData()
Get the manufacturer data.
Definition: NimBLEAdvertisedDevice.cpp:145
uint8_t * getPayload()
Get the payload advertised by the device.
Definition: NimBLEAdvertisedDevice.cpp:726
bool haveServiceData()
Does this advertisement have a service data value?
Definition: NimBLEAdvertisedDevice.cpp:563
uint8_t getAddressType()
Get the advertised device address type.
Definition: NimBLEAdvertisedDevice.cpp:764
int8_t getTXPower()
Get the TX Power.
Definition: NimBLEAdvertisedDevice.cpp:471
Perform and manage BLE scans.
Definition: NimBLEScan.h:61
A model of a BLE UUID.
Definition: NimBLEUUID.h:31