一.蓝牙广播包解析

蓝牙广播包解析

注意:蓝牙发包都是低bit先发

一.物理层

在这里插入图片描述
前导码:10101010b(0xaa)或者01010101b(0x55),根据接入地址与其相连的bit来决定,为了保证0和1交替。
接入地址:广播的接入地址是0x8e89bed6,这是固定的。
PDU:数据包。
CRC:对PDU进行24位的CRC校验。
这是最原始的包,通过白化后便可发出去。

二.PDU

在这里插入图片描述
包头:里面保存着广播类型和广播的地址类型。
长度:指广播pdu的长度。
广播PDU:广播的数据

三.广播PDU

在这里插入图片描述
设备地址:就是我们常说的MAC或者DB_ADDR,通常表示为11:22:33:44:55:66的形式。
广播数据:对于手机APP开发者开说,这段内容才是他们能见到的数据。

四.广播数据

广播数据是由很多个段组成的,只要不超31byte,多少个段都行
在这里插入图片描述

Length:本段的长度(AD type和AD data的长度)。
AD type:本段代表的类型,例如设备名字,设备UUID等。
AD data:本段的数据,例如名字是什么,UUID是什么等。

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个低功耗蓝牙广播的代解析例子,以 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,如果您使用其他的嵌入式平台和开发工具,代会有所不同。但是,低功耗蓝牙广播的实现原理是相同的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值