Skip to content

Commit b2e1dbd

Browse files
committed
Implement name and address get/set
1 parent 5a6f456 commit b2e1dbd

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,40 @@ bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
113113
}
114114

115115
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
116-
return NULL;
116+
uint8_t address_bytes[6];
117+
uint8_t address_type = BLE_ADDR_RANDOM;
118+
ble_hs_id_infer_auto(0, &address_type);
119+
int result = ble_hs_id_copy_addr(address_type, address_bytes, NULL);
120+
if (result != 0) {
121+
return NULL;
122+
}
123+
124+
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
125+
address->base.type = &bleio_address_type;
126+
common_hal_bleio_address_construct(address, address_bytes, BLEIO_ADDRESS_TYPE_RANDOM_STATIC);
127+
return address;
117128
}
118129

119130
bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) {
120-
return false;
131+
if (address->type != BLEIO_ADDRESS_TYPE_RANDOM_STATIC) {
132+
return false;
133+
}
134+
mp_buffer_info_t bufinfo;
135+
if (!mp_get_buffer(address->bytes, &bufinfo, MP_BUFFER_READ)) {
136+
return false;
137+
}
138+
int result = ble_hs_id_set_rnd(bufinfo.buf);
139+
return result == 0;
121140
}
122141

123142
mp_obj_str_t *common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) {
124-
return NULL;
143+
const char *name = ble_svc_gap_device_name();
144+
145+
return mp_obj_new_str(name, strlen(name));
125146
}
126147

127148
void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *name) {
149+
ble_svc_gap_device_name_set(name);
128150
}
129151

130152
static int _scan_event(struct ble_gap_event *event, void *scan_results_in) {

0 commit comments

Comments
 (0)