This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Adding Baud Rate Characteristic to UART example

Hello All,

I am implementing a simple UART bridge over BLE and want to add a Characteristic to update the baud rate being used. I thought I added code to support a new characteristic everywhere it needs to go, but when I scan for characteristics with the nRF connect app or another BLE scanner, it does not show up in the list.  The plan is to look for the newly defined BLE_NUS_EVT_BAUD event in the nus_data_handler and update the baud rate accordingly. Is there anything I am missing here when it comes to adding a characteristic?

I added the following lines of code to the UART example:

in ble_nus.h


struct ble_nus_s
{
uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the SoftDevice). */
ble_gatts_char_handles_t tx_handles; /**< Handles related to the TX characteristic (as provided by the SoftDevice). */
ble_gatts_char_handles_t baud_handles;
ble_gatts_char_handles_t rx_handles; /**< Handles related to the RX characteristic (as provided by the SoftDevice). */
blcm_link_ctx_storage_t * const p_link_ctx_storage; /**< Pointer to link context storage with handles of all current connections and its context. */
ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
};

in ble_nus.c

#define BLE_UUID_NUS_BAUD_CHARACTERISTIC 0x0004 /**< The UUID of the BAUD Characteristic. */
#define BLE_UUID_NUS_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
#define BLE_UUID_NUS_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */

#define BLE_NUS_MAX_BAUD_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**< Maximum length of the BAUD Characteristic (in bytes). */
#define BLE_NUS_MAX_RX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**< Maximum length of the RX Characteristic (in bytes). */
#define BLE_NUS_MAX_TX_CHAR_LEN BLE_NUS_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */

in on_write in ble_nus.c

else if ((p_evt_write->handle == p_nus->baud_handles.value_handle) &&
(p_nus->data_handler != NULL))
{
evt.type = BLE_NUS_EVT_BAUD;
evt.params.rx_data.p_data = p_evt_write->data;
evt.params.rx_data.length = p_evt_write->len;

p_nus->data_handler(&evt);
}

in ble_nus_init in ble_nus.c

// Add the BAUD Characteristic.
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = BLE_UUID_NUS_BAUD_CHARACTERISTIC;
add_char_params.uuid_type = p_nus->uuid_type;
add_char_params.max_len = BLE_NUS_MAX_RX_CHAR_LEN;
add_char_params.init_len = sizeof(uint8_t);
add_char_params.is_var_len = true;
add_char_params.char_props.write = 1;
add_char_params.char_props.read = 1;

add_char_params.read_access = SEC_OPEN;
add_char_params.write_access = SEC_OPEN;

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

Related