使用 nrf52 发送 Ble 数据

Ble data sending with nrf52

我正在使用 nrf52,我为 17 字节数据数组创建了一个自定义服务,我想发送它,但在数据更新(发送)功能和重置程序时出现致命错误。

这是我的自定义服务数据更新函数:

uint32_t ble_cus_mydata_update(ble_cus_t * p_cus, uint8_array_t mydata, uint16_t conn_handle)
{

if (p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)
{
    ble_gatts_hvx_params_t params;

memset(&params, 0, sizeof(params));
params.type   = BLE_GATT_HVX_NOTIFICATION;
params.handle = p_cus->mydata_char_handles.value_handle;
params.p_data = &mydata;
params.p_len  = 17;

return sd_ble_gatts_hvx(conn_handle, &params); 
}}

我用timeout_handler

发送数据
static void mydata_timeout_handler(void * p_context)

{
    ret_code_t      err_code;

    UNUSED_PARAMETER(p_context);

bsp_board_led_on(2);
nrf_delay_ms(100);
bsp_board_led_off(2);


uint8_array_t mydata_value [17] = {0x55,0x10,0x01,0x23,0x99,0xFF,0xFF,0xCC,0xBB,0x00,0x00,0x00,0x00,0x00,0x24,0x24,0x20};
err_code = ble_cus_mydata_update(&m_cus, *mydata_value , m_conn_handle);

if ((err_code != NRF_SUCCESS) &&
    (err_code != NRF_ERROR_INVALID_STATE) &&
    (err_code != NRF_ERROR_RESOURCES) &&
    (err_code != NRF_ERROR_BUSY) &&
    (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
   )
{
    APP_ERROR_HANDLER(err_code);


  }

}

这是我的服务字符添加

 uint8_array_t mydata_char_init_value [17] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid              = MYDATA_CHAR_UUID;
add_char_params.uuid_type         = p_cus->uuid_type;

add_char_params.init_len          = 17;
add_char_params.max_len           = 17;
add_char_params.p_init_value      = mydata_char_init_value;

add_char_params.char_props.read   = 1;
add_char_params.char_props.notify = 1;

add_char_params.read_access       = SEC_OPEN;
add_char_params.cccd_write_access = SEC_OPEN;

err_code = characteristic_add(p_cus->service_handle,
                              &add_char_params,
                              &p_cus->mydata_char_handles);
if (err_code != NRF_SUCCESS)
{
    return err_code;
}

我得到一个错误:

nrf error_code= 0x2000310c

没有关于此错误信息的信息;它抛出这个致命错误并在错误处理程序后转到 nrf_breakpoint_cond 并自行重置。

请帮帮我;我尝试了我所知道的一切,但我无法前进。提前感谢任何试图帮助我的人。

err_code = ble_cus_mydata_update(&m_cus, *mydata_value , m_conn_handle); 不应该是 err_code = ble_cus_mydata_update(&m_cus, mydata_value , m_conn_handle); 吗?

我解决了它并想帮助其他可能需要它的人它有点不同这段代码用于发送 4 字节的数据你需要对 17 字节做一些小的改动但是其他代码并不相同你只需要将更新功能更改为这样的东西

uint32_t ble_cus_pres_level_update(ble_cus_t * p_cus, uint32_t presvalue)
    {
    //    NRF_LOG_INFO("In ble_cus_presvalue_update. \r\n"); 
        if (p_cus == NULL)
        {
            return NRF_ERROR_NULL;
        }

    uint32_t err_code = NRF_SUCCESS;
    ble_gatts_value_t gatts_value;

    // Initialize value struct.
    memset(&gatts_value, 0, sizeof(gatts_value));

    gatts_value.len     = sizeof(uint32_t);
    gatts_value.offset  = 0;
    gatts_value.p_value = &presvalue;

    // Update database.
    err_code = sd_ble_gatts_value_set(p_cus->conn_handle,
                                      p_cus->bme280_presvalue_char_handles.value_handle,
                                      &gatts_value);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }
 // Send value if connected and notifying.

if (p_cus->conn_handle != BLE_CONN_HANDLE_INVALID)
{
    ble_gatts_hvx_params_t params;

    memset(&params, 0, sizeof(params));
    params.type   = BLE_GATT_HVX_NOTIFICATION;
    params.handle = p_cus->bme280_presvalue_char_handles.value_handle;
    params.offset = gatts_value.offset;
    params.p_data = gatts_value.p_value;
    params.p_len  = &gatts_value.len;

        err_code = sd_ble_gatts_hvx(p_cus->conn_handle, &params);
//        NRF_LOG_INFO("sd_ble_gatts_hvx result: %x. \r\n", err_code); 
    }
    else
    {
        err_code = NRF_ERROR_INVALID_STATE;
//        NRF_LOG_INFO("sd_ble_gatts_hvx result: NRF_ERROR_INVALID_STATE. \r\n"); 
    }


    return err_code;
}