Skip to content

Commit 1eedd9c

Browse files
committed
lib: bluetooth: ble_adv: fix directed advertising set configuration
When configuring the advertisement set for direct advertisement with sd_ble_gap_adv_set_configure(), NULL must be passed for the advertising data argument because advertising data is not supported in this mode. The sd_ble_gap_adv_set_configure() would return NRF_ERROR_INVALID_PARAM in this case. When doing directed advertisement, the address of the peer to direct the advertisement towards needed to be specified in the advertisement parameters when calling sd_ble_gap_adv_set_configure(). Set the address in the advertisement parameters to the address supplied by the application calling the ble_adv_peer_addr_reply() function in response to the BLE_ADV_EVT_PEER_ADDR_REQUEST event. Signed-off-by: Andreas Moltumyr <[email protected]>
1 parent 537203f commit 1eedd9c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lib/bluetooth/ble_adv/ble_adv.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
337337
{
338338
uint32_t nrf_err;
339339
struct ble_adv_evt adv_evt;
340+
ble_gap_adv_data_t *adv_data;
341+
340342
if (!ble_adv) {
341343
return NRF_ERROR_NULL;
342344
}
@@ -354,6 +356,8 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
354356
/* Reset peer address */
355357
memset(&ble_adv->peer_address, 0, sizeof(ble_adv->peer_address));
356358

359+
adv_data = &ble_adv->adv_data;
360+
357361
/* If `mode` is initially directed advertising (and that's supported)
358362
* ask the application for a peer address
359363
*/
@@ -362,6 +366,16 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
362366
ble_adv->peer_addr_reply_expected = true;
363367
adv_evt.evt_type = BLE_ADV_EVT_PEER_ADDR_REQUEST;
364368
ble_adv->evt_handler(ble_adv, &adv_evt);
369+
370+
if (ble_adv->peer_addr_reply_expected == false) {
371+
/* Peer address was set by the application. Use it. */
372+
ble_adv->adv_params.p_peer_addr = &ble_adv->peer_address;
373+
adv_data = NULL;
374+
} else {
375+
/* No peer address was supplied. Skip directed advertising. */
376+
ble_adv->peer_addr_reply_expected = false;
377+
mode = BLE_ADV_MODE_FAST;
378+
}
365379
}
366380
}
367381

@@ -436,7 +450,7 @@ uint32_t ble_adv_start(struct ble_adv *ble_adv, enum ble_adv_mode mode)
436450
}
437451

438452
if (mode != BLE_ADV_MODE_IDLE) {
439-
nrf_err = sd_ble_gap_adv_set_configure(&ble_adv->adv_handle, &ble_adv->adv_data,
453+
nrf_err = sd_ble_gap_adv_set_configure(&ble_adv->adv_handle, adv_data,
440454
&ble_adv->adv_params);
441455
if (nrf_err) {
442456
LOG_ERR("Failed to set advertising data, nrf_error %#x", nrf_err);

0 commit comments

Comments
 (0)