Skip to content

Commit

Permalink
Improve initialization process for NimBLE client
Browse files Browse the repository at this point in the history
Rename setupAndStartBleScans to setupBleScans since it only
configures the BLE scanning.

Create new internal function to start BLE scans that gets called
from the begin() method.

keepAlive doesn't need to be called on init to start the scanning.
It can be used to re-start in case of errors as documented.
  • Loading branch information
psachs committed Oct 24, 2024
1 parent 73b9bb3 commit 099fb9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
52 changes: 30 additions & 22 deletions src/NimBleClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,42 @@

void NimBleClient::begin(BleClientCallback* callback) {
_callback = callback;
setupAndStartBleScans();
setupBleScans();
startBleScans();
}

void NimBleClient::keepAlive() {
// If an error occurs that stops the scan, it will be restarted here.
if (!_bleScan->isScanning()) {
// Start scan with: duration = 0 seconds(forever), no scan end callback,
// not a continuation of a previous scan.
_bleScan->start(0, nullptr, false);
startBleScans();
}
}

void NimBleClient::setupAndStartBleScans() {

void NimBleClient::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
if (!advertisedDevice->haveManufacturerData()) {
return;
}

// MAC address contains 6 bytes of MAC address (in reversed order)
// (Note: advertisedDevice->getAddress().toString() seems broken)
const uint8_t* bleMACAddress = advertisedDevice->getAddress().getNative();
std::string address;
for (int i = 5; i >= 0; i--) {
address.push_back(static_cast<char>(bleMACAddress[i]));
}

std::string name = advertisedDevice->haveName()
? advertisedDevice->getName()
: "UNDEFINED";
std::string manufacturerData = advertisedDevice->getManufacturerData();

_callback->onAdvertisementReceived(address, name, manufacturerData);
}

void NimBleClient::setupBleScans() {
// CONFIG_BTDM_SCAN_DUPL_TYPE_DATA_DEVICE (2)
// Filter by address and data, advertisements from the same address will be
// reported only once, except if the data in the advertisement has changed,
Expand All @@ -37,23 +60,8 @@ void NimBleClient::setupAndStartBleScans() {
_bleScan->setMaxResults(0);
}

void NimBleClient::onResult(NimBLEAdvertisedDevice* advertisedDevice) {
if (!advertisedDevice->haveManufacturerData()) {
return;
}

// MAC address contains 6 bytes of MAC address (in reversed order)
// (Note: advertisedDevice->getAddress().toString() seems broken)
const uint8_t* bleMACAddress = advertisedDevice->getAddress().getNative();
std::string address;
for (int i = 5; i >= 0; i--) {
address.push_back(static_cast<char>(bleMACAddress[i]));
}

std::string name = advertisedDevice->haveName()
? advertisedDevice->getName()
: "UNDEFINED";
std::string manufacturerData = advertisedDevice->getManufacturerData();

_callback->onAdvertisementReceived(address, name, manufacturerData);
void NimBleClient::startBleScans() {
// Start scan with: duration = 0 seconds(forever), no scan end callback,
// not a continuation of a previous scan.
_bleScan->start(0, nullptr, false);
}
3 changes: 2 additions & 1 deletion src/NimBleClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class __attribute__((unused)) NimBleClient
private:
NimBLEScan* _bleScan;
BleClientCallback* _callback;
void setupAndStartBleScans();
void setupBleScans();
void startBleScans();
void onResult(NimBLEAdvertisedDevice* advertisedDevice) override;
};

Expand Down
1 change: 0 additions & 1 deletion src/Sensirion_upt_ble_auto_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const int COMPANY_ID_FILTER = 54534;
void SensiScan::begin() {
_bleClient = new NimBleClient();
_bleClient->begin(this);
_bleClient->keepAlive();
}

__attribute__((unused)) void SensiScan::getScanResults(
Expand Down

0 comments on commit 099fb9b

Please sign in to comment.