在使用CH57x/CH58x从机时,一般都是由主机去使能通知,使能通知后,主机才可以收到从机的notify。

1.那么从机想要自己使能notify该怎么做呢?

可以添加这样一个函数:

uint8 enable_notify(uint16 connection_handle,uint8 enable) {
uint16 cccd = 0;
if(enable) {
cccd |= GATT_CLIENT_CFG_NOTIFY;
}else {
cccd &= ~GATT_CLIENT_CFG_NOTIFY;
}
return GATTServApp_WriteCharCfg( connection_handle, simpleProfileChar4Config, cccd );
}

在连接后此函数(Peripheral_LinkEstablished)调用就可以了。 

enable_notify( peripheralConnList.connHandle,ENABLE);

2.在蓝牙主机的代码中是如何打开从机的notify的呢?

在Central例程中,连接成功后会去找服务的句柄,找到notify的句柄后进行一个写操作就可以了。

else if(centralDiscState == BLE_DISC_STATE_CCCD)
{
if(pMsg->method == ATT_READ_BY_TYPE_RSP &&
pMsg->msg.readByTypeRsp.numPairs > 0)
{
centralCCCDHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0],
pMsg->msg.readByTypeRsp.pDataList[1]);
centralProcedureInProgress = FALSE;

// Start do write CCCD
tmos_start_task(centralTaskId, START_WRITE_CCCD_EVT, DEFAULT_WRITE_CCCD_DELAY);           //通过这个写任务打开notify

// Display Characteristic 1 handle
PRINT("Found client characteristic configuration handle : %x \n", centralCCCDHdl);
}
centralDiscState = BLE_DISC_STATE_IDLE;
}

3.主机代码获取notify数据位置:

else if(pMsg->method == ATT_HANDLE_VALUE_NOTI)
{
PRINT("Receive noti: %x\n", *pMsg->msg.handleValueNoti.pValue);
}

posted on 2022-09-15 14:52  WCH蓝牙应用分享  阅读(1177)  评论(0编辑  收藏  举报