Skip to content

Commit 40b0c43

Browse files
sgbihusys_maker
authored and
sys_maker
committed
Jira 797, Central can scan with MAC address, git 376
New feature: - Add the capability for a Central to scan for a Peripheral using its MAC address. Code Modifications: - Added the API for MAC address scanning. - For Device Manager, added provision for holding the MAC address of a Peripheral. - For scan discovery handler, added the checking for MAC address comparison, if MAC address scanning is requested. File changes: 1. libraries/CurieBLE/src/BLEDevice.cpp: - Added API for MAC address scanning. 2. libraries/CurieBLE/src/BLEDevice.h: - API definition. 3. libraries/CurieBLE/src/internal/BLEDeviceManager.cpp: - Added provision for storing the MAC address. - Added MAC address comparison in the scan discovery handle if MAC address scanning is selected. 4. libraries/CurieBLE/src/internal/BLEDeviceManager.h: - Prototyping.
1 parent 78c3d0a commit 40b0c43

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

libraries/CurieBLE/src/BLEDevice.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ void BLEDevice::scanForUuid(String uuid, bool withDuplicates)
275275
startScan(withDuplicates);
276276
}
277277

278+
void BLEDevice::scanForAddress(String macaddr, bool withDuplicates)
279+
{
280+
BLEDeviceManager::instance()->setAdvertiseCritical(macaddr.c_str());
281+
startScan(withDuplicates);
282+
}
283+
278284
void BLEDevice::stopScan()
279285
{
280286
BLEDeviceManager::instance()->stopScanning();

libraries/CurieBLE/src/BLEDevice.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,9 @@ class BLEDevice
420420
* @note option to filter out duplicate addresses for Arduino.
421421
* The current only support fileter duplicate mode.
422422
*/
423-
void scanForUuid(String uuid, bool withDuplicates);
423+
void scanForUuid(String uuid, bool withDuplicates);
424424

425+
void scanForAddress(String macaddr, bool withDuplicates = true);
425426
/**
426427
* @brief Stop scanning for peripherals
427428
*

libraries/CurieBLE/src/internal/BLEDeviceManager.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ BLEDeviceManager::BLEDeviceManager():
7777
memset(_peer_adv_mill, 0, sizeof(_peer_adv_mill));
7878
memset(&_adv_accept_critical, 0, sizeof(_adv_accept_critical));
7979
memset(&_adv_critical_service_uuid, 0, sizeof(_adv_critical_service_uuid));
80+
memset(&_adv_accept_device, 0, sizeof(_adv_accept_device));
8081

8182
memset(_peer_peripheral, 0, sizeof(_peer_peripheral));
8283
memset(_peer_peripheral_adv_data, 0, sizeof(_peer_peripheral_adv_data));
@@ -562,6 +563,7 @@ bool BLEDeviceManager::stopScanning()
562563
void BLEDeviceManager::clearAdvertiseCritical()
563564
{
564565
memset(&_adv_accept_critical, 0, sizeof(_adv_accept_critical));
566+
memset(&_adv_accept_device, 0, sizeof(_adv_accept_device));
565567
//memset(&_adv_critical_service_uuid, 0, sizeof(_adv_critical_service_uuid));
566568
}
567569

@@ -598,6 +600,11 @@ void BLEDeviceManager::setAdvertiseCritical(BLEService& service)
598600
_adv_accept_critical.data = data;
599601
}
600602

603+
void BLEDeviceManager::setAdvertiseCritical(const char* macaddress)
604+
{
605+
BLEUtils::macAddressString2BT(macaddress, _adv_accept_device);
606+
}
607+
601608
bool BLEDeviceManager::hasLocalName(const BLEDevice* device) const
602609
{
603610
if (BLEUtils::isLocalBLE(*device) == true)
@@ -1248,6 +1255,13 @@ void BLEDeviceManager::handleDeviceFound(const bt_addr_le_t *addr,
12481255
if (type == BT_LE_ADV_IND || type == BT_LE_ADV_DIRECT_IND)
12491256
{
12501257
//pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
1258+
// Filter address
1259+
if (BLEUtils::macAddressValid(_adv_accept_device) == true &&
1260+
(memcmp(addr->val, _adv_accept_device.val, sizeof (addr->val)) != 0))
1261+
{
1262+
return;
1263+
}
1264+
12511265
while (data_len > 1)
12521266
{
12531267
uint8_t len = data[0];

libraries/CurieBLE/src/internal/BLEDeviceManager.h

+2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ class BLEDeviceManager
305305
void clearAdvertiseCritical();
306306
void setAdvertiseCritical(String name);
307307
void setAdvertiseCritical(BLEService& service);
308+
void setAdvertiseCritical(const char* macaddress);
308309
bool startScanning(); // start scanning for peripherals
309310
bool startScanningWithDuplicates(); // start scanning for peripherals, and report all duplicates
310311
bool stopScanning(); // stop scanning for peripherals
@@ -378,6 +379,7 @@ class BLEDeviceManager
378379
bt_data_t _adv_accept_critical; // The filters for central device
379380
String _adv_critical_local_name;
380381
bt_uuid_128_t _adv_critical_service_uuid;
382+
bt_addr_le_t _adv_accept_device;
381383

382384
bt_addr_le_t _wait_for_connect_peripheral;
383385
uint8_t _wait_for_connect_peripheral_adv_data[BLE_MAX_ADV_SIZE];

0 commit comments

Comments
 (0)