esp-nimble-cpp  1.4.0
NimBLEScan.h
1 /*
2  * NimBLEScan.h
3  *
4  * Created: on Jan 24 2020
5  * Author H2zero
6  *
7  * Originally:
8  *
9  * BLEScan.h
10  *
11  * Created on: Jul 1, 2017
12  * Author: kolban
13  */
14 #ifndef COMPONENTS_NIMBLE_SCAN_H_
15 #define COMPONENTS_NIMBLE_SCAN_H_
16 
17 #include "nimconfig.h"
18 #if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
19 
20 #include "NimBLEAdvertisedDevice.h"
21 #include "NimBLEUtils.h"
22 
23 #if defined(CONFIG_NIMBLE_CPP_IDF)
24 #include "host/ble_gap.h"
25 #else
26 #include "nimble/nimble/host/include/host/ble_gap.h"
27 #endif
28 
29 #include <vector>
30 
31 class NimBLEDevice;
32 class NimBLEScan;
35 class NimBLEAddress;
36 
45 public:
46  void dump();
47  int getCount();
49  std::vector<NimBLEAdvertisedDevice*>::iterator begin();
50  std::vector<NimBLEAdvertisedDevice*>::iterator end();
52 
53 private:
54  friend NimBLEScan;
55  std::vector<NimBLEAdvertisedDevice*> m_advertisedDevicesVector;
56 };
57 
63 class NimBLEScan {
64 public:
65  bool start(uint32_t duration, void (*scanCompleteCB)(NimBLEScanResults), bool is_continue = false);
66  NimBLEScanResults start(uint32_t duration, bool is_continue = false);
67  bool isScanning();
68  void setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* pAdvertisedDeviceCallbacks, bool wantDuplicates = false);
69  void setActiveScan(bool active);
70  void setInterval(uint16_t intervalMSecs);
71  void setWindow(uint16_t windowMSecs);
72  void setDuplicateFilter(bool enabled);
73  void setLimitedOnly(bool enabled);
74  void setFilterPolicy(uint8_t filter);
75  void clearDuplicateCache();
76  bool stop();
77  void clearResults();
79  void setMaxResults(uint8_t maxResults);
80  void erase(const NimBLEAddress &address);
81 
82 
83 private:
84  friend class NimBLEDevice;
85 
86  NimBLEScan();
87  ~NimBLEScan();
88  static int handleGapEvent(ble_gap_event* event, void* arg);
89  void onHostReset();
90  void onHostSync();
91 
92  NimBLEAdvertisedDeviceCallbacks* m_pAdvertisedDeviceCallbacks = nullptr;
93  void (*m_scanCompleteCB)(NimBLEScanResults scanResults);
94  ble_gap_disc_params m_scan_params;
95  bool m_ignoreResults;
96  NimBLEScanResults m_scanResults;
97  uint32_t m_duration;
98  ble_task_data_t *m_pTaskData;
99  uint8_t m_maxResults;
100 };
101 
102 #endif /* CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER */
103 #endif /* COMPONENTS_NIMBLE_SCAN_H_ */
A BLE device address.
Definition: NimBLEAddress.h:39
A callback handler for callbacks associated device scanning.
Definition: NimBLEAdvertisedDevice.h:185
A representation of a BLE advertised device found by a scan.
Definition: NimBLEAdvertisedDevice.h:42
A model of a BLE Device from which all the BLE roles are created.
Definition: NimBLEDevice.h:96
Perform and manage BLE scans.
Definition: NimBLEScan.h:63
bool start(uint32_t duration, void(*scanCompleteCB)(NimBLEScanResults), bool is_continue=false)
Start scanning.
Definition: NimBLEScan.cpp:310
NimBLEScanResults getResults()
Get the results of the scan.
Definition: NimBLEScan.cpp:503
void setInterval(uint16_t intervalMSecs)
Set the interval to scan.
Definition: NimBLEScan.cpp:280
void setFilterPolicy(uint8_t filter)
Sets the scan filter policy.
Definition: NimBLEScan.cpp:249
void erase(const NimBLEAddress &address)
Delete peer device from the scan results vector.
Definition: NimBLEScan.cpp:466
bool stop()
Stop an in progress scan.
Definition: NimBLEScan.cpp:425
void clearDuplicateCache()
Clears the duplicate scan filter cache.
Definition: NimBLEScan.cpp:454
void setLimitedOnly(bool enabled)
Set whether or not the BLE controller only report scan results from devices advertising in limited di...
Definition: NimBLEScan.cpp:226
void setActiveScan(bool active)
Should we perform an active or passive scan? The default is a passive scan. An active scan means that...
Definition: NimBLEScan.cpp:204
void setMaxResults(uint8_t maxResults)
Sets the max number of results to store.
Definition: NimBLEScan.cpp:259
void setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks *pAdvertisedDeviceCallbacks, bool wantDuplicates=false)
Set the call backs to be invoked.
Definition: NimBLEScan.cpp:269
bool isScanning()
Get the status of the scanner.
Definition: NimBLEScan.cpp:298
void setWindow(uint16_t windowMSecs)
Set the window to actively scan.
Definition: NimBLEScan.cpp:289
void setDuplicateFilter(bool enabled)
Set whether or not the BLE controller should only report results from devices it has not already seen...
Definition: NimBLEScan.cpp:216
void clearResults()
Clear the results of the scan.
Definition: NimBLEScan.cpp:511
A class that contains and operates on the results of a BLE scan.
Definition: NimBLEScan.h:44
void dump()
Dump the scan results to the log.
Definition: NimBLEScan.cpp:523
std::vector< NimBLEAdvertisedDevice * >::iterator end()
Get iterator to the end of the vector of advertised device pointers.
Definition: NimBLEScan.cpp:564
NimBLEAdvertisedDevice getDevice(uint32_t i)
Return the specified device at the given index. The index should be between 0 and getCount()-1.
Definition: NimBLEScan.cpp:546
std::vector< NimBLEAdvertisedDevice * >::iterator begin()
Get iterator to the beginning of the vector of advertised device pointers.
Definition: NimBLEScan.cpp:555
int getCount()
Get the count of devices found in the last scan.
Definition: NimBLEScan.cpp:535