继续捣鼓蓝牙功能,今天我们就实现一个标准的电量服务,并实现Notify功能
继续在上一篇的工程修改,由于Notify功能需要定时发送,我们先来配置一下定时器2作为Notify定时发送的中断源
打开对应中断
添加一个服务,里面一个特征,这个服务参考标准电量服务进行配置
生成代码,打开app_conf.h添加一个新的任务ID
typedef enum
{
CFG_TASK_BLE_STACK,
CFG_TASK_VTIMER,
CFG_TASK_NVM,
/* USER CODE BEGIN CFG_Task_Id_t */
CFG_TASK_BATTERY_NOTIF,
/* USER CODE END CFG_Task_Id_t */
CFG_TASK_NBR, /**< Shall be LAST in the list */
} CFG_Task_Id_t;
打开battery_app.c进行修改,注册CFG_TASK_BATTERY_NOTIF任务
void BATTERY_APP_Init(void)
{
BATTERY_APP_Context.ConnectionHandle = 0xFFFF;
BATTERY_Init();
/* USER CODE BEGIN Service2_APP_Init */
UTIL_SEQ_RegTask( 1U << CFG_TASK_BATTERY_NOTIF, UTIL_SEQ_RFU, BATTERY_Batterylevel_SendNotification);
BATTERY_APP_Context.Batterylevel_Notification_Status = Batterylevel_NOTIFICATION_OFF;
/* USER CODE END Service2_APP_Init */
return;
}
填充回调函数
void BATTERY_Notification(BATTERY_NotificationEvt_t *p_Notification)
{
/* USER CODE BEGIN Service2_Notification_1 */
/* USER CODE END Service2_Notification_1 */
switch(p_Notification->EvtOpcode)
{
/* USER CODE BEGIN Service2_Notification_Service2_EvtOpcode */
/* USER CODE END Service2_Notification_Service2_EvtOpcode */
case BATTERY_BATTERYLEVEL_READ_EVT:
/* USER CODE BEGIN Service2Char1_READ_EVT */
/* USER CODE END Service2Char1_READ_EVT */
break;
case BATTERY_BATTERYLEVEL_NOTIFY_ENABLED_EVT:
/* USER CODE BEGIN Service2Char1_NOTIFY_ENABLED_EVT */
BATTERY_APP_Context.Batterylevel_Notification_Status = Batterylevel_NOTIFICATION_ON;
/* USER CODE END Service2Char1_NOTIFY_ENABLED_EVT */
break;
case BATTERY_BATTERYLEVEL_NOTIFY_DISABLED_EVT:
/* USER CODE BEGIN Service2Char1_NOTIFY_DISABLED_EVT */
BATTERY_APP_Context.Batterylevel_Notification_Status = Batterylevel_NOTIFICATION_OFF;
/* USER CODE END Service2Char1_NOTIFY_DISABLED_EVT */
break;
default:
/* USER CODE BEGIN Service2_Notification_default */
/* USER CODE END Service2_Notification_default */
break;
}
/* USER CODE BEGIN Service2_Notification_2 */
/* USER CODE END Service2_Notification_2 */
return;
}
实现Notify发送
__USED void BATTERY_Batterylevel_SendNotification(void) /* Property Notification */
{
BATTERY_APP_SendInformation_t notification_on_off = Batterylevel_NOTIFICATION_OFF;
BATTERY_Data_t battery_notification_data;
battery_notification_data.p_Payload = (uint8_t*)a_BATTERY_UpdateCharData;
battery_notification_data.Length = 0;
/* USER CODE BEGIN Service2Char1_NS_1*/
a_BATTERY_UpdateCharData[0] = 50;
battery_notification_data.Length = 1;
notification_on_off = BATTERY_APP_Context.Batterylevel_Notification_Status;
/* USER CODE END Service2Char1_NS_1*/
if (notification_on_off != Batterylevel_NOTIFICATION_OFF && BATTERY_APP_Context.ConnectionHandle != 0xFFFF)
{
BATTERY_NotifyValue(BATTERY_BATTERYLEVEL, &battery_notification_data, BATTERY_APP_Context.ConnectionHandle);
}
/* USER CODE BEGIN Service2Char1_NS_Last*/
/* USER CODE END Service2Char1_NS_Last*/
return;
}
修改中断回调函数,加入请求执行CFG_TASK_BATTERY_NOTIF任务
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
UTIL_SEQ_SetTask(1U << CFG_TASK_BATTERY_NOTIF, CFG_SEQ_PRIO_0);
/* USER CODE END TIM2_IRQn 1 */
}
编译下载,连接蓝牙后即可看到标准的电量服务
点击接收通知,即可看到蓝牙设备发来的电量信息
具体通信数据