以我目前开发使用的LSM6DSL传感器为例,通过蓝牙,把计算好的三轴角度数据以周期250ms发送到手机app。手机端可read可Notify查看传输过来的数据。
通过Simplicity Studio V5先通过图形化设计出自己的Service和characteristic。
这里自定义的ID字符为LSM6DSL_angle_X,这里后边程序会使用到这三个ID。LSM6DSL_angle_Y,LSM6DSL_angle_Z同LSM6DSL_angle_X类似。
下面介绍程序:
sl_gatt_service_lsm6dsl_on_event(evt);此函数为我自己添加的蓝牙event。
sl_gatt_service_lsm6dsl_on_event里涉及到3个回调函数:
lsm6dsl_connection_closed_cb(&evt->data.evt_connection_closed);
lsm6dsl_char_read_cb(&evt->data.evt_gatt_server_user_read_request);
lsm6dsl_char_config_changed_cb(&evt->data.evt_gatt_server_characteristic_status);
static void lsm6dsl_connection_closed_cb(sl_bt_evt_connection_closed_t * data)
{
(void)data;
sl_status_t sc;
// stop periodic timer
sc = sl_simple_timer_stop(&lsm6dsl_timer);
app_assert_status(sc);
// reset notification flags
lsm6dsl_angle_x_notification = false;
lsm6dsl_angle_y_notification = false;
lsm6dsl_angle_z_notification = false;
}
static void lsm6dsl_char_read_cb(sl_bt_evt_gatt_server_user_read_request_t * data)
{
sl_status_t sc;
// lsm6dsl_update();
switch (data->characteristic)
{
case gattdb_LSM6DSL_angle_X:
sc = sl_bt_gatt_server_send_user_read_response(
data->connection,
data->characteristic,
0,
sizeof(Angle_Roll_x100),
(uint8_t*)&Angle_Roll_x100,
NULL);
break;
case gattdb_LSM6DSL_angle_Y:
sc = sl_bt_gatt_server_send_user_read_response(
data->connection,
data->characteristic,
0,
sizeof(Angle_Pitch_x100),
(uint8_t*)&Angle_Pitch_x100,
NULL);
break;
case gattdb_LSM6DSL_angle_Z:
sc = sl_bt_gatt_server_send_user_read_response(
data->connection,
data->characteristic,
0,
sizeof(Angle_Yaw_x100),
(uint8_t*)&Angle_Yaw_x100,
NULL);
break;
default:
app_assert(false, "Unexpected characteristic\n");
break;
}
app_assert_status(sc);
}
static void lsm6dsl_char_config_changed_cb(sl_bt_evt_gatt_server_characteristic_status_t * data)
{
sl_status_t sc;
bool enable = sl_bt_gatt_disable != data->client_config_flags;
lsm6dsl_connection = data->connection;
// update notification status
switch (data->characteristic)
{
case gattdb_LSM6DSL_angle_X:
lsm6dsl_angle_x_notification = enable;
// send the first notification
sc = sl_bt_gatt_server_send_notification(
lsm6dsl_connection,
gattdb_LSM6DSL_angle_X,
sizeof(Angle_Roll_x100),
(uint8_t*)&Angle_Roll_x100);
app_assert_status(sc);
break;
case gattdb_LSM6DSL_angle_Y:
lsm6dsl_angle_y_notification = enable;
// send the first notification
sc = sl_bt_gatt_server_send_notification(
lsm6dsl_connection,
gattdb_LSM6DSL_angle_Y,
sizeof(Angle_Pitch_x100),
(uint8_t*)&Angle_Pitch_x100);
app_assert_status(sc);
break;
case gattdb_LSM6DSL_angle_Z:
lsm6dsl_angle_z_notification = enable;
// send the first notification
sc = sl_bt_gatt_server_send_notification(
lsm6dsl_connection,
gattdb_LSM6DSL_angle_Z,
sizeof(Angle_Yaw_x100),
(uint8_t*)&Angle_Yaw_x100);
app_assert_status(sc);
break;
}
// start periodic timer if any of the notifications are enabled
if(lsm6dsl_angle_x_notification || lsm6dsl_angle_y_notification || lsm6dsl_angle_z_notification)
{
//250ms
sc = sl_simple_timer_start(&lsm6dsl_timer,
LSM6DSL_MEASUREMENT_INTERVAL_MS,
lsm6dsl_timer_cb,
NULL,
true);
app_assert_status(sc);
}
else
{
sc = sl_simple_timer_stop(&lsm6dsl_timer);
app_assert_status(sc);
}
}
f打开手机软件EFR Connect,连接上蓝牙设备后,点击Notify小铃铛按钮后,数据会以250ms周期发送到手机app端。