Created
May 21, 2017 17:13
-
-
Save ciniml/7c0ea33b95e6f9db35b7ed8c1e094625 to your computer and use it in GitHub Desktop.
esp32_ble_gattc_scan
This file contains 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" | |
#include "esp_gattc_api.h" | |
static esp_gatt_if_t appGattcIf = ESP_GATT_IF_NONE; | |
static constexpr uint16_t appId = 0x0001u; | |
static esp_ble_scan_params_t scanParams; | |
static void checkResult(esp_err_t err, const __FlashStringHelper* s) | |
{ | |
if (err != ESP_OK) { | |
Serial.printf("[ERR %08x] ", err); | |
Serial.println(s); | |
} | |
} | |
void gapBleCallback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) | |
{ | |
switch (event) | |
{ | |
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: { // Scan initiated. | |
if (param->scan_start_cmpl.status == ESP_BT_STATUS_SUCCESS) { | |
// Scan started. | |
} | |
break; | |
} | |
case ESP_GAP_BLE_SCAN_RESULT_EVT: { | |
// Scan result. | |
Serial.printf("Device: %02x:%02x:%02x:%02x:%02x:%02x", | |
param->scan_rst.bda[0], | |
param->scan_rst.bda[1], | |
param->scan_rst.bda[2], | |
param->scan_rst.bda[3], | |
param->scan_rst.bda[4], | |
param->scan_rst.bda[5]); | |
Serial.printf(" RSSI: %d\n", param->scan_rst.rssi); | |
break; | |
} | |
} | |
} | |
void gattCallback(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) | |
{ | |
switch (event) | |
{ | |
case ESP_GATTC_REG_EVT: { | |
if (param->reg.status == ESP_GATT_OK && param->reg.app_id == appId) { | |
appGattcIf = gattc_if; | |
} | |
break; | |
} | |
} | |
} | |
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_gattc_register_callback(gattCallback), F("Failed to register GATT callback")); | |
checkResult(esp_ble_gap_register_callback(gapBleCallback), F("Failed to register GAP callback.")); | |
checkResult(esp_ble_gap_set_device_name("HogeDevice"), F("Failed to set device name")); | |
checkResult(esp_ble_gattc_app_register(appId), F("Failed to register app.")); | |
scanParams.scan_type = BLE_SCAN_TYPE_PASSIVE; // Passive scan. | |
scanParams.own_addr_type = BLE_ADDR_TYPE_PUBLIC; | |
scanParams.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL; // All | |
scanParams.scan_interval = 0x0010; // 10[ms] | |
scanParams.scan_window = 0x0010; // 10[ms] | |
checkResult(esp_ble_gap_set_scan_params(&scanParams), F("Failed to set scan params.")); | |
checkResult(esp_ble_gap_start_scanning(60), F("Failed to start scanning.")); | |
} | |
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