extmod/modbluetooth_nimble: Use random addr if public isn't available.
@@ -277,7 +277,30 @@ STATIC void reset_cb(int reason) {
}
STATIC void sync_cb(void) {
- ble_hs_util_ensure_addr(0); // prefer public address
+ int rc;
+ ble_addr_t addr;
+
+ rc = ble_hs_util_ensure_addr(0); // prefer public address
+ if (rc != 0) {
+ // https://mynewt.apache.org/latest/tutorials/ble/eddystone.html#configure-the-nimble-stack-with-an-address
+ #if MICROPY_PY_BLUETOOTH_RANDOM_ADDR
+ rc = ble_hs_id_gen_rnd(1, &addr);
+ assert(rc == 0);
+ rc = ble_hs_id_set_rnd(addr.val);
+ assert(rc == 0);
+ #else
+ uint8_t addr_be[6];
+ mp_hal_get_mac(MP_HAL_MAC_BDADDR, addr_be);
+ reverse_addr_byte_order(addr.val, addr_be);
+ // ble_hs_id_set_pub(addr.val);
+ rc = ble_hs_id_set_rnd(addr.val);
+ assert(rc == 0);
+ #endif
+
+ rc = ble_hs_util_ensure_addr(0); // prefer public address
+ assert(rc == 0);
+ }
+
ble_svc_gap_device_name_set(MICROPY_PY_BLUETOOTH_DEFAULT_NAME);
ble_state = BLE_STATE_ACTIVE;
@@ -461,12 +484,24 @@ int mp_bluetooth_gap_advertise_start(bool connectable, uint16_t interval_ms, con
};
ret = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
- if (ret != 0) {
- return ble_hs_err_to_errno(ret);
+ if (ret == 0) {
+ return 0;
}
-
+ ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
+ if (ret == 0) {
return 0;
}
+ ret = ble_gap_adv_start(BLE_OWN_ADDR_RPA_RANDOM_DEFAULT, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
+ if (ret == 0) {
+ return 0;
+ }
+ ret = ble_gap_adv_start(BLE_OWN_ADDR_RANDOM, NULL, BLE_HS_FOREVER, &adv_params, gap_event_cb, NULL);
+ if (ret == 0) {
+ return 0;
+ }
+
+ return ble_hs_err_to_errno(ret);
+}
void mp_bluetooth_gap_advertise_stop(void) {
if (ble_gap_adv_active()) {
