完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我用CH573跑蓝牙主机程序,现在能找到并连接从机设备,但是无法接收来自从机的透传数据,想请假大家,是不是因为主机程序中没有使能CCCD,所以接收不到notify。如果是的话,主机程序中该如何使能CCCD。
下面图片是串口输出的一些信息。 |
|
相关推荐
1个回答
|
|
蓝牙连上后,一般来说 要先进行服务和特征发现,
拿到对应uuid 对应的handle, 然后根据handle去操作,诸如使能cccd,和读写数据 如下面是一个发现服务,发现char 和获取cccd 对应的handle的api: void central_db_dis_process(void){ uint8_t result = 0; attReadByTypeReq_t req; switch(ble_db_dis_state){ case BLE_DISC_STATE_IDLE: break; case BLE_DISC_STATE_SVC: result = GATT_DiscPrimaryServiceByUUID( centralConnHandle, service_uuid, 16, centralTaskId ); PRINT("GATT_DiscPrimaryServiceByUUID:%xrn",result); break; case BLE_DISC_STATE_CHAR: result = GATT_DiscAllChars(centralConnHandle,centralSvcStartHdl,centralSvcEndHdl,centralTaskId); break; case BLE_DISC_STATE_CCCD: req.startHandle = centralSvcStartHdl; req.endHandle = centralSvcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(GATT_CLIENT_CHAR_CFG_UUID); req.type.uuid[1] = HI_UINT16(GATT_CLIENT_CHAR_CFG_UUID); result = GATT_ReadUsingCharUUID( centralConnHandle, &req, centralTaskId ); break; default: break; } if(result != SUCCESS){ if(centralConnHandle != BLE_STATE_IDLE){ tmos_start_task(centralTaskId,START_SVC_DISCOVERY_EVT,1600); }else{ PRINT("conntection has been disconnectedrn"); } } } 其中处理函数为: /********************************************************************* * @fn centralGATTDiscoveryEvent * * @brief Process GATT discovery event * * @return none */ static void centralGATTDiscoveryEvent( gattMsgEvent_t *pMsg ){ PRINT("centralGATTDiscoveryEventrn"); switch(ble_db_dis_state){ case BLE_DISC_STATE_SVC: // Service found, store handles if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP ){ if( pMsg->msg.findByTypeValueRsp.numInfo > 0 ){ centralSvcStartHdl = ATT_ATTR_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo,0); centralSvcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo,0); // Display Profile Service handle range PRINT("Found Profile Service handle : %x ~ %x n",centralSvcStartHdl,centralSvcEndHdl); } if( ( pMsg->hdr.status == bleProcedureComplete )||( pMsg->method == ATT_ERROR_RSP ) ) { if ( centralSvcStartHdl != 0 ){ central_db_dis_change_state(BLE_DISC_STATE_CHAR); } } } break; case BLE_DISC_STATE_CHAR: // Characteristic found, store handle if ( pMsg->method == ATT_READ_BY_TYPE_RSP ){ if(pMsg->msg.readByTypeRsp.numPairs > 0 ){ for(unsigned char i = 0; i < pMsg->msg.readByTypeRsp.numPairs ; i++){ #if 0 //characteristic properties uint8_t char_properties = pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 2]; #endif uint16_t char_value_handle = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i+3], pMsg->msg.readByTypeRsp.pDataList[pMsg->msg.readByTypeRsp.len * i + 4]); //characteristic uuid length uint8_t char_uuid_length = pMsg->msg.readByGrpTypeRsp.len - 5; //uuid uint8_t *chat_uuid = &(pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 5]); if(sizeof(write_uuid) == char_uuid_length){ if(tmos_memcmp(write_uuid,chat_uuid,char_uuid_length)){ PRINT("write_uuid found,handle:%02xrn",char_value_handle); }else if(tmos_memcmp(notify_uuid,chat_uuid,char_uuid_length)){ PRINT("notify uuid found,handle:%02xrn",char_value_handle); } } } } if((pMsg->hdr.status == bleProcedureComplete ) || ( pMsg->method == ATT_ERROR_RSP ) ) { central_db_dis_change_state(BLE_DISC_STATE_CCCD); PRINT("BLE_DISC_STATE_CHAR donern"); } } break; case BLE_DISC_STATE_CCCD: if ( pMsg->method == ATT_READ_BY_TYPE_RSP) { if(pMsg->msg.readByTypeRsp.numPairs > 0 ) { centralCCCDHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.pDataList[0], pMsg->msg.readByTypeRsp.pDataList[1] ); PRINT("Found client characteristic configuration handle : %x n",centralCCCDHdl); central_enbale_notify(centralConnHandle,centralCCCDHdl); } ble_db_dis_state = BLE_DISC_STATE_IDLE; } break; default: break; } } 然后enable notify 可以是: uint8_t central_gatt_write_char_value(uint16_t connection_handle,uint16_t char_handle,uint8_t *data,uint16_t length){ uint8_t result; attWriteReq_t req; req.cmd = FALSE; req.sig = FALSE; req.handle = char_handle; req.len = length; req.pValue = GATT_bm_alloc(connection_handle,ATT_WRITE_REQ,req.len,NULL,0); if ( req.pValue != NULL ){ tmos_memcpy(req.pValue,data,length); result = GATT_WriteCharValue(centralConnHandle,&req,centralTaskId); if(SUCCESS == result){ return SUCCESS; }else{ GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ); write_data_when_failed.connection_handle = connection_handle; write_data_when_failed.char_handle = char_handle; write_data_when_failed.data_length = length; tmos_memcpy(write_data_when_failed.data,data,write_data_when_failed.data_length); PRINT("data0 data1=%02x %02x rn",write_data_when_failed.data[0],write_data_when_failed.data[1]); tmos_start_task(centralTaskId,START_WRITE_EVT,80); return result; } } return 0xff; } uint8_t central_enbale_notify(uint16_t connection_handle,uint16_t notify_handle){ uint8_t data[2] = {0x01,0x00}; return central_gatt_write_char_value(connection_handle,notify_handle,data,2); } uint8_t central_disable_notify(uint16_t connection_handle,uint16_t notify_handle){ uint8_t data[2] = {0x00,0x00}; return central_gatt_write_char_value(connection_handle,notify_handle,data,2); } |
|
|
|
只有小组成员才能发言,加入小组>>
477 浏览 1 评论
CH579M+RT-Thread,RTC从Sleep模式唤醒失败是什么原因?
2874 浏览 2 评论
2361 浏览 1 评论
820浏览 2评论
CH569通过HSPI实现USB3.0和FPGA高速双向通讯
649浏览 1评论
501浏览 1评论
CH32F103C8T6使用当前官网上的CDC例程会出现设备描述符请求失败
366浏览 1评论
643浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 08:51 , Processed in 0.992572 second(s), Total 75, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号