Skip to content

Commit

Permalink
Added support for directed BLE advertisements.
Browse files Browse the repository at this point in the history
Can be used for example with advertising type BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED.
  • Loading branch information
UUSim committed Sep 5, 2023
1 parent bf50479 commit 931b0ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions libraries/Bluefruit52Lib/src/BLEAdvertising.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ BLEAdvertising::BLEAdvertising(void)
_stop_timeout = _left_timeout = 0;
_stop_cb = NULL;
_slow_cb = NULL;
_directed = false; // Broadcast advertising is the default
}

void BLEAdvertising::setFastTimeout(uint16_t sec)
Expand Down Expand Up @@ -315,6 +316,17 @@ void BLEAdvertising::setStopCallback(stop_callback_t fp)
_stop_cb = fp;
}

void BLEAdvertising::setPeerAddress(const ble_gap_addr_t& peer_addr)
{
_directed = true;
_peer_addr = peer_addr; // Copy address, used later in start function
}

void BLEAdvertising::removePeerAddress()
{
_directed = false;
}

bool BLEAdvertising::isRunning(void)
{
return _runnning;
Expand Down Expand Up @@ -354,6 +366,11 @@ bool BLEAdvertising::_start(uint16_t interval, uint16_t timeout)
// , .set_id, .scan_req_notification
};

if (_directed) {
// Set target address when not using broadcasting
adv_para.p_peer_addr = &_peer_addr;
}

// gap_adv long-live is required by SD v6
static ble_gap_adv_data_t gap_adv =
{
Expand Down Expand Up @@ -464,5 +481,4 @@ void BLEAdvertising::_eventHandler(ble_evt_t* evt)

default: break;
}
}

}
8 changes: 8 additions & 0 deletions libraries/Bluefruit52Lib/src/BLEAdvertising.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ class BLEAdvertising : public BLEAdvertisingData
bool setBeacon(BLEBeacon& beacon);
bool setBeacon(EddyStoneUrl& eddy_url);

/// Advertise to a single peer instead of broadcasting
void setPeerAddress(const ble_gap_addr_t& peer_addr);

/// Revert to broadcast advertising
void removePeerAddress();

bool isRunning(void);

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

uint32_t _conn_mask;

Expand Down

0 comments on commit 931b0ac

Please sign in to comment.