沁恒蓝牙开发纪要(CH585)3、新增Notify特征-向手机上报数据
本文介绍的是在例程的基础上新增Notify特征,并且添加实验直接转发手机下发的数据,测试添加成功与否。
新增Notify特征-向手机上报数据
本文介绍的是在例程的基础上新增Notify特征,并且添加实验直接转发手机下发的数据,测试添加成功与否。
1、添加Notify特征
本文是在Characteristic 5上新增notify特征,流程如下:
1.1、修改特征 与 添加Notify配置
// Simple Profile Characteristic 5 Properties
static uint8_t simpleProfileChar5Props = GATT_PROP_READ|GATT_PROP_NOTIFY;
// Simple Profile Characteristic 5 Configuration Each client has its own
// instantiation of the Client Characteristic Configuration. Reads of the
// Client Characteristic Configuration only shows the configuration for
// that client and writes only affect the configuration of that client.
static gattCharCfg_t simpleProfileChar5Config[PERIPHERAL_MAX_CONNECTION];
1.2、修改属性表
添加notify的配置,添加后手机APP才可以使能Notify
// Characteristic 5 configuration
{
{ATT_BT_UUID_SIZE, clientCharCfgUUID},
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8_t *)simpleProfileChar5Config},
1.3、注册Notify特征
GATTServApp_InitCharCfg(INVALID_CONNHANDLE, simpleProfileChar5Config);
1.4、添加连接状态回调
GATTServApp_InitCharCfg(connHandle, simpleProfileChar5Config);
1.5、烧录效果
下图可见已经成功添加了,notify熟悉了。
2、添加数据发送接口
2.1、底层数据发送接口
如下图,需要修改的是红框中的两块代码,修改为新增的notify的配置。
#define SIMPLEPROFILE_CHAR5_VALUE_POS 15
bStatus_t simpleProfile5_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti)
{
uint16_t value = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar5Config);
// If notifications enabled
if(value & GATT_CLIENT_CFG_NOTIFY)
{
// Set the handle
pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR5_VALUE_POS].handle;
// Send the notification
return GATT_Notification(connHandle, pNoti, FALSE);
}
return bleIncorrectMode;
}
2.2、添加notify的数据发送接口
在原数据发送接口上只修改了函数名 和 调用的底层数据发送接口
static void peripheralChar5Notify(uint8_t *pValue, uint16_t len)
{
attHandleValueNoti_t noti;
if(len > (peripheralMTU - 3))
{
PRINT("Too large noti\n");
return;
}
noti.len = len;
noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle,
ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
if(noti.pValue)
{
tmos_memcpy(noti.pValue, pValue, noti.len);
if(simpleProfile5_Notify(peripheralConnList.connHandle, ¬i) != SUCCESS)
{
GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
}
}
}
2.3、数据转发测试
在char1接收数据的回调中,直接将接收到的书转发回手机
实验结果:
注意:手机要接收蓝牙上报的数据需要先手动使能对应char x 的 Notify 使能。
Notify 使能如下:
以上已经完成了数据发送的添加和验证了。
3、handle确认
static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
{
attHandleValueNoti_t noti;
if(len > (peripheralMTU - 3))
{
PRINT("Too large noti\n");
return;
}
noti.len = len;
noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle,
ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
if(noti.pValue)
{
tmos_memcpy(noti.pValue, pValue, noti.len);
if(simpleProfile_Notify(peripheralConnList.connHandle, ¬i) != SUCCESS)
{
GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
}
}
}
阅读上面代码可以看见最终于发送数据接口是 simpleProfile_Notify ,所以再次跳转
bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti)
{
uint16_t value = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar4Config);
// If notifications enabled
if(value & GATT_CLIENT_CFG_NOTIFY)
{
// Set the handle
pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR4_VALUE_POS].handle;
// Send the notification
return GATT_Notification(connHandle, pNoti, FALSE);
}
return bleIncorrectMode;
}
此处就可以看到正在使用的handle了,再次跳转数组和宏。
// Position of simpleProfilechar4 value in attribute array
#define SIMPLEPROFILE_CHAR4_VALUE_POS 11
#define SIMPLEPROFILE_CHAR5_VALUE_POS 15
simpleProfileAttrTbl 是服务的属性表,SIMPLEPROFILE_CHAR4_VALUE_POS 宏上的注释提示是“simpleProfilechar4值在属性数组中的位置”,所以11代表着,属性表从上到下数到第12个(0开始),就是Notify的handle。当改动属性标时也需要同步修改,否者主机收不到上报的数据。

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)