#include "nimble/ble.h" #include "esp_err.h" #include "host/ble_gap.h" #include "host/ble_hs_adv.h" #include "nimble/nimble_port.h" #include "nimble/nimble_port_freertos.h" #include "services/gap/ble_svc_gap.h" #include struct ble_hs_adv_fields adv_fields = { .tx_pwr_lvl_is_present = 1, .tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO, .flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP, .name = (uint8_t*)&"Power Profiler"[0], .name_len = 15, .name_is_complete = 1, }; struct ble_gap_adv_params adv_params = { .conn_mode = BLE_GAP_CONN_MODE_UND, .disc_mode = BLE_GAP_DISC_MODE_GEN, }; static void ble_host_task(){ nimble_port_run(); nimble_port_freertos_deinit(); } static void ble_advertise(void){ ESP_ERROR_CHECK(ble_gap_adv_set_fields(&adv_fields)); ESP_ERROR_CHECK(ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, &adv_params, NULL, NULL)); } static void gatt_svr_init(void){ ble_svc_gap_init(); } static void ble_on_sync(void){ ble_advertise(); } static void ble_on_reset(int p){ } void initBLE(){ nimble_port_init(); ble_hs_cfg.sync_cb = ble_on_sync; ble_hs_cfg.reset_cb = ble_on_reset; gatt_svr_init(); ESP_ERROR_CHECK(ble_svc_gap_device_name_set("Power Profiler")); nimble_port_freertos_init(ble_host_task); }