2018-01-15 11:47:23 +00:00
|
|
|
/*
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2017-07-18 12:42:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
*
|
2017-07-25 06:36:00 +00:00
|
|
|
* This file is for iBeacon demo. It supports both iBeacon sender and receiver
|
|
|
|
* which is distinguished by macros IBEACON_SENDER and IBEACON_RECEIVER,
|
|
|
|
*
|
|
|
|
* iBeacon is a trademark of Apple Inc. Before building devices which use iBeacon technology,
|
|
|
|
* visit https://developer.apple.com/ibeacon/ to obtain a license.
|
2017-07-18 12:42:22 +00:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2017-07-25 06:36:00 +00:00
|
|
|
#include "nvs_flash.h"
|
2017-07-18 12:42:22 +00:00
|
|
|
|
2017-12-07 13:48:27 +00:00
|
|
|
#include "esp_bt.h"
|
2017-07-18 12:42:22 +00:00
|
|
|
#include "esp_gap_ble_api.h"
|
|
|
|
#include "esp_gattc_api.h"
|
|
|
|
#include "esp_gatt_defs.h"
|
|
|
|
#include "esp_bt_main.h"
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "esp_bt_defs.h"
|
2017-09-04 13:17:09 +00:00
|
|
|
#include "esp_ibeacon_api.h"
|
2018-04-08 04:10:50 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
2017-07-18 12:42:22 +00:00
|
|
|
|
2017-07-25 06:36:00 +00:00
|
|
|
static const char* DEMO_TAG = "IBEACON_DEMO";
|
2017-09-04 13:17:09 +00:00
|
|
|
extern esp_ble_ibeacon_vendor_t vendor_config;
|
2017-07-25 06:36:00 +00:00
|
|
|
|
2017-07-18 12:42:22 +00:00
|
|
|
///Declare static functions
|
|
|
|
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
|
|
|
|
|
|
|
|
#if (IBEACON_MODE == IBEACON_RECEIVER)
|
|
|
|
static esp_ble_scan_params_t ble_scan_params = {
|
|
|
|
.scan_type = BLE_SCAN_TYPE_ACTIVE,
|
|
|
|
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
|
|
|
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
|
|
|
|
.scan_interval = 0x50,
|
2018-05-03 12:22:08 +00:00
|
|
|
.scan_window = 0x30,
|
|
|
|
.scan_duplicate = BLE_SCAN_DUPLICATE_DISABLE
|
2017-07-18 12:42:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#elif (IBEACON_MODE == IBEACON_SENDER)
|
|
|
|
static esp_ble_adv_params_t ble_adv_params = {
|
|
|
|
.adv_int_min = 0x20,
|
|
|
|
.adv_int_max = 0x40,
|
|
|
|
.adv_type = ADV_TYPE_NONCONN_IND,
|
|
|
|
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
|
|
|
|
.channel_map = ADV_CHNL_ALL,
|
|
|
|
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static void esp_gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
|
|
|
|
{
|
2018-02-22 13:48:53 +00:00
|
|
|
esp_err_t err;
|
|
|
|
|
2017-07-18 12:42:22 +00:00
|
|
|
switch (event) {
|
|
|
|
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:{
|
|
|
|
#if (IBEACON_MODE == IBEACON_SENDER)
|
2017-07-25 06:36:00 +00:00
|
|
|
esp_ble_gap_start_advertising(&ble_adv_params);
|
2017-07-18 12:42:22 +00:00
|
|
|
#endif
|
2017-07-25 06:36:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-07-18 12:42:22 +00:00
|
|
|
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: {
|
|
|
|
#if (IBEACON_MODE == IBEACON_RECEIVER)
|
|
|
|
//the unit of the duration is second, 0 means scan permanently
|
|
|
|
uint32_t duration = 0;
|
|
|
|
esp_ble_gap_start_scanning(duration);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT:
|
|
|
|
//scan start complete event to indicate scan start successfully or failed
|
2018-02-22 13:48:53 +00:00
|
|
|
if ((err = param->scan_start_cmpl.status) != ESP_BT_STATUS_SUCCESS) {
|
|
|
|
ESP_LOGE(DEMO_TAG, "Scan start failed: %s", esp_err_to_name(err));
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-07-25 06:36:00 +00:00
|
|
|
case ESP_GAP_BLE_ADV_START_COMPLETE_EVT:
|
2017-07-18 12:42:22 +00:00
|
|
|
//adv start complete event to indicate adv start successfully or failed
|
2018-02-22 13:48:53 +00:00
|
|
|
if ((err = param->adv_start_cmpl.status) != ESP_BT_STATUS_SUCCESS) {
|
|
|
|
ESP_LOGE(DEMO_TAG, "Adv start failed: %s", esp_err_to_name(err));
|
2017-07-25 06:36:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-07-18 12:42:22 +00:00
|
|
|
case ESP_GAP_BLE_SCAN_RESULT_EVT: {
|
|
|
|
esp_ble_gap_cb_param_t *scan_result = (esp_ble_gap_cb_param_t *)param;
|
|
|
|
switch (scan_result->scan_rst.search_evt) {
|
|
|
|
case ESP_GAP_SEARCH_INQ_RES_EVT:
|
2017-07-25 06:36:00 +00:00
|
|
|
/* Search for BLE iBeacon Packet */
|
2017-07-18 12:42:22 +00:00
|
|
|
if (esp_ble_is_ibeacon_packet(scan_result->scan_rst.ble_adv, scan_result->scan_rst.adv_data_len)){
|
|
|
|
esp_ble_ibeacon_t *ibeacon_data = (esp_ble_ibeacon_t*)(scan_result->scan_rst.ble_adv);
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_LOGI(DEMO_TAG, "----------iBeacon Found----------");
|
2018-04-08 04:10:50 +00:00
|
|
|
esp_log_buffer_hex("IBEACON_DEMO: Device address:", scan_result->scan_rst.bda, ESP_BD_ADDR_LEN );
|
2017-07-18 12:42:22 +00:00
|
|
|
esp_log_buffer_hex("IBEACON_DEMO: Proximity UUID:", ibeacon_data->ibeacon_vendor.proximity_uuid, ESP_UUID_LEN_128);
|
|
|
|
|
2017-09-04 13:17:09 +00:00
|
|
|
uint16_t major = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.major);
|
|
|
|
uint16_t minor = ENDIAN_CHANGE_U16(ibeacon_data->ibeacon_vendor.minor);
|
|
|
|
ESP_LOGI(DEMO_TAG, "Major: 0x%04x (%d)", major, major);
|
|
|
|
ESP_LOGI(DEMO_TAG, "Minor: 0x%04x (%d)", minor, minor);
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_LOGI(DEMO_TAG, "Measured power (RSSI at a 1m distance):%d dbm", ibeacon_data->ibeacon_vendor.measured_power);
|
|
|
|
ESP_LOGI(DEMO_TAG, "RSSI of packet:%d dbm", scan_result->scan_rst.rssi);
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ESP_GAP_BLE_SCAN_STOP_COMPLETE_EVT:
|
2018-02-22 13:48:53 +00:00
|
|
|
if ((err = param->scan_stop_cmpl.status) != ESP_BT_STATUS_SUCCESS){
|
|
|
|
ESP_LOGE(DEMO_TAG, "Scan stop failed: %s", esp_err_to_name(err));
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_LOGI(DEMO_TAG, "Stop scan successfully");
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT:
|
2018-02-22 13:48:53 +00:00
|
|
|
if ((err = param->adv_stop_cmpl.status) != ESP_BT_STATUS_SUCCESS){
|
|
|
|
ESP_LOGE(DEMO_TAG, "Adv stop failed: %s", esp_err_to_name(err));
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_LOGI(DEMO_TAG, "Stop adv successfully");
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-25 06:36:00 +00:00
|
|
|
void ble_ibeacon_appRegister(void)
|
2017-07-18 12:42:22 +00:00
|
|
|
{
|
|
|
|
esp_err_t status;
|
|
|
|
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_LOGI(DEMO_TAG, "register callback");
|
2017-07-18 12:42:22 +00:00
|
|
|
|
|
|
|
//register the scan callback function to the gap module
|
|
|
|
if ((status = esp_ble_gap_register_callback(esp_gap_cb)) != ESP_OK) {
|
2018-02-22 13:48:53 +00:00
|
|
|
ESP_LOGE(DEMO_TAG, "gap register error: %s", esp_err_to_name(status));
|
2017-07-18 12:42:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-25 06:36:00 +00:00
|
|
|
void ble_ibeacon_init(void)
|
2017-07-18 12:42:22 +00:00
|
|
|
{
|
|
|
|
esp_bluedroid_init();
|
|
|
|
esp_bluedroid_enable();
|
2017-07-25 06:36:00 +00:00
|
|
|
ble_ibeacon_appRegister();
|
2017-07-18 12:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
2017-07-25 06:36:00 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2017-12-12 07:24:43 +00:00
|
|
|
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
|
2017-07-18 12:42:22 +00:00
|
|
|
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
|
|
|
|
esp_bt_controller_init(&bt_cfg);
|
2017-12-12 07:24:43 +00:00
|
|
|
esp_bt_controller_enable(ESP_BT_MODE_BLE);
|
2017-07-18 12:42:22 +00:00
|
|
|
|
2017-07-25 06:36:00 +00:00
|
|
|
ble_ibeacon_init();
|
2017-07-18 12:42:22 +00:00
|
|
|
|
|
|
|
/* set scan parameters */
|
|
|
|
#if (IBEACON_MODE == IBEACON_RECEIVER)
|
|
|
|
esp_ble_gap_set_scan_params(&ble_scan_params);
|
|
|
|
|
|
|
|
#elif (IBEACON_MODE == IBEACON_SENDER)
|
2017-07-25 06:36:00 +00:00
|
|
|
esp_ble_ibeacon_t ibeacon_adv_data;
|
2017-07-18 12:42:22 +00:00
|
|
|
esp_err_t status = esp_ble_config_ibeacon_data (&vendor_config, &ibeacon_adv_data);
|
|
|
|
if (status == ESP_OK){
|
2017-07-25 06:36:00 +00:00
|
|
|
esp_ble_gap_config_adv_data_raw((uint8_t*)&ibeacon_adv_data, sizeof(ibeacon_adv_data));
|
|
|
|
}
|
|
|
|
else {
|
2018-02-22 13:48:53 +00:00
|
|
|
ESP_LOGE(DEMO_TAG, "Config iBeacon data failed: %s\n", esp_err_to_name(status));
|
2017-07-25 06:36:00 +00:00
|
|
|
}
|
2017-07-18 12:42:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|