diff --git a/extmod/modbluetooth.c b/extmod/modbluetooth.c index cb153f70e9b5c..95e87d013df8f 100644 --- a/extmod/modbluetooth.c +++ b/extmod/modbluetooth.c @@ -628,6 +628,16 @@ STATIC mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t } STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, bluetooth_ble_gatts_register_services); +STATIC mp_obj_t bluetooth_ble_gatts_get_service_handle(mp_obj_t self_in, mp_obj_t service_uuid_in){ + (void)self_in; + + const mp_obj_bluetooth_uuid_t *service_uuid = MP_OBJ_TO_PTR(service_uuid_in); + uint16_t service_handle = mp_bluetooth_ble_gatts_get_service_handle(service_uuid); + + return mp_obj_new_int(service_handle); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_get_service_handle_obj, bluetooth_ble_gatts_get_service_handle); + #if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE STATIC mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) { uint8_t addr_type = mp_obj_get_int(args[1]); @@ -935,6 +945,7 @@ STATIC const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) }, { MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) }, + { MP_ROM_QSTR(MP_QSTR_gatts_get_service_handle), MP_ROM_PTR(&bluetooth_ble_gatts_get_service_handle_obj) }, #if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT // GATT Client { MP_ROM_QSTR(MP_QSTR_gattc_discover_services), MP_ROM_PTR(&bluetooth_ble_gattc_discover_services_obj) }, diff --git a/extmod/modbluetooth.h b/extmod/modbluetooth.h index 43519e5941a8b..a02375528f6d0 100644 --- a/extmod/modbluetooth.h +++ b/extmod/modbluetooth.h @@ -347,6 +347,9 @@ int mp_bluetooth_gatts_indicate(uint16_t conn_handle, uint16_t value_handle); // Append-mode means that remote writes will append and local reads will clear after reading. int mp_bluetooth_gatts_set_buffer(uint16_t value_handle, size_t len, bool append); +// Return the value handle of a service identified from its uuid. +uint16_t mp_bluetooth_ble_gatts_get_service_handle(const mp_obj_bluetooth_uuid_t *service_uuid_in); + // Disconnect from a central or peripheral. int mp_bluetooth_gap_disconnect(uint16_t conn_handle); diff --git a/extmod/nimble/modbluetooth_nimble.c b/extmod/nimble/modbluetooth_nimble.c index e4b4cb68af1af..9ba7971074b9d 100644 --- a/extmod/nimble/modbluetooth_nimble.c +++ b/extmod/nimble/modbluetooth_nimble.c @@ -1075,6 +1075,15 @@ int mp_bluetooth_set_preferred_mtu(uint16_t mtu) { return 0; } +uint16_t mp_bluetooth_ble_gatts_get_service_handle(const mp_obj_bluetooth_uuid_t *service_uuid_in){ + const ble_uuid_t *service_nimble_uuid = create_nimble_uuid(service_uuid_in, NULL); + uint16_t service_handle = 0; + + ble_gatts_find_svc(service_nimble_uuid, &service_handle); + + return service_handle; +} + #if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING int mp_bluetooth_gap_pair(uint16_t conn_handle) { DEBUG_printf("mp_bluetooth_gap_pair: conn_handle=%d\n", conn_handle);