一、情况
最近使用CC2541做主机来连接CH573做测试,发现获取不到CH573的notify属性的特征值,后面发现,通过 GATT_ReadUsingCharUUID() 是搜索不到的,而通过 GATT_DiscCharsByUUID() 是可以搜索到的。
二、需要注意的点
-
GATT_ReadUsingCharUUID() 和 GATT_DiscCharsByUUID()的区别:
由于一个characteristic定义是由:characteristic declaration,characteristic value,characteristic configuration组成;GATT_ReadUsingCharUUID()读取到的是 characteristic value的hdl,而 GATT_DiscCharsByUUID() 读取到的 是 characteristic declaration 的hdl。 -
对于 characteristic configuration ,只能通过 GATT_ReadUsingCharUUID() 来操作,所以我们这里可以这样做: 对于特征值的 value的hdl ,我们可以通过 disc找到hdl +1 处理,对于需要ccc打开notify选项,则通过read来直接读取即可。
三、代码
- 主机
/*********************************************************************
* @fn simpleBLEGATTDiscoveryEvent
*
* @brief Process GATT discovery event
*
* @return none
*/
static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg )
{
attReadByTypeReq_t req;
BLE_DEV *p = simpleBLECentralGetDev(pMsg->connHandle);
if ( p->simpleBLEDiscState == BLE_DISC_STATE_SVC )
{
// Service found, store handles
if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->msg.findByTypeValueRsp.numInfo > 0 )
{
p->simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle;
p->simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle;
}
// If procedure complete
if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->hdr.status == bleProcedureComplete ) ||
( pMsg->method == ATT_ERROR_RSP ) )
{
if ( p->simpleBLESvcStartHdl != 0 )
{
// Discover characteristic
p->simpleBLEDiscState = BLE_DISC_STATE_CHAR1;
req.startHandle = p->simpleBLESvcStartHdl;
req.endHandle = p->simpleBLESvcEndHdl;
req.type.len = ATT_BT_UUID_SIZE;
req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);
req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);
GATT_DiscCharsByUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId );
}
}
}
else if ( p->simpleBLEDiscState == BLE_DISC_STATE_CHAR1 ) //先找CHAR1
{
NPI_PrintValue("1 pMsg->method", pMsg->method, 10);
NPI_PrintValue("1 numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
// Characteristic found, store handle
if ( pMsg->method == ATT_READ_BY_TYPE_RSP &&
pMsg->msg.readByTypeRsp.numPairs > 0 )
{
p->simpleBLECharHdl[0] = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],
pMsg->msg.readByTypeRsp.dataList[1] );
NPI_PrintValue("1 hdl", p->simpleBLECharHdl[0], 10);
LCD_WRITE_STRING( "CHAR 1 Found", HAL_LCD_LINE_3 );
p->simpleBLEProcedureInProgress = TRUE;
}
else // pMsg->msg.readByTypeRsp.numPairs is 0.
{
p->simpleBLEDiscState = BLE_DISC_STATE_CHAR6;
req.startHandle = p->simpleBLESvcStartHdl;
req.endHandle = p->simpleBLESvcEndHdl;
req.type.len = ATT_BT_UUID_SIZE;
req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR6_UUID);
req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR6_UUID);
GATT_DiscCharsByUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId );
}
}
else if (p->simpleBLEDiscState == BLE_DISC_STATE_CHAR6) //再找CHAR6
{ // Characteristic found, store handle
NPI_PrintValue("6 pMsg->method", pMsg->method, 10);
NPI_PrintValue("6 numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
if ( (pMsg->method == ATT_READ_BY_TYPE_RSP) &&
pMsg->msg.readByTypeRsp.numPairs > 0 )
{
p->simpleBLECharHdl[5] = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],
pMsg->msg.readByTypeRsp.dataList[1] );
LCD_WRITE_STRING( "CHAR 6 Found", HAL_LCD_LINE_4 );
// p->simpleBLEProcedureInProgress = FALSE;
p->simpleBLEProcedureInProgress = TRUE;
NPI_PrintValue("6 hdl", p->simpleBLECharHdl[5], 10);
}
else
{
p->simpleBLEDiscState = BLE_DISC_STATE_CCC;
req.startHandle = p->simpleBLESvcStartHdl;
req.endHandle = p->simpleBLESvcEndHdl;
req.type.len = ATT_BT_UUID_SIZE;
req.type.uuid[0] = LO_UINT16(0x2902);
req.type.uuid[1] = HI_UINT16(0x2902);
GATT_ReadUsingCharUUID( p->simpleBLEConnHandle, &req, simpleBLETaskId );
}
//p->simpleBLEDiscState = BLE_DISC_STATE_IDLE;
}
else if (p->simpleBLEDiscState == BLE_DISC_STATE_CCC) //再找CCC
{
NPI_PrintValue("ccc pMsg->method", pMsg->method, 10);
NPI_PrintValue("ccc numPairs", pMsg->msg.readByTypeRsp.numPairs, 10);
if ( (pMsg->method == ATT_READ_BY_TYPE_RSP) &&
pMsg->msg.readByTypeRsp.numPairs > 0 )
{
ccc_dhl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0],
pMsg->msg.readByTypeRsp.dataList[1] );
NPI_PrintValue("ccc hdl", ccc_dhl, 10);
}
else //CHAR6完全找到后 写CCC了直接
{
p->simpleBLEDiscState = BLE_DISC_STATE_IDLE;
p->simpleBLEProcedureInProgress = FALSE;
// Do a write
attWriteReq_t req;
req.handle = ccc_dhl;
req.len = 2;
req.sig = 0;
req.cmd = 0;
req.value[0] = 1; //写 1 打开notify选项
req.value[1] = 0;
uint8 status;
status = GATT_WriteCharValue( p->simpleBLEConnHandle, &req, simpleBLETaskId ); // 发送数据
if(SUCCESS != status)
{
NPI_PrintValue("Write Error :", status, 10);
}
else
{
NPI_PrintValue("ccc Write success :", status, 10);
}
}
}
}
- LOG
[KEY UP pressed!]
Discovering...
>>>GAP_DEVICE_INFO_EVENT
>>>GAP_DEVICE_INFO_EVENT
>>>GAP_DEVICE_INFO_EVENT
Key DOWN = Read RSSI
>>>GAP_DEVICE_DISCOVERY_EVENT
Devices Found 1[0]=0x84C2E43F4F47
Key CENTER = Connect
Connecting......
Idx= 0
Connecting
0x84C2E43F4F47
Idx= 0
>>>GAP_LINK_ESTABLISHED_EVENT
0x84C2E43F4F47
Key DOWN = Read RSSI
1 pMsg->method 9
1 numPairs 1
1 hdl 34
CHAR 1 Found
1 pMsg->method 96 pMsg->method 9
6 numPairs 1
CHAR 6 Found
6 hdl 43
6 pMsg->method 9
6 numPairs 0
ccc pMsg->method 9
ccc numPairs 1
ccc hdl 45ccc pMsg->method 9
ccc numPairs 0
ccc Write success : 0
Write sent: 0
ATT_HANDLE_VALUE_NOTI
ATT_HANDLE_VALUE_NOTI