Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 919c79f

Browse files
committed
esp32/network_bluetooth.c: Fix for disconnect/reconnect
https://github.com/MrSurly/micropython-esp32/issues/1
1 parent e00c2a3 commit 919c79f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

esp32/network_bluetooth.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ STATIC void network_bluetooth_gatt_id_print(const mp_print_t *print, const esp_g
453453
typedef enum {
454454
NETWORK_BLUETOOTH_FIND_SERVICE,
455455
NETWORK_BLUETOOTH_FIND_CONNECTION,
456+
NETWORK_BLUETOOTH_DEL_CONNECTION,
456457
NETWORK_BLUETOOTH_FIND_CHAR_DESCR_IN,
457458
NETWORK_BLUETOOTH_FIND_CHAR_DESCR,
458459
NETWORK_BLUETOOTH_DEL_SERVICE,
@@ -532,12 +533,19 @@ STATIC mp_obj_t network_bluetooth_item_op(mp_obj_t list, esp_bt_uuid_t* uuid, ui
532533
break;
533534

534535
case NETWORK_BLUETOOTH_FIND_CONNECTION:
536+
case NETWORK_BLUETOOTH_DEL_CONNECTION:
535537
{
536538
network_bluetooth_connection_obj_t* conn = (network_bluetooth_connection_obj_t*) items[i];
537539
if ((bda != NULL && memcmp(conn->bda, bda, ESP_BD_ADDR_LEN) == 0) || (bda == NULL && conn->conn_id == handle_or_conn_id)) {
538540
ret = conn;
539-
goto NETWORK_BLUETOOTH_ITEM_END;
540541
}
542+
543+
if (kind == NETWORK_BLUETOOTH_DEL_CONNECTION) {
544+
// a bit ineffecient, but
545+
// list_pop() is static
546+
mp_obj_list_remove(list, conn);
547+
}
548+
goto NETWORK_BLUETOOTH_ITEM_END;
541549
}
542550
break;
543551
}
@@ -609,6 +617,11 @@ STATIC mp_obj_t network_bluetooth_del_service_by_uuid(esp_bt_uuid_t* uuid) {
609617
return network_bluetooth_item_op(bluetooth->services, uuid, 0, NULL, NETWORK_BLUETOOTH_DEL_SERVICE);
610618
}
611619

620+
STATIC mp_obj_t network_bluetooth_del_connection_by_bda(esp_bd_addr_t bda) {
621+
network_bluetooth_obj_t* bluetooth = network_bluetooth_get_singleton();
622+
return network_bluetooth_item_op(bluetooth->connections, NULL, 0, bda, NETWORK_BLUETOOTH_DEL_CONNECTION);
623+
}
624+
612625
STATIC void network_bluetooth_print_bda(const mp_print_t *print, esp_bd_addr_t bda) {
613626
if (print != NULL) {
614627
mp_printf(print, "%02X:%02X:%02X:%02X:%02X:%02X", bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
@@ -2881,7 +2894,9 @@ STATIC mp_obj_t network_bluetooth_connection_disconnect(mp_obj_t self_in) {
28812894
network_bluetooth_connection_obj_t* connection = (network_bluetooth_connection_obj_t*) self_in;
28822895
if (connection->conn_id != -1) {
28832896
esp_ble_gattc_close(bluetooth->gattc_interface, connection->conn_id);
2897+
network_bluetooth_del_connection_by_bda(connection->bda);
28842898
connection->conn_id = -1;
2899+
connection->services = mp_obj_new_list(0, NULL);
28852900
}
28862901
return mp_const_none;
28872902
}
@@ -3396,6 +3411,14 @@ STATIC mp_obj_t network_bluetooth_services(mp_obj_t self_in) {
33963411
}
33973412
STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_bluetooth_services_obj, network_bluetooth_services);
33983413

3414+
// bluetooth.conns()
3415+
3416+
STATIC mp_obj_t network_bluetooth_conns(mp_obj_t self_in) {
3417+
network_bluetooth_obj_t* bluetooth = network_bluetooth_get_singleton();
3418+
return bluetooth->connections;
3419+
}
3420+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_bluetooth_conns_obj, network_bluetooth_conns);
3421+
33993422
// bluetooth.is_scanning()
34003423

34013424
STATIC mp_obj_t network_bluetooth_is_scanning(mp_obj_t self_in) {
@@ -3466,6 +3489,7 @@ STATIC const mp_rom_map_elem_t network_bluetooth_locals_dict_table[] = {
34663489
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_bluetooth_connect_obj) },
34673490
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&network_bluetooth_gatts_service_type) },
34683491
{ MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&network_bluetooth_services_obj) },
3492+
{ MP_ROM_QSTR(MP_QSTR_conns), MP_ROM_PTR(&network_bluetooth_conns_obj) },
34693493
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&network_bluetooth_callback_obj) },
34703494
{ MP_ROM_QSTR(MP_QSTR_scan_start), MP_ROM_PTR(&network_bluetooth_scan_start_obj) },
34713495
{ MP_ROM_QSTR(MP_QSTR_scan_stop), MP_ROM_PTR(&network_bluetooth_scan_stop_obj) },

0 commit comments

Comments
 (0)