Code: Select all
BluetoothDevice.connectGatt(context, true, callbacks); // Autoconnect allows the phone to passively scan for the device in the background and connect to it if seen
Code: Select all
esp_ble_adv_params_t advertise = {
.adv_int_min = BT_BLE_MIN_ADV_INT_MS * BT_BLE_ADV_INT_FACTOR,
.adv_int_max = BT_BLE_MAX_ADV_INT_MS * BT_BLE_ADV_INT_FACTOR,
.adv_type = ADV_TYPE_DIRECT_IND_HIGH,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST,
};
int num = esp_ble_get_bond_device_num();
esp_ble_bond_dev_t *peers = alloca(num * sizeof(esp_ble_bond_dev_t));
if (!peers) {
return ESP_ERR_NO_MEM;
}
if (esp_ble_get_bond_device_list(&num, peers) != ESP_OK) {
return ESP_FAIL;
}
if ((peers[0].bond_key.key_mask & ESP_BLE_ID_KEY_MASK) != ESP_BLE_ID_KEY_MASK) {
return ESP_FAIL;
}
memcpy(advertise.peer_addr, peers[0].bond_key.pid_key.static_addr, sizeof(esp_bd_addr_t));
advertise.peer_addr_type = peers[0].bond_key.pid_key.addr_type;
if (esp_ble_gap_start_advertising(&advertise) != ESP_OK) {
return ESP_FAIL;
}