低功耗蓝牙广播包,代码解析例子
时间: 2023-07-03 07:16:50 浏览: 104
以下是一个低功耗蓝牙广播包的代码解析例子,以 Nordic nRF5 SDK 为例:
```
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nordic_common.h"
#include "boards.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_drv_clock.h"
#include "nrf_drv_power.h"
#include "nrf_drv_rng.h"
#include "nrf_drv_saadc.h"
#include "nrfx_wdt.h"
#include "app_error.h"
#include "app_timer.h"
#include "app_util_platform.h"
#include "ble_advdata.h"
#include "ble_gap.h"
#include "ble_nus.h"
#include "ble_hci.h"
#include "ble_conn_params.h"
#include "ble_conn_state.h"
#include "ble_db_discovery.h"
#include "ble_hci.h"
#include "nrf_ble_gatt.h"
#include "nrf_ble_qwr.h"
#include "nrf_ble_scan.h"
#include "nrf_ble_lesc.h"
#include "nrf_ble_conn_params.h"
#include "nrf_ble_gq.h"
#include "nrf_ble_ancs_c.h"
#include "nrf_ble_ans_c.h"
#include "nrf_ble_bms_c.h"
#include "nrf_ble_cscs_c.h"
#include "nrf_ble_gattc.h"
#include "nrf_ble_ias_c.h"
#include "nrf_ble_lbs_c.h"
#include "nrf_ble_rscs_c.h"
#include "nrf_ble_tps_c.h"
#include "nrf_ble_wscs_c.h"
#include "bsp_btn_ble.h"
#define DEVICE_NAME "LEDBlinker" /**< Name of device. Will be included in the advertising data. */
#define MANUFACTURER_NAME "NordicSemiconductor" /**< Manufacturer. Will be passed to Device Information Service. */
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define APP_ADV_INTERVAL 1600 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 1 sec). */
#define APP_ADV_DURATION 0 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
#define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define APP_FEATURE_NOT_SUPPORTED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2 /**< Reply when unsupported features are requested. */
static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
static uint8_t m_enc_scandata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
static void advertising_start(void)
{
ret_code_t err_code;
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
static void advertising_init(void)
{
ret_code_t err_code;
ble_advdata_t advdata;
ble_advdata_manuf_data_t manuf_data;
manuf_data.company_identifier = 0xFFFF;
manuf_data.data.p_data = (uint8_t *) "Hello";
manuf_data.data.size = 5;
memset(&advdata, 0, sizeof(advdata));
advdata.name_type = BLE_ADVDATA_FULL_NAME;
advdata.include_appearance = true;
advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
advdata.p_manuf_specific_data = &manuf_data;
memset(m_enc_advdata, 0, sizeof(m_enc_advdata));
memset(m_enc_scandata, 0, sizeof(m_enc_scandata));
err_code = ble_advdata_encode(&advdata, m_enc_advdata, &m_adv_params.adv_data.len);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_data_set(m_adv_handle, m_enc_advdata, m_adv_params.adv_data.len, m_enc_scandata, m_adv_params.scan_rsp_data.len);
APP_ERROR_CHECK(err_code);
}
static void gap_params_init(void)
{
ret_code_t err_code;
ble_gap_conn_params_t gap_conn_params;
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, m_adv_handle, 4);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_conn_params_get(&gap_conn_params);
APP_ERROR_CHECK(err_code);
gap_conn_params.min_conn_interval = MSEC_TO_UNITS(20, UNIT_1_25_MS);
gap_conn_params.max_conn_interval = MSEC_TO_UNITS(75, UNIT_1_25_MS);
gap_conn_params.slave_latency = 0;
gap_conn_params.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS);
err_code = sd_ble_gap_conn_params_set(&gap_conn_params);
APP_ERROR_CHECK(err_code);
}
int main(void)
{
ret_code_t err_code;
err_code = nrf_drv_clock_init();
APP_ERROR_CHECK(err_code);
nrf_drv_clock_lfclk_request(NULL);
err_code = nrf_drv_power_init(NULL);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_rng_init(NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
err_code = bsp_init(BSP_INIT_LED, NULL);
APP_ERROR_CHECK(err_code);
err_code = sd_ble_gap_adv_set_configure(&m_adv_handle, &m_enc_advdata[0], &m_adv_params, &m_enc_scandata[0]);
APP_ERROR_CHECK(err_code);
gap_params_init();
advertising_init();
advertising_start();
while (1)
{
// Do nothing.
}
}
```
该例子使用了 Nordic nRF5 SDK,实现了一个低功耗蓝牙广播包的发送。在 `advertising_init()` 函数中,定义了广播包的参数和内容,使用 `ble_advdata_encode()` 函数将广播包编码成二进制数据,并通过 `sd_ble_gap_adv_data_set()` 函数设置广播包数据。在 `advertising_start()` 函数中,启动了广播传输。
需要注意的是,该例子使用了 Nordic nRF5 SDK,如果您使用其他的嵌入式平台和开发工具,代码会有所不同。但是,低功耗蓝牙广播包的实现原理是相同的。