修改simple_peripheral工程的simple_gatt_profile.c文件:
1、工程默认char4为notify,长度为1个字节,我们参考char5把它修改为20个字节。
2、为了使主机能自动获取char4的handle,从而打开notify开关,我们为char4的属性添加一个read 权限
修改后的代码如下,其中 SIMPLEPROFILE_CHAR4_LEN = 20;
// Simple Profile Characteristic 4 Properties
static uint8 simpleProfileChar4Props = GATT_PROP_NOTIFY | GATT_PROP_READ;
// Characteristic 4 Value
//static uint8 simpleProfileChar4 = 0;
static uint8 simpleProfileChar4[SIMPLEPROFILE_CHAR4_LEN] = { 0, 0, 0, 0, 0 };
// Simple Profile Characteristic 4 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 *simpleProfileChar4Config;
// Simple Profile Characteristic 4 User Description
static uint8 simpleProfileChar4UserDesp[17] = "Characteristic 4";
3、修改 数组 simpleProfileAttrTbl[]:
static gattAttribute_t simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED] =
{
//。。。
// Characteristic 3
// Characteristic 4 Declaration
{
{ ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&simpleProfileChar4Props
},
// Characteristic Value 4
{
{ ATT_BT_UUID_SIZE, simpleProfilechar4UUID },
GATT_PERMIT_READ,//0
0,
simpleProfileChar4
},
// Characteristic 4 configuration
{
{ ATT_BT_UUID_SIZE, clientCharCfgUUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8 *)&simpleProfileChar4Config
},
// Characteristic 4 User Description
{
{ ATT_BT_UUID_SIZE, charUserDescUUID },
GATT_PERMIT_READ,
0,
simpleProfileChar4UserDesp
},
// Characteristic 5 Declaration
。。。
};
4、修改函数SimpleProfile_SetParameter( )
bStatus_t SimpleProfile_SetParameter( uint8 param, uint8 len, void *value )
{
//。。。
case SIMPLEPROFILE_CHAR4:
if ( 1 )//len <= 20)
{
VOID memcpy( simpleProfileChar4, value, len );
// See if Notification has been enabled
GATTServApp_ProcessCharCfg( simpleProfileChar4Config, simpleProfileChar4, FALSE,
simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
INVALID_TASK_ID, simpleProfile_ReadAttrCB );
}
else
{
ret = bleInvalidRange;
}
break;
//。。。
}
5、修改函数SimpleProfile_GetParameter( )
bStatus_t SimpleProfile_GetParameter( uint8 param, void *value )
{
//。。。
case SIMPLEPROFILE_CHAR4:
VOID memcpy( value, simpleProfileChar4, SIMPLEPROFILE_CHAR4_LEN );
break;
//。。。
}
6、修改函数simpleProfile_ReadAttrCB( )
static bStatus_t simpleProfile_ReadAttrCB(uint16_t connHandle,
gattAttribute_t *pAttr,
uint8_t *pValue, uint16_t *pLen,
uint16_t offset, uint16_t maxLen,
uint8_t method)
{
//。。。
case SIMPLEPROFILE_CHAR1_UUID:
case SIMPLEPROFILE_CHAR2_UUID:
case SIMPLEPROFILE_CHAR4_UUID:
*pLen = SIMPLEPROFILE_CHAR4_LEN;
VOID memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR4_LEN );
break;
//。。。
}
7、最后添加最重要的函数 NotifyByChar4(),其中通过simpleProfileChar4Config获取连ConnHandle,通过simpleProfileAttrTbl[]获取notify句柄。
void NotifyByChar4(uint8 *pBuffer,uint16 length)
{
attHandleValueNoti_t noti;
bStatus_t status;
gattCharCfg_t *pItem = simpleProfileChar4Config;
if ( pItem->connHandle != INVALID_CONNHANDLE )
{
noti.pValue = (uint8 *)GATT_bm_alloc( pItem->connHandle, ATT_HANDLE_VALUE_NOTI,length,&length);
if (noti.pValue != NULL)
{
//simpleProfileAttrTbl[11]为Char4 Value对应的数组
noti.handle = simpleProfileAttrTbl[11].handle;
noti.len = length;
memcpy(noti.pValue, pBuffer, length);
status = GATT_Notification( pItem->connHandle, ¬i, FALSE );
if ( status != SUCCESS )
{
GATT_bm_free( (gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI );
}
}
else
{
GATT_bm_free( (gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI );
status = bleNoResources;
}
}
}
版权声明:本文为mengbinaaa原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。