Skip to content

Commit f5e9131

Browse files
authored
Merge pull request #2301 from tannewt/support_extended_advertising
Add support for extended (>31 byte) BLE advertisements.
2 parents fe05be8 + 3fc58ce commit f5e9131

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,9 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre
508508
// The nRF SD 6.1.0 can only do one concurrent advertisement so share the advertising handle.
509509
uint8_t adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
510510

511-
STATIC void check_data_fit(size_t data_len) {
512-
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX) {
511+
STATIC void check_data_fit(size_t data_len, bool connectable) {
512+
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED ||
513+
(connectable && data_len > BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED)) {
513514
mp_raise_ValueError(translate("Data too large for advertisement packet"));
514515
}
515516
}
@@ -525,11 +526,31 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
525526
common_hal_bleio_adapter_stop_advertising(self);
526527
}
527528

529+
530+
bool extended = advertising_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX ||
531+
scan_response_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX;
532+
533+
uint8_t adv_type;
534+
if (extended) {
535+
if (connectable) {
536+
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_UNDIRECTED;
537+
} else if (scan_response_data_len > 0) {
538+
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_SCANNABLE_UNDIRECTED;
539+
} else {
540+
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
541+
}
542+
} else if (connectable) {
543+
adv_type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
544+
} else if (scan_response_data_len > 0) {
545+
adv_type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED;
546+
} else {
547+
adv_type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
548+
}
549+
528550
uint32_t err_code;
529551
ble_gap_adv_params_t adv_params = {
530552
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS),
531-
.properties.type = connectable ? BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED
532-
: BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED,
553+
.properties.type = adv_type,
533554
.duration = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED,
534555
.filter_policy = BLE_GAP_ADV_FP_ANY,
535556
.primary_phy = BLE_GAP_PHY_1MBPS,
@@ -562,15 +583,15 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
562583
}
563584
// interval value has already been validated.
564585

565-
check_data_fit(advertising_data_bufinfo->len);
566-
check_data_fit(scan_response_data_bufinfo->len);
586+
check_data_fit(advertising_data_bufinfo->len, connectable);
587+
check_data_fit(scan_response_data_bufinfo->len, connectable);
567588
// The advertising data buffers must not move, because the SoftDevice depends on them.
568589
// So make them long-lived and reuse them onwards.
569590
if (self->advertising_data == NULL) {
570-
self->advertising_data = (uint8_t *) gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_MAX * sizeof(uint8_t), false, true);
591+
self->advertising_data = (uint8_t *) gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED * sizeof(uint8_t), false, true);
571592
}
572593
if (self->scan_response_data == NULL) {
573-
self->scan_response_data = (uint8_t *) gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_MAX * sizeof(uint8_t), false, true);
594+
self->scan_response_data = (uint8_t *) gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED * sizeof(uint8_t), false, true);
574595
}
575596

576597
memcpy(self->advertising_data, advertising_data_bufinfo->buf, advertising_data_bufinfo->len);

shared-bindings/_bleio/Adapter.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ const mp_obj_property_t bleio_adapter_name_obj = {
144144
//| Starts advertising until `stop_advertising` is called or if connectable, another device
145145
//| connects to us.
146146
//|
147+
//| .. warning: If data is longer than 31 bytes, then this will automatically advertise as an
148+
//| extended advertisement that older BLE 4.x clients won't be able to scan for.
149+
//|
147150
//| :param buf data: advertising data packet bytes
148151
//| :param buf scan_response: scan response data packet bytes. ``None`` if no scan response is needed.
149152
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.

0 commit comments

Comments
 (0)