58
58
******************************************************************************/
59
59
#define BT_SCAN_QUEUE_SIZE_MAX (16)
60
60
#define BT_GATTS_QUEUE_SIZE_MAX (2)
61
- #define BT_CHAR_VALUE_SIZE_MAX (20)
61
+ #define BT_MTU_SIZE_MAX (200)
62
+ #define BT_CHAR_VALUE_SIZE_MAX (BT_MTU_SIZE_MAX - 3)
62
63
63
64
#define MOD_BT_CLIENT_APP_ID (0)
64
65
#define MOD_BT_SERVER_APP_ID (1)
@@ -209,6 +210,9 @@ static volatile bt_obj_t bt_obj;
209
210
static QueueHandle_t xScanQueue ;
210
211
static QueueHandle_t xGattsQueue ;
211
212
213
+ static esp_ble_adv_data_t adv_data ;
214
+ static esp_ble_adv_data_t scan_rsp_data ;
215
+
212
216
static const mp_obj_type_t mod_bt_connection_type ;
213
217
static const mp_obj_type_t mod_bt_service_type ;
214
218
static const mp_obj_type_t mod_bt_characteristic_type ;
@@ -219,7 +223,7 @@ static const mp_obj_type_t mod_bt_gatts_char_type;
219
223
220
224
static esp_ble_adv_params_t bt_adv_params = {
221
225
.adv_int_min = 0x20 ,
222
- .adv_int_max = 0x20 ,
226
+ .adv_int_max = 0x40 ,
223
227
.adv_type = ADV_TYPE_IND ,
224
228
.own_addr_type = BLE_ADDR_TYPE_PUBLIC ,
225
229
.channel_map = ADV_CHNL_ALL ,
@@ -451,6 +455,7 @@ static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
451
455
bt_obj .busy = false;
452
456
// intentional fall through
453
457
case ESP_GATTC_CLOSE_EVT :
458
+ case ESP_GATTC_DISCONNECT_EVT :
454
459
close_connection (p_data -> close .conn_id );
455
460
break ;
456
461
default :
@@ -684,7 +689,7 @@ static mp_obj_t bt_init_helper(bt_obj_t *self, const mp_arg_val_t *args) {
684
689
esp_ble_gattc_app_register (MOD_BT_CLIENT_APP_ID );
685
690
esp_ble_gatts_app_register (MOD_BT_SERVER_APP_ID );
686
691
687
- esp_ble_gatt_set_local_mtu (200 );
692
+ esp_ble_gatt_set_local_mtu (BT_MTU_SIZE_MAX );
688
693
689
694
self -> init = true;
690
695
}
@@ -989,7 +994,6 @@ STATIC mp_obj_t bt_set_advertisement (mp_uint_t n_args, const mp_obj_t *pos_args
989
994
{ MP_QSTR_service_uuid , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
990
995
};
991
996
992
- esp_ble_adv_data_t adv_data ;
993
997
mp_buffer_info_t manuf_bufinfo ;
994
998
mp_buffer_info_t srv_bufinfo ;
995
999
mp_buffer_info_t uuid_bufinfo ;
@@ -1056,7 +1060,21 @@ STATIC mp_obj_t bt_set_advertisement (mp_uint_t n_args, const mp_obj_t *pos_args
1056
1060
adv_data .appearance = 0x00 ;
1057
1061
adv_data .flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT );
1058
1062
1063
+ // copy all the info to the scan response
1064
+ memcpy (& scan_rsp_data , & adv_data , sizeof (esp_ble_adv_data_t ));
1065
+ scan_rsp_data .set_scan_rsp = true;
1066
+ // do not include the name or the tx power in the scan response
1067
+ scan_rsp_data .include_name = false;
1068
+ scan_rsp_data .include_txpower = false;
1069
+ // do not include the service uuid or service data in the advertisement, only in the scan response
1070
+ adv_data .manufacturer_len = 0 ;
1071
+ adv_data .p_manufacturer_data = NULL ;
1072
+ adv_data .service_data_len = 0 ;
1073
+ adv_data .p_service_data = NULL ;
1074
+ adv_data .service_uuid_len = 0 ;
1075
+ adv_data .p_service_uuid = NULL ;
1059
1076
esp_ble_gap_config_adv_data (& adv_data );
1077
+ esp_ble_gap_config_adv_data (& scan_rsp_data );
1060
1078
1061
1079
// wait for the advertisement data to be configured
1062
1080
bt_gatts_event_result_t gatts_event ;
@@ -1068,6 +1086,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bt_set_advertisement_obj, 1, bt_set_advertisem
1068
1086
1069
1087
STATIC mp_obj_t bt_advertise (mp_obj_t self_in , mp_obj_t enable ) {
1070
1088
if (mp_obj_is_true (enable )) {
1089
+ // some sensible time to wait for the advertisement configuration to complete
1090
+ mp_hal_delay_ms (50 );
1071
1091
esp_ble_gap_start_advertising (& bt_adv_params );
1072
1092
bt_obj .advertising = true;
1073
1093
} else {
0 commit comments