Hi,
I modified the LBS service to experiment sending more than a byte of data.
On the peripheral side, modified from ble_peripheral/ble_app_blinky
typedef struct
{
uint8_t data[8]; /* 8 bytes data */
} ble_button_t;
...
memset(&add_char_params, 0, sizeof(add_char_params));
add_char_params.uuid = LBS_UUID_BUTTON_CHAR;
add_char_params.uuid_type = p_lbs->uuid_type;
// add_char_params.init_len = sizeof(uint8_t);
// add_char_params.max_len = sizeof(uint8_t);
add_char_params.init_len = sizeof(ble_button_t);
add_char_params.max_len = sizeof(ble_button_t);
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
ret_code_t err_code;
static uint8_t counter = 0;
switch (pin_no)
{
case LEDBUTTON_BUTTON:
NRF_LOG_INFO("Send button state change.");
m_button_data.data[0] = button_action;
counter++;
m_button_data.data[1] = counter;
printf("sending %d bytes - ", sizeof(m_button_data));
for (int i = 0; i < sizeof(m_button_data); i++) {
printf("%2.2x ", m_button_data.data[i]);
}
printf("\r\n");
err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, &m_button_data);
if (err_code != NRF_SUCCESS &&
err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
err_code != NRF_ERROR_INVALID_STATE &&
err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
{
APP_ERROR_CHECK(err_code);
}
On the central side,
static void on_hvx(ble_lbs_c_t * p_ble_lbs_c, ble_evt_t const * p_ble_evt)
{
// Check if the event is on the link for this instance.
if (p_ble_lbs_c->conn_handle != p_ble_evt->evt.gattc_evt.conn_handle)
{
return;
}
// Check if this is a Button notification.
if (p_ble_evt->evt.gattc_evt.params.hvx.handle == p_ble_lbs_c->peer_lbs_db.button_handle)
{
if (p_ble_evt->evt.gattc_evt.params.hvx.len == 1)
{
ble_lbs_c_evt_t ble_lbs_c_evt;
ble_lbs_c_evt.evt_type = BLE_LBS_C_EVT_BUTTON_NOTIFICATION;
ble_lbs_c_evt.conn_handle = p_ble_lbs_c->conn_handle;
ble_lbs_c_evt.params.button.button_state = p_ble_evt->evt.gattc_evt.params.hvx.data[0];
memcpy(ble_lbs_c_evt.params.button.data, &p_ble_evt->evt.gattc_evt.params.hvx.data[1], sizeof(ble_lbs_c_evt.params.button.data));
p_ble_lbs_c->evt_handler(p_ble_lbs_c, &ble_lbs_c_evt);
} else {
printf("hvx len: %d - ", p_ble_evt->evt.gattc_evt.params.hvx.len);
for (int i = 0; i < p_ble_evt->evt.gattc_evt.params.hvx.len; i++) {
printf("%2.2x ", p_ble_evt->evt.gattc_evt.params.hvx.data[i]);
}
printf("\r\n");
}
}
}
I could see the data sent was as expected, the first 2 bytes were button state followed by counter.
However the central received different data
hvx len: 8 - 21 2b 00 20 03 00 05 00
hvx len: 8 - 21 2b 00 20 03 00 05 00
hvx len: 8 - 21 2b 00 20 03 00 05 00
hvx len: 8 - 21 2b 00 20 03 00 05 00
hvx len: 8 - 21 2b 00 20 03 00 05 00
I can't figure out what was wrong.
Many thanks,
Job