我们的从service给client传递数据也有两种方式:
第一种:直接通过GATT_Notification()这个函数来通知client
第二种:通过GATTServApp_ProcessCharCfg()这个函数间接的通知client
为什么说GATTServApp_ProcessCharCfg()是一种间接方式呢?
原因是GATTServApp_ProcessCharCfg()再会让主机向slave去发送一个read的请求,然后调用 simpleProfile_ReadAttrCB()函数,然后再执行里面的赋值语句
case
SIMPLEPROFILE_CHAR4_UUID:
*pLen
= 1;pValue[0]
= *pAttr->pValue;break;
那么如果notification已经被使能,GATTServApp_ProcessCharCfg()函数内部还是会调用GATT_Notification()函数,所以我就说GATTServApp_ProcessCharCfg()是一种间接发送notification的方式,现在大家明白了吧?
还不懂?那就看代码:
1、GATTServApp_ProcessCharCfg方式:
case
SIMPLEPROFILE_CHAR4:
if ( len == sizeof (
uint8 ) ) {
simpleProfileChar4 =
*((uint8*)value);
GATTServApp_ProcessCharCfg(
simpleProfileChar4Config, &simpleProfileChar4,
FALSE,
simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
INVALID_TASK_ID );
直接调用这一句就可以了,我们来看一下这里面的参数,
@1:特征表配置参数,
@2:传递的数据,
@3:判断是否经过省份验证,TURE
FALSE
@4:属性表
@5:在属性表中属性的数目
@6:任务的确认通知
接下来,函数的内部会自动调用simpleProfile_ReadAttrCB() 这个函数,
这个函数再根据UUID做相应的操作,比如说赋值:
case SIMPLEPROFILE_CHAR4_UUID:
*pLen = 1;
pValue[0] = *pAttr->pValue;
break;
这样,就完成了发送通知,当然我们省略了填写属性表的步骤(这个前面讲过了就不说了)。
2、GATT_Notification方式
这个简单,直接上代码
static attHandleValueNoti_t
pReport ;//声明attHandleValueNoti_t这个结构体
uint16 noti_cHandle; //存放handle
pReport.handle =
simpleProfileAttrTbl[11].handle;//读取notification对应的handle
GAPRole_GetParameter( 0x30E,
¬i_cHandle);//获取Connection Handle
pReport.len = 1;//数据长度
pReport.value[0] = 0x03;//赋值
GATT_Notification(noti_cHandle,&pReport,FALSE);
这样是不是一目了然?,当然如果我们要用notification的话,建议使用直接调用GATT_Notification函数这种方式。