继续捣鼓蓝牙,今天实现一个WB09开启一个LED控制的服务,里面有两个特征,分别控制LED的点亮与熄灭。
对上一篇的帖子里面的工程继续编辑,首先配置一下LED灯控制IO,默认设为高电平,也就是LED熄灭

添加一个服务

配置LED控制服务,里面添加两个特征值

配置LED打开特征值,数据长度设置1,只打开写

配置LED关闭特征值,数据长度设置1,只打开写

生成工程,打开led_control_app.c文件,添加回调处理
/* Functions Definition ------------------------------------------------------*/
void LED_CONTROL_Notification(LED_CONTROL_NotificationEvt_t *p_Notification)
{
/* USER CODE BEGIN Service1_Notification_1 */
/* USER CODE END Service1_Notification_1 */
switch(p_Notification->EvtOpcode)
{
/* USER CODE BEGIN Service1_Notification_Service1_EvtOpcode */
/* USER CODE END Service1_Notification_Service1_EvtOpcode */
case LED_CONTROL_LED_ON_WRITE_EVT:
/* USER CODE BEGIN Service1Char1_WRITE_EVT */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/* USER CODE END Service1Char1_WRITE_EVT */
break;
case LED_CONTROL_LED_OFF_WRITE_EVT:
/* USER CODE BEGIN Service1Char2_WRITE_EVT */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
/* USER CODE END Service1Char2_WRITE_EVT */
break;
default:
/* USER CODE BEGIN Service1_Notification_default */
/* USER CODE END Service1_Notification_default */
break;
}
/* USER CODE BEGIN Service1_Notification_2 */
/* USER CODE END Service1_Notification_2 */
return;
}
编译并下载,使用手机扫描,点击CONNECT连接

可以看到有一个自定义服务,该服务下有两个特征

尝试写入

当对第一个特征写入任意值后,小灯点亮

当对第一个特征写入任意值后,小灯熄灭

|