自定义广播字节内容

yangzh · 246次点击 · 2023-09-09

使用中,有需要用到自己定义广播字节内容,比如MAC地址,一定意义的字节等,这里以simple为示例,介绍广播MAC地址的方法

1)打开app. c文件,找到

appm_build_adv_data  函数


2)修改函数内容

static void appm_build_adv_data(uint16_t max_length, uint16_t *p_length, uint8_t *p_buf)
{
    // Remaining Length
    uint8_t rem_len = max_length;
    uint8_t manufa[8] = {0};

    // Get mac
    nvds_tag_len_t temp_len = 6;
    uint8_t addr_temp[6] = {0};
    nvds_get(NVDS_TAG_BD_ADDRESS, &temp_len, addr_temp);

    // Manufacturer Specific Data
    manufa[0] = sizeof(manufa) - 1;
    manufa[1] = APP_ADV_DATATYPE_MANUFACTURER_SPECIFIC_DATA;
    manufa[2] = addr_temp[0]; // MAC地址0
    manufa[3] = addr_temp[1]; // MAC地址1
    manufa[4] = addr_temp[2]; // MAC地址2
    manufa[5] = addr_temp[3]; // MAC地址3
    manufa[6] = addr_temp[4]; // MAC地址4
    manufa[7] = addr_temp[5]; // MAC地址5

    // Set Manufacturer
    memcpy(p_buf, manufa, sizeof(manufa));
    *p_length += sizeof(manufa);
    p_buf += sizeof(manufa);

    // Set list of UUIDs
    memcpy(p_buf, APP_ADV_DATA_UUID, APP_ADV_DATA_UUID_LEN);
    *p_length += APP_ADV_DATA_UUID_LEN;
    p_buf += APP_ADV_DATA_UUID_LEN;

    // Sanity check
    ASSERT_ERR(rem_len >= max_length);

    // Get remaining space in the Advertising Data - 2 bytes are used for name length/flag
    rem_len -= *p_length;

    // Check if additional data can be added to the Advertising data - 2 bytes needed for type and length
    if (rem_len > 2)
    {
        uint8_t dev_name_length = co_min(app_env.dev_name_len, (rem_len - 2));

        // Device name length
        *p_buf = dev_name_length + 1;
        // Device name flag (check if device name is complete or not)
        *(p_buf + 1) = '\x09';
        // Copy device name
        memcpy(p_buf + 2, app_env.dev_name, dev_name_length);

        // Update advertising data length
        *p_length += (dev_name_length + 2);
    }
}


3)因常用的广播字节不能超过31字节,所以自定义数组不能过大,示例定义的是8个字节(6个自定义字节)

4)nrf 测试如下:

image.png


被收藏 0  ∙  0 赞  
加入收藏
点赞
0 回复  
善言善语 (您需要 登录 后才能回复 没有账号 ?)

请先登录网站