根据WCH提供是EVT包中的蓝牙从机例程进(Peripheral例程)行修改实现:
步骤一:更改配对模式,将等待对方发起配对改为主动发起配对,
默认是:uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
更改为:uint8_t pairMode = GAPBOND_PAIRING_MODE_INITIATE;
步骤二:在Peripheral_Init()函数中添加 GATT_InitClient();

步骤三:添加一个tmos任务用来枚举主机服务handle
此处传入不同的uuid可以获取不同的数据,如0x2a24和0x2a50

步骤四:添加下方程序
static void centralGATTDiscoveryEvent(gattMsgEvent_t *pMsg) { attReadByTypeReq_t req; if(centralDiscState == BLE_DISC_STATE_CHAR) { // Characteristic found, store handle if(pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->msg.readByTypeRsp.numPairs > 0) { centralCharHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0], pMsg->msg.readByTypeRsp.pDataList[1]); // Display Characteristic 1 handle PRINT("Found Characteristic 1 handle : %x \n", centralCharHdl); } if((pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->hdr.status == bleProcedureComplete) || (pMsg->method == ATT_ERROR_RSP)) { attReadReq_t req; req.handle = centralCharHdl; if(GATT_ReadCharValue(hidEmuConnHandle, &req, hidDevTaskId) == SUCCESS) { PRINT("read success \r\n"); centralProcedureInProgress = TRUE; } centralDiscState = BLE_DISC_STATE_IDLE; } } }
/*********************************************************************
* @fn centralProcessGATTMsg
*
* @brief Process GATT messages
*
* @return none
*/
void centralProcessGATTMsg(gattMsgEvent_t *pMsg)
{
PRINT("pMsg->method :%02x\r\n",pMsg->method );
if(centralState != BLE_STATE_CONNECTED)
{
// In case a GATT message came after a connection has dropped,
// ignore the message
GATT_bm_free(&pMsg->msg, pMsg->method);
return;
}
if((pMsg->method == ATT_EXCHANGE_MTU_RSP) ||
((pMsg->method == ATT_ERROR_RSP) &&
(pMsg->msg.errorRsp.reqOpcode == ATT_EXCHANGE_MTU_REQ)))
{
if(pMsg->method == ATT_ERROR_RSP)
{
uint8_t status = pMsg->msg.errorRsp.errCode;
PRINT("Exchange MTU Error: %x\n", status);
}
centralProcedureInProgress = FALSE;
}
if(pMsg->method == ATT_MTU_UPDATED_EVENT)
{
PRINT("MTU: %x\n", pMsg->msg.mtuEvt.MTU);
}
if((pMsg->method == ATT_READ_RSP) ||
((pMsg->method == ATT_ERROR_RSP) &&
(pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ)))
{
centralProcedureInProgress = FALSE;
if(pMsg->method == ATT_ERROR_RSP)
{
uint8_t status = pMsg->msg.errorRsp.errCode;
PRINT("Read Error: %x\n", status);
}
else
{
// After a successful read, display the read value
centralProcedureInProgress = FALSE;
PRINT("read len:%03d\r\ndata:",pMsg->msg.readRsp.len);
for(uint8_t i=0;i<pMsg->msg.readRsp.len;i++)
PRINT("%c",pMsg->msg.readRsp.pValue[i]);
PRINT("\n");
hex_dump(pMsg->msg.readRsp.pValue, pMsg->msg.readRsp.len);
// if (pMsg->msg.readRsp.pValue[1] == 0xe0 &&
// pMsg->msg.readRsp.pValue[2]== 0x00 &&
// pMsg->msg.readRsp.pValue[3]== 0x05 &&
// pMsg->msg.readRsp.pValue[4] == 0xc4)
// {
// PRINT("OS_CHROME \r\n");
// }
if (pMsg->msg.readRsp.pValue[0] == 0x01 &&
pMsg->msg.readRsp.pValue[1] == 0x06)
{
PRINT("OS_WINDOWS \r\n");
}
else if (pMsg->msg.readRsp.pValue[0] == 0x69 && // i
pMsg->msg.readRsp.pValue[1] == 0x50) // p
{
if (pMsg->msg.readRsp.pValue[2] == 0x68)
{
PRINT("OS_iPhone \r\n");
}
else
{
PRINT("OS_IOS \r\n");
}
}
else if (pMsg->msg.readRsp.pValue[0] == 0x4d && // m
pMsg->msg.readRsp.pValue[1] == 0x61) // a
{
PRINT("OS_MAC \r\n");
}
else{
tmos_start_task(hidEmuTaskId, START_DIS_SERVICE_EVT2, 1600);
}
}
}
else if((pMsg->method == ATT_WRITE_RSP) ||
((pMsg->method == ATT_ERROR_RSP) &&
(pMsg->msg.errorRsp.reqOpcode == ATT_WRITE_REQ)))
{
if(pMsg->method == ATT_ERROR_RSP)
{
uint8_t status = pMsg->msg.errorRsp.errCode;
PRINT("Write Error: %x\n", status);
}
else
{
// Write success
PRINT("Write success \n");
}
centralProcedureInProgress = FALSE;
}
else if(pMsg->method == ATT_HANDLE_VALUE_NOTI)
{
PRINT("Receive noti: %x\n", *pMsg->msg.handleValueNoti.pValue);
}
else if(centralDiscState != BLE_DISC_STATE_IDLE)
{
centralGATTDiscoveryEvent(pMsg);
}
GATT_bm_free(&pMsg->msg, pMsg->method);
}
步骤五:在Peripheral_ProcessTMOSMsg函数中调用centralProcessGATTMsg((gattMsgEvent_t *)pMsg);
static void Peripheral_ProcessTMOSMsg( tmos_event_hdr_t *pMsg ) { //PRINT("pMsg->event =%x\r\n",pMsg->event); gattMsgEvent_t *gattmsg = (gattMsgEvent_t *)pMsg; switch ( pMsg->event ){ case GATT_MSG_EVENT: centralProcessGATTMsg((gattMsgEvent_t *)pMsg); #if 0 //gattMsgEvent_t *gattmsg = (gattMsgEvent_t *)pMsg; switch(gattmsg->method){ case ATT_MTU_UPDATED_EVENT: PRINT("mtu update %d\r\n",gattmsg->msg.mtuEvt.MTU); break; default: PRINT("gattmsg->method=%02x\r\n",gattmsg->method); break; } #endif break; default: break; } }
步骤六:连接之后调用枚举服务tmos任务tmos_start_task(Peripheral_TaskID, SERVICE_DISCOVER_EVT, 1600);
static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
{
gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent;
// See if already connected
if(peripheralConnList.connHandle != GAP_CONNHANDLE_INIT)
{
GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
PRINT("Connection max...\n");
}
else
{
peripheralConnList.connHandle = event->connectionHandle;
peripheralConnList.connInterval = event->connInterval;
peripheralConnList.connSlaveLatency = event->connLatency;
peripheralConnList.connTimeout = event->connTimeout;
peripheralMTU = ATT_MTU_SIZE;
centralState = BLE_STATE_CONNECTED;
// Set timer for periodic event
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
// Set timer for param update event
tmos_start_task(Peripheral_TaskID, SBP_PARAM_UPDATE_EVT, SBP_PARAM_UPDATE_DELAY);
// Start read rssi
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
tmos_start_task(Peripheral_TaskID, SERVICE_DISCOVER_EVT, 1600);
PRINT("Conn %x - Int %x \n", event->connectionHandle, event->connInterval);
}
}
...................
浙公网安备 33010602011771号