2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEScan.h
|
|
|
|
*
|
|
|
|
* Created: on Jan 24 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEScan.h
|
|
|
|
*
|
|
|
|
* Created on: Jul 1, 2017
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
|
|
|
#ifndef COMPONENTS_NIMBLE_SCAN_H_
|
|
|
|
#define COMPONENTS_NIMBLE_SCAN_H_
|
|
|
|
|
2020-05-14 06:03:56 +02:00
|
|
|
#include "nimconfig.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEAdvertisedDevice.h"
|
2020-06-22 06:07:01 +02:00
|
|
|
#include "NimBLEUtils.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "host/ble_gap.h"
|
2021-09-07 05:14:43 +02:00
|
|
|
#else
|
|
|
|
#include "nimble/nimble/host/include/host/ble_gap.h"
|
|
|
|
#endif
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-05-18 04:21:35 +02:00
|
|
|
#include <vector>
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
class NimBLEDevice;
|
|
|
|
class NimBLEScan;
|
|
|
|
class NimBLEAdvertisedDevice;
|
2022-08-27 21:13:27 +02:00
|
|
|
class NimBLEScanCallbacks;
|
2020-05-18 04:21:35 +02:00
|
|
|
class NimBLEAddress;
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief A class that contains and operates on the results of a BLE scan.
|
|
|
|
* @details When a scan completes, we have a set of found devices. Each device is described
|
|
|
|
* by a NimBLEAdvertisedDevice object. The number of items in the set is given by
|
2020-03-30 01:44:20 +02:00
|
|
|
* getCount(). We can retrieve a device by calling getDevice() passing in the
|
|
|
|
* index (starting at 0) of the desired device.
|
|
|
|
*/
|
|
|
|
class NimBLEScanResults {
|
|
|
|
public:
|
Add iterators to client remote attributes.
Add iterators for NimBLEScan: NimBLEadvertisedDevice, NimBLEClient: NimBLERemoteService, NimBLERemoteService: NimBLERemoteCharacteristic and NimBLERemoteCharacteristic: NimBLERemoteDescriptor
This is handy e.g. for showing every address of the advertised devices from a scan. To do so, first get a new scan and next:
```
for(auto pAdvertisedDevice: pBLEScan->getResults()) {
Serial.printf("Address is %s\n", std::string(pAdvertisedDevice->getAddress()).c_str());
}
```
Of course any other property of the advertised device can be shown (or looked up, if that is your use case)
Also this is handy e.g. for showing every UUID in a peripheral. To do so, first connect to a peripheral and next:
```
for(auto pService: *pClient) {
Serial.printf("Service UUID is %s\n", std::string(pService->getUUID()).c_str());
for(auto pCharacteristic: *pService) {
Serial.printf("Characteristic UUID is %s\n", std::string(pCharacteristic->getUUID()).c_str());
for(auto pDescriptor: *pCharacteristic) {
Serial.printf("Descriptor UUID is %s\n", std::string(pDescriptor->getUUID()).c_str());
}
}
}
```
Again of course any other property can be shown, or looked up.
2020-05-23 04:13:52 +02:00
|
|
|
void dump();
|
|
|
|
int getCount();
|
|
|
|
NimBLEAdvertisedDevice getDevice(uint32_t i);
|
|
|
|
std::vector<NimBLEAdvertisedDevice*>::iterator begin();
|
|
|
|
std::vector<NimBLEAdvertisedDevice*>::iterator end();
|
|
|
|
NimBLEAdvertisedDevice *getDevice(const NimBLEAddress &address);
|
2020-03-30 01:44:20 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend NimBLEScan;
|
2020-05-18 04:21:35 +02:00
|
|
|
std::vector<NimBLEAdvertisedDevice*> m_advertisedDevicesVector;
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Perform and manage %BLE scans.
|
|
|
|
*
|
|
|
|
* Scanning is associated with a %BLE client that is attempting to locate BLE servers.
|
|
|
|
*/
|
|
|
|
class NimBLEScan {
|
|
|
|
public:
|
2022-08-27 21:13:27 +02:00
|
|
|
bool start(uint32_t duration, bool is_continue = false);
|
2020-07-24 04:18:41 +02:00
|
|
|
bool isScanning();
|
2022-08-27 21:13:27 +02:00
|
|
|
void setScanCallbacks(NimBLEScanCallbacks* pScanCallbacks, bool wantDuplicates = false);
|
2020-03-30 01:44:20 +02:00
|
|
|
void setActiveScan(bool active);
|
|
|
|
void setInterval(uint16_t intervalMSecs);
|
|
|
|
void setWindow(uint16_t windowMSecs);
|
2020-07-24 04:18:41 +02:00
|
|
|
void setDuplicateFilter(bool enabled);
|
|
|
|
void setLimitedOnly(bool enabled);
|
2020-07-02 01:26:44 +02:00
|
|
|
void setFilterPolicy(uint8_t filter);
|
2021-04-01 04:24:57 +02:00
|
|
|
void clearDuplicateCache();
|
2020-06-22 06:07:01 +02:00
|
|
|
bool stop();
|
2020-03-30 01:44:20 +02:00
|
|
|
void clearResults();
|
|
|
|
NimBLEScanResults getResults();
|
2022-08-27 21:13:27 +02:00
|
|
|
NimBLEScanResults getResults(uint32_t duration, bool is_continue = false);
|
2021-01-31 02:27:06 +01:00
|
|
|
void setMaxResults(uint8_t maxResults);
|
2020-05-10 15:21:46 +02:00
|
|
|
void erase(const NimBLEAddress &address);
|
2020-05-14 06:03:56 +02:00
|
|
|
|
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
private:
|
|
|
|
friend class NimBLEDevice;
|
2020-07-29 04:57:33 +02:00
|
|
|
|
|
|
|
NimBLEScan();
|
|
|
|
~NimBLEScan();
|
2022-08-27 21:13:27 +02:00
|
|
|
static int handleGapEvent(ble_gap_event* event, void* arg);
|
|
|
|
void onHostReset();
|
|
|
|
void onHostSync();
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2022-08-27 21:13:27 +02:00
|
|
|
NimBLEScanCallbacks* m_pScanCallbacks;
|
|
|
|
ble_gap_disc_params m_scan_params;
|
|
|
|
bool m_ignoreResults;
|
|
|
|
NimBLEScanResults m_scanResults;
|
|
|
|
uint32_t m_duration;
|
|
|
|
ble_task_data_t *m_pTaskData;
|
|
|
|
uint8_t m_maxResults;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A callback handler for callbacks associated device scanning.
|
|
|
|
*/
|
|
|
|
class NimBLEScanCallbacks {
|
|
|
|
public:
|
|
|
|
virtual ~NimBLEScanCallbacks() {}
|
|
|
|
/**
|
|
|
|
* @brief Called when a new scan result is detected.
|
|
|
|
*
|
|
|
|
* As we are scanning, we will find new devices. When found, this call back is invoked with a reference to the
|
|
|
|
* device that was found. During any individual scan, a device will only be detected one time.
|
|
|
|
*/
|
|
|
|
virtual void onResult(NimBLEAdvertisedDevice* advertisedDevice) {};
|
|
|
|
virtual void onScanEnd(NimBLEScanResults scanResults) {};
|
2020-03-30 01:44:20 +02:00
|
|
|
};
|
|
|
|
|
2021-09-07 05:14:43 +02:00
|
|
|
#endif /* CONFIG_BT_ENABLED CONFIG_BT_NIMBLE_ROLE_OBSERVER */
|
2020-03-30 01:44:20 +02:00
|
|
|
#endif /* COMPONENTS_NIMBLE_SCAN_H_ */
|