Created
May 1, 2017 18:19
-
-
Save ciniml/09f315d6fbda369b8c37f9bee708ed89 to your computer and use it in GitHub Desktop.
ESP32 Advertising example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Esp.h> | |
#include <esp32-hal-bt.h> | |
#include "bt.h" | |
#include "esp_bt_main.h" | |
#include "esp_gap_ble_api.h" | |
static void checkResult(esp_err_t err, const __FlashStringHelper* s) | |
{ | |
if (err != ESP_OK) { | |
Serial.printf("[ERR %08x] ", err); | |
Serial.println(s); | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
if (!btStart()) Serial.println(F("Failed to initialize bluetooth controller.")); | |
checkResult(esp_bluedroid_init(), F("Failed to initialize bluedroid.")); | |
checkResult(esp_bluedroid_enable(), F("Failed to enable bluedroid.")); | |
checkResult(esp_ble_gap_set_device_name("HogeDevice"), F("Failed to set device name")); | |
esp_ble_adv_data_t advData = { 0 }; | |
advData.flag = ESP_BLE_ADV_FLAG_NON_LIMIT_DISC; | |
advData.include_name = true; | |
advData.min_interval = 0x0100; | |
advData.max_interval = 0x0100; | |
checkResult(esp_ble_gap_config_adv_data(&advData), F("Failed to configure advertisement data.")); | |
esp_ble_adv_params_t advParams = { 0 }; | |
advParams.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY; | |
advParams.adv_int_max = 0x0100; | |
advParams.adv_int_min = 0x0100; | |
advParams.adv_type = ADV_TYPE_NONCONN_IND; | |
advParams.channel_map = ADV_CHNL_ALL; | |
advParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC; | |
checkResult(esp_ble_gap_start_advertising(&advParams), F("Failed to start advertising.")); | |
} | |
void loop() | |
{ | |
auto status = esp_bluedroid_get_status(); | |
Serial.printf("bluedroid status: %02x\n", status); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment