Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ jobs:
example:
- Advanced/NimBLE_Client
- Advanced/NimBLE_Server
- basic/BLE_client
- basic/BLE_notify
- basic/BLE_scan
- basic/BLE_server
- basic/BLE_uart
- Continuous_scan
- Bluetooth_5/NimBLE_extended_client
- Bluetooth_5/NimBLE_extended_server
- Bluetooth_5/NimBLE_multi_advertiser
Expand Down
8 changes: 4 additions & 4 deletions examples/Advanced/NimBLE_Client/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

extern "C" {void app_main(void);}

static NimBLEAdvertisedDevice* advDevice;
static const NimBLEAdvertisedDevice* advDevice;

static bool doConnect = false;
static uint32_t scanTime = 0; /** scan time in milliseconds, 0 = scan forever */
Expand Down Expand Up @@ -67,7 +67,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {

/** Define a class to handle the callbacks when advertisments are received */
class scanCallbacks: public NimBLEScanCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
if(advertisedDevice->isAdvertisingService(NimBLEUUID("DEAD")))
{
Expand All @@ -82,8 +82,8 @@ class scanCallbacks: public NimBLEScanCallbacks {
}

/** Callback to process the results of the completed scan or restart it */
void onScanEnd(NimBLEScanResults results) {
printf("Scan Ended\n");
void onScanEnd(const NimBLEScanResults& results, int reason) {
printf("Scan Ended, reason: %d, device count: %d\n", reason, results.getCount());
}
};

Expand Down
6 changes: 6 additions & 0 deletions examples/Continuous_scan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(Continuous_scan)
4 changes: 4 additions & 0 deletions examples/Continuous_scan/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.cpp")
set(COMPONENT_ADD_INCLUDEDIRS ".")

register_component()
45 changes: 45 additions & 0 deletions examples/Continuous_scan/main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Continuous Scan Example
*
* This example demonstrates how to continuously scan for BLE devices.
* When devices are found the onDiscovered and onResults callbacks will be called with the device data.
* The scan will not store the results, only the callbacks will be used
* When the scan timeout is reached the onScanEnd callback will be called and the scan will be restarted.
* This will clear the duplicate cache in the controller and allow the same devices to be reported again.
*
* Created: on March 24 2020
* Author: H2zero
*
*/

#include "NimBLEDevice.h"

static constexpr uint32_t scanTime = 30 * 1000; // 30 seconds scan time.

class scanCallbacks : public NimBLEScanCallbacks {
// Initial discovery, advertisement data only.
void onDiscovered(const NimBLEAdvertisedDevice* advertisedDevice) override {
printf("Discovered Device: %s\n", advertisedDevice->toString().c_str());
}

// If active scanning the result here will have the scan response data.
// If not active scanning then this will be the same as onDiscovered.
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) override {
printf("Device result: %s\n", advertisedDevice->toString().c_str());
}

void onScanEnd(const NimBLEScanResults& results, int reason) override {
printf("Scan ended reason = %d; restarting scan\n", reason);
NimBLEDevice::getScan()->start(scanTime, false, true);
}
} scanCallbacks; // create a callback class instance.

extern "C" void app_main() {
NimBLEDevice::init(""); // Initialize the device, you can specify a device name if you want.
NimBLEScan* pBLEScan = NimBLEDevice::getScan(); // Create the scan object.
pBLEScan->setScanCallbacks(&scanCallbacks, false); // Set the callback for when devices are discovered, no duplicates.
pBLEScan->setActiveScan(true); // Set active scanning, this will get more data from the advertiser.
pBLEScan->setMaxResults(0); // Do not store the scan results, use callback only.
pBLEScan->start(scanTime, false, true); // duration, not a continuation of last scan, restart to get all devices again.
printf("Scanning...\n");
}
12 changes: 12 additions & 0 deletions examples/Continuous_scan/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Override some defaults so BT stack is enabled
# in this example

#
# BT config
#
CONFIG_BT_ENABLED=y
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
CONFIG_BTDM_CTRL_MODE_BTDM=n
CONFIG_BT_BLUEDROID_ENABLED=n
CONFIG_BT_NIMBLE_ENABLED=y
2 changes: 1 addition & 1 deletion examples/NimBLE_Async_Client/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ClientCallbacks : public NimBLEClientCallbacks {
} clientCB;

class scanCallbacks : public NimBLEScanCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
void onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
printf("Advertised Device found: %s\n", advertisedDevice->toString().c_str());
if (advertisedDevice->haveName() && advertisedDevice->getName() == "NimBLE-Server") {
printf("Found Our Device\n");
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/BLE_client/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static bool doConnect = false;
static bool connected = false;
static bool doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
static const BLEAdvertisedDevice* myDevice;

static void notifyCallback(
BLERemoteCharacteristic* pBLERemoteCharacteristic,
Expand Down Expand Up @@ -137,7 +137,7 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {

/*** Only a reference to the advertised device is passed now
void onResult(BLEAdvertisedDevice advertisedDevice) { **/
void onResult(BLEAdvertisedDevice* advertisedDevice) {
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
printf("BLE Advertised Device found: %s\n", advertisedDevice->toString().c_str());

// We have found a device, let us now see if it contains the service we are looking for.
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/BLE_scan/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int scanTime = 5 * 1000; // In milliseconds, 0 = scan forever
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice* advertisedDevice) {
void onResult(const BLEAdvertisedDevice* advertisedDevice) {
printf("Advertised Device: %s \n", advertisedDevice->toString().c_str());
}
};
Expand All @@ -35,7 +35,7 @@ void scanTask (void * parameter){
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
vTaskDelay(2000/portTICK_PERIOD_MS); // Delay a second between loops.
}

vTaskDelete(NULL);
}

Expand Down
8 changes: 8 additions & 0 deletions src/NimBLEAdvertisedDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,14 @@ bool NimBLEAdvertisedDevice::isConnectable() const {
return (m_advType & BLE_HCI_ADV_CONN_MASK) || (m_advType & BLE_HCI_ADV_DIRECT_MASK);
} // isConnectable

/**
* @brief Check if this device is advertising as scannable.
* @return True if the device is scannable.
*/
bool NimBLEAdvertisedDevice::isScannable() const {
return isLegacyAdvertisement() && (m_advType == BLE_HCI_ADV_TYPE_ADV_IND || m_advType == BLE_HCI_ADV_TYPE_ADV_SCAN_IND);
} // isScannable

/**
* @brief Check if this advertisement is a legacy or extended type
* @return True if legacy (Bluetooth 4.x), false if extended (bluetooth 5.x).
Expand Down
1 change: 1 addition & 0 deletions src/NimBLEAdvertisedDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class NimBLEAdvertisedDevice {
bool haveType(uint16_t type) const;
std::string toString() const;
bool isConnectable() const;
bool isScannable() const;
bool isLegacyAdvertisement() const;
# if CONFIG_BT_NIMBLE_EXT_ADV
uint8_t getSetId() const;
Expand Down
10 changes: 1 addition & 9 deletions src/NimBLEDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,15 +723,7 @@ void NimBLEDevice::onReset(int reason) {

m_synced = false;

NIMBLE_LOGE(LOG_TAG, "Resetting state; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));

# if defined(CONFIG_BT_NIMBLE_ROLE_OBSERVER)
if (m_initialized) {
if (m_pScan != nullptr) {
m_pScan->onHostReset();
}
}
# endif
NIMBLE_LOGE(LOG_TAG, "Host reset; reason=%d, %s", reason, NimBLEUtils::returnCodeToString(reason));
} // onReset

/**
Expand Down
Loading