Skip to content

Commit 931b0ac

Browse files
committed
Added support for directed BLE advertisements.
Can be used for example with advertising type BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED.
1 parent bf50479 commit 931b0ac

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

libraries/Bluefruit52Lib/src/BLEAdvertising.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ BLEAdvertising::BLEAdvertising(void)
265265
_stop_timeout = _left_timeout = 0;
266266
_stop_cb = NULL;
267267
_slow_cb = NULL;
268+
_directed = false; // Broadcast advertising is the default
268269
}
269270

270271
void BLEAdvertising::setFastTimeout(uint16_t sec)
@@ -315,6 +316,17 @@ void BLEAdvertising::setStopCallback(stop_callback_t fp)
315316
_stop_cb = fp;
316317
}
317318

319+
void BLEAdvertising::setPeerAddress(const ble_gap_addr_t& peer_addr)
320+
{
321+
_directed = true;
322+
_peer_addr = peer_addr; // Copy address, used later in start function
323+
}
324+
325+
void BLEAdvertising::removePeerAddress()
326+
{
327+
_directed = false;
328+
}
329+
318330
bool BLEAdvertising::isRunning(void)
319331
{
320332
return _runnning;
@@ -354,6 +366,11 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
354366
// , .set_id, .scan_req_notification
355367
};
356368

369+
if (_directed) {
370+
// Set target address when not using broadcasting
371+
adv_para.p_peer_addr = &_peer_addr;
372+
}
373+
357374
// gap_adv long-live is required by SD v6
358375
static ble_gap_adv_data_t gap_adv =
359376
{
@@ -464,5 +481,4 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)
464481

465482
default: break;
466483
}
467-
}
468-
484+
}

libraries/Bluefruit52Lib/src/BLEAdvertising.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ class BLEAdvertising : public BLEAdvertisingData
134134
bool setBeacon(BLEBeacon& beacon);
135135
bool setBeacon(EddyStoneUrl& eddy_url);
136136

137+
/// Advertise to a single peer instead of broadcasting
138+
void setPeerAddress(const ble_gap_addr_t& peer_addr);
139+
140+
/// Revert to broadcast advertising
141+
void removePeerAddress();
142+
137143
bool isRunning(void);
138144

139145
void restartOnDisconnect(bool enable);
@@ -152,6 +158,8 @@ class BLEAdvertising : public BLEAdvertisingData
152158
uint8_t _type;
153159
bool _start_if_disconnect;
154160
bool _runnning;
161+
ble_gap_addr_t _peer_addr; //! Target address for an ADV_DIRECT_IND advertisement
162+
bool _directed; //! Whether the advertisements are directed or broadcast
155163

156164
uint32_t _conn_mask;
157165

0 commit comments

Comments
 (0)