510
个编辑
更改
→源码讲解
==== 源码讲解 ====
<syntaxhighlight lang="c" line="1" start="54">
// BEACON数据
#define APP_BEACON_INFO_LENGTH 0x17 // BEACON数据总长度
#define APP_ADV_DATA_LENGTH 0x15 // BEACON特殊字节的长度
#define APP_DEVICE_TYPE 0x02 // 字节0x02代表这个设备是BEACON
#define APP_MEASURED_RSSI 0xC3 // BEACON在1米距离处的信号强度
#define APP_COMPANY_IDENTIFIER 0x004C // 004C代表的是Apple id(只有这个ID,设备才会叫iBeacon)
#define APP_MAJOR_VALUE 0x27, 0x12 // major
#define APP_MINOR_VALUE 0x0B, 0x86 // minor
#define APP_BEACON_UUID 0xFD,0xA5,0x06,0x93, \
0xA4,0xE2,0x4F,0xB1, \
0xAF,0xCF,0xC6,0xEB, \
0x07,0x64,0x78,0x25 // UUID
// BEACON数据数组,用于初始化广播数据内容
static uint8_t m_beacon_info[APP_BEACON_INFO_LENGTH] =
{
APP_DEVICE_TYPE,
APP_ADV_DATA_LENGTH,
APP_BEACON_UUID,
APP_MAJOR_VALUE,
APP_MINOR_VALUE,
APP_MEASURED_RSSI
};
</syntaxhighlight><syntaxhighlight lang="c" line="1" start="110">
//******************************************************************
// fn : advertising_init
//
// brief : 用于初始化广播
//
// param : none
//
// return : none
static void advertising_init(void)
{
uint32_t err_code;
ble_advertising_init_t init;
ble_advdata_manuf_data_t manuf_specific_data;
memset(&init, 0, sizeof(init));
// 扫描回调数据包含短设备名称(方便大家看app显示BEACON标志)
init.srdata.name_type = BLE_ADVDATA_SHORT_NAME;
init.srdata.short_name_len = 9;
// 配置广播数据的flag,设置为beacon,配置为BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED
init.advdata.flags = BLE_GAP_ADV_FLAG_LE_GENERAL_DISC_MODE|BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
// 配置广播数据中的BEACON数据
manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
manuf_specific_data.data.p_data = (uint8_t *) m_beacon_info;
manuf_specific_data.data.size = APP_BEACON_INFO_LENGTH;
init.advdata.p_manuf_specific_data = &manuf_specific_data;
// 配置广播周期,40ms快速广播,超时参数配置为0代表一直广播
init.config.ble_adv_fast_enabled = true;
init.config.ble_adv_fast_interval = 64; // 64*0.625 = 40ms
init.config.ble_adv_fast_timeout = 0; // 1800*10ms = 18s
err_code = ble_advertising_init(&m_advertising, &init);
APP_ERROR_CHECK(err_code);
ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}
</syntaxhighlight>
=== 从机NUS实验 ===