6868#define BLE_SLAVE_LATENCY 0
6969#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
7070
71- #ifndef BLEIO_VS_UUID_COUNT
72- #define BLEIO_VS_UUID_COUNT 75
73- #endif
74-
75- #ifndef BLEIO_HVN_TX_QUEUE_SIZE
76- #define BLEIO_HVN_TX_QUEUE_SIZE 9
77- #endif
78-
79- #ifndef BLEIO_CENTRAL_ROLE_COUNT
80- #define BLEIO_CENTRAL_ROLE_COUNT 4
81- #endif
82-
83- #ifndef BLEIO_PERIPH_ROLE_COUNT
84- #define BLEIO_PERIPH_ROLE_COUNT 4
85- #endif
86-
87- #ifndef BLEIO_ATTR_TAB_SIZE
88- #define BLEIO_ATTR_TAB_SIZE (BLE_GATTS_ATTR_TAB_SIZE_DEFAULT * 5)
89- #endif
90-
9171bleio_connection_internal_t bleio_connections [BLEIO_TOTAL_CONNECTION_COUNT ];
9272
9373// STATIC bool adapter_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
@@ -193,6 +173,11 @@ STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
193173// Get various values and limits set by the adapter.
194174STATIC void bleio_adapter_get_info (bleio_adapter_obj_t * self ) {
195175
176+ // Get supported features.
177+ if (hci_le_read_local_supported_features (self -> features ) != HCI_OK ) {
178+ mp_raise_bleio_BluetoothError (translate ("Could not read BLE features" ));
179+ }
180+
196181 // Get ACL buffer info.
197182 uint16_t le_max_len ;
198183 uint8_t le_max_num ;
@@ -212,26 +197,28 @@ STATIC void bleio_adapter_get_info(bleio_adapter_obj_t *self) {
212197 self -> max_acl_num_buffers = acl_max_num ;
213198 }
214199
215- // Get max advertising length.
216- uint16_t max_adv_data_len ;
217- if (hci_le_read_maximum_advertising_data_length (& max_adv_data_len ) != HCI_OK ) {
218- mp_raise_bleio_BluetoothError (translate ("Could not get max advertising length" ));
200+ // Get max advertising length if extended advertising is supported.
201+ if (BT_FEAT_LE_EXT_ADV (self -> features )) {
202+ uint16_t max_adv_data_len ;
203+ if (hci_le_read_maximum_advertising_data_length (& max_adv_data_len ) != HCI_OK ) {
204+ mp_raise_bleio_BluetoothError (translate ("Could not get max advertising length" ));
205+ }
206+ self -> max_adv_data_len = max_adv_data_len ;
207+ } else {
208+ self -> max_adv_data_len = 31 ;
219209 }
220- self -> max_adv_data_len = max_adv_data_len ;
221210}
222211
223212void common_hal_bleio_adapter_hci_uart_init (bleio_adapter_obj_t * self , busio_uart_obj_t * uart , digitalio_digitalinout_obj_t * rts , digitalio_digitalinout_obj_t * cts ) {
224213 self -> hci_uart = uart ;
225214 self -> rts_digitalinout = rts ;
226215 self -> cts_digitalinout = cts ;
216+
217+ // Advertising-related fields are initialized by common_hal_bleio_adapter_set_enabled().
227218 self -> enabled = false;
228- self -> now_advertising = false;
229- self -> circuitpython_advertising = false;
230- self -> extended_advertising = false;
231- self -> advertising_timeout_msecs = 0 ;
232219
220+ common_hal_bleio_adapter_set_enabled (self , true);
233221 bleio_adapter_get_info (self );
234-
235222 bleio_adapter_reset_name (self );
236223}
237224
@@ -243,15 +230,14 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
243230 return ;
244231 }
245232
246- //FIX enable/disable HCI adapter, but don't reset it, since we don't know how.
247233 self -> enabled = enabled ;
248- if (! enabled ) {
249- // Stop any current activity.
250- check_hci_error (hci_reset ());
251- self -> now_advertising = false;
252- self -> extended_advertising = false;
253- self -> circuitpython_advertising = false;
254- }
234+
235+ // Stop any current activity; reset to known state .
236+ check_hci_error (hci_reset ());
237+ self -> now_advertising = false;
238+ self -> extended_advertising = false;
239+ self -> circuitpython_advertising = false;
240+ self -> advertising_timeout_msecs = 0 ;
255241}
256242
257243bool common_hal_bleio_adapter_get_enabled (bleio_adapter_obj_t * self ) {
@@ -506,6 +492,10 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
506492 advertising_data_len > self -> max_adv_data_len || scan_response_data_len > self -> max_adv_data_len ;
507493
508494 if (extended ) {
495+ if (!BT_FEAT_LE_EXT_ADV (self -> features )) {
496+ mp_raise_bleio_BluetoothError (translate ("Data length needs extended advertising, but this adapter does not support it" ));
497+ }
498+
509499 uint16_t props = 0 ;
510500 if (connectable ) {
511501 props |= BT_HCI_LE_ADV_PROP_CONN ;
0 commit comments