esp-nimble-cpp 2.0.2
Loading...
Searching...
No Matches
NimBLEScan.h
1/*
2 * Copyright 2020-2024 Ryan Powell <ryan@nable-embedded.io> and
3 * esp-nimble-cpp, NimBLE-Arduino contributors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef NIMBLE_CPP_SCAN_H_
19#define NIMBLE_CPP_SCAN_H_
20
21#include "nimconfig.h"
22#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
23
24# include "NimBLEAdvertisedDevice.h"
25# include "NimBLEUtils.h"
26
27# if defined(CONFIG_NIMBLE_CPP_IDF)
28# include "host/ble_gap.h"
29# else
30# include "nimble/nimble/host/include/host/ble_gap.h"
31# endif
32
33# include <vector>
34
35class NimBLEDevice;
36class NimBLEScan;
39class NimBLEAddress;
40
49 public:
50 void dump() const;
51 int getCount() const;
52 const NimBLEAdvertisedDevice* getDevice(uint32_t idx) const;
53 const NimBLEAdvertisedDevice* getDevice(const NimBLEAddress& address) const;
54 std::vector<NimBLEAdvertisedDevice*>::const_iterator begin() const;
55 std::vector<NimBLEAdvertisedDevice*>::const_iterator end() const;
56
57 private:
58 friend NimBLEScan;
59 std::vector<NimBLEAdvertisedDevice*> m_deviceVec;
60};
61
68 public:
69 bool start(uint32_t duration, bool isContinue = false, bool restart = true);
70 bool isScanning();
71 void setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool wantDuplicates = false);
72 void setActiveScan(bool active);
73 void setInterval(uint16_t intervalMs);
74 void setWindow(uint16_t windowMs);
75 void setDuplicateFilter(uint8_t enabled);
76 void setLimitedOnly(bool enabled);
77 void setFilterPolicy(uint8_t filter);
78 bool stop();
79 void clearResults();
81 NimBLEScanResults getResults(uint32_t duration, bool is_continue = false);
82 void setMaxResults(uint8_t maxResults);
83 void erase(const NimBLEAddress& address);
84 void erase(const NimBLEAdvertisedDevice* device);
85
86# if CONFIG_BT_NIMBLE_EXT_ADV
87 enum Phy { SCAN_1M = 0x01, SCAN_CODED = 0x02, SCAN_ALL = 0x03 };
88 void setPhy(Phy phyMask);
89 void setPeriod(uint32_t periodMs);
90# endif
91
92 private:
93 friend class NimBLEDevice;
94
95 NimBLEScan();
97 static int handleGapEvent(ble_gap_event* event, void* arg);
98 void onHostSync();
99
100 NimBLEScanCallbacks* m_pScanCallbacks;
101 ble_gap_disc_params m_scanParams;
102 NimBLEScanResults m_scanResults;
103 NimBLETaskData* m_pTaskData;
104 uint8_t m_maxResults;
105
106# if CONFIG_BT_NIMBLE_EXT_ADV
107 uint8_t m_phy{SCAN_ALL};
108 uint16_t m_period{0};
109# endif
110};
111
116 public:
117 virtual ~NimBLEScanCallbacks() {}
118
123 virtual void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice);
124
129 virtual void onResult(const NimBLEAdvertisedDevice* advertisedDevice);
130
136 virtual void onScanEnd(const NimBLEScanResults& scanResults, int reason);
137};
138
139#endif // CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER
140#endif // NIMBLE_CPP_SCAN_H_
A BLE device address.
Definition NimBLEAddress.h:41
A representation of a BLE advertised device found by a scan.
Definition NimBLEAdvertisedDevice.h:45
A model of a BLE Device from which all the BLE roles are created.
Definition NimBLEDevice.h:109
A callback handler for callbacks associated device scanning.
Definition NimBLEScan.h:115
virtual void onDiscovered(const NimBLEAdvertisedDevice *advertisedDevice)
Called when a new device is discovered, before the scan result is received (if applicable).
Definition NimBLEScan.cpp:538
virtual void onScanEnd(const NimBLEScanResults &scanResults, int reason)
Called when a scan operation ends.
Definition NimBLEScan.cpp:546
virtual void onResult(const NimBLEAdvertisedDevice *advertisedDevice)
Called when a new scan result is complete, including scan response data (if applicable).
Definition NimBLEScan.cpp:542
Perform and manage BLE scans.
Definition NimBLEScan.h:67
void setWindow(uint16_t windowMs)
Set the window to actively scan.
Definition NimBLEScan.cpp:257
void setPhy(Phy phyMask)
Set the PHYs to scan.
Definition NimBLEScan.cpp:276
NimBLEScanResults getResults()
Get the results of the scan.
Definition NimBLEScan.cpp:463
void setScanCallbacks(NimBLEScanCallbacks *pScanCallbacks, bool wantDuplicates=false)
Set the call backs to be invoked.
Definition NimBLEScan.cpp:234
void setFilterPolicy(uint8_t filter)
Sets the scan filter policy.
Definition NimBLEScan.cpp:216
void setInterval(uint16_t intervalMs)
Set the interval to scan.
Definition NimBLEScan.cpp:249
void erase(const NimBLEAddress &address)
Delete peer device from the scan results vector.
Definition NimBLEScan.cpp:398
bool stop()
Stop an in progress scan.
Definition NimBLEScan.cpp:373
void setLimitedOnly(bool enabled)
Set whether or not the BLE controller only reports scan results from devices advertising in limited d...
Definition NimBLEScan.cpp:194
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:171
void setPeriod(uint32_t periodMs)
Set the extended scanning period.
Definition NimBLEScan.cpp:287
void setDuplicateFilter(uint8_t enabled)
Set whether or not the BLE controller should only report results from devices it has not already seen...
Definition NimBLEScan.cpp:185
void setMaxResults(uint8_t maxResults)
Sets the max number of results to store.
Definition NimBLEScan.cpp:225
bool isScanning()
Get the status of the scanner.
Definition NimBLEScan.cpp:265
bool start(uint32_t duration, bool isContinue=false, bool restart=true)
Start scanning.
Definition NimBLEScan.cpp:300
void clearResults()
Clear the stored results of the scan.
Definition NimBLEScan.cpp:470
A class that contains and operates on the results of a BLE scan.
Definition NimBLEScan.h:48
std::vector< NimBLEAdvertisedDevice * >::const_iterator end() const
Get iterator to the end of the vector of advertised device pointers.
Definition NimBLEScan.cpp:516
int getCount() const
Get the count of devices found in the last scan.
Definition NimBLEScan.cpp:490
std::vector< NimBLEAdvertisedDevice * >::const_iterator begin() const
Get iterator to the beginning of the vector of advertised device pointers.
Definition NimBLEScan.cpp:508
const NimBLEAdvertisedDevice * getDevice(uint32_t idx) const
Return the specified device at the given index. The index should be between 0 and getCount()-1.
Definition NimBLEScan.cpp:500
void dump() const
Dump the scan results to the log.
Definition NimBLEScan.cpp:480
A structure to hold data for a task that is waiting for a response.
Definition NimBLEUtils.h:32