WCH的蓝牙从机被苹果手机的蓝牙工具(如Lightblue)连接后,如何去获取苹果手机的时间呢?

疑问:为什么是水果手机,安卓机不可以吗?答:是的,安卓手机的没有用来存放时间的蓝牙服务,所以获取不到。

接下来我来根据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

步骤四:添加下方程序,

static void centralGATTDiscoveryEvent(gattMsgEvent_t *pMsg)
{
    attReadByTypeReq_t req;

    if(centralDiscState == BLE_DISC_STATE_SVC)
    {
        // Service found, store handles
        if(pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
           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("service handle range:%04x-%04x\r\n", centralSvcStartHdl, centralSvcEndHdl);
        }
        // If procedure complete
        if((pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
            pMsg->hdr.status == bleProcedureComplete) ||
           (pMsg->method == ATT_ERROR_RSP))
        {
            if(centralSvcStartHdl != 0)
            {

                // Discover characteristic
                centralDiscState = BLE_DISC_STATE_CHAR; 
                uint8_t result = GATT_DiscAllChars(peripheralConnList.connHandle,centralSvcStartHdl,centralSvcEndHdl,Peripheral_TaskID);
                                            PRINT("GATT_DiscAllChars:%02x\r\n",result);
            }
        }
    }

    else if(centralDiscState == BLE_DISC_STATE_CHAR)
    {
        PRINT("pMsg->method1=%02x  pMsg->msg.readByTypeRsp.numPairs=%02x",pMsg->method,pMsg->msg.readByTypeRsp.numPairs);
        // Characteristic found, store handle
        if(pMsg->method == ATT_READ_BY_TYPE_RSP &&
           pMsg->msg.readByTypeRsp.numPairs > 0)
        {
            for(unsigned char i = 0; i < pMsg->msg.readByTypeRsp.numPairs ; i++) {
#if 1
                //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 *char_uuid = &(pMsg->msg.readByGrpTypeRsp.pDataList[pMsg->msg.readByGrpTypeRsp.len * i + 5]);

                PRINT("char_properties  :%02x,%s\r\n",char_properties,(char_properties&(GATT_PROP_WRITE|GATT_PROP_WRITE_NO_RSP))?"wite handle":"");
                PRINT("char_value_handle:%04x\r\n",char_value_handle);
                PRINT("char_uuid_length :%02d\r\n",char_uuid_length);
                PRINT("char_uuid        :");
                hex_dump(char_uuid,char_uuid_length);



                if(char_properties==(GATT_PROP_READ|GATT_PROP_NOTIFY)) {
                    centralCharHdl = char_value_handle;
                   }
            }
        }
        if((pMsg->method == ATT_READ_BY_TYPE_RSP &&
            pMsg->hdr.status == bleProcedureComplete) ||
           (pMsg->method == ATT_ERROR_RSP))
        {
     attReadReq_t req;
                     req.handle = centralCharHdl;
                     PRINT("req.handle=%x\r\n",req.handle);
                     if(GATT_ReadCharValue(peripheralConnList.connHandle, &req, Peripheral_TaskID) == SUCCESS) {
                         PRINT("read success ***********************************\r\n");
//                           centralProcedureInProgress = TRUE;
                     }
            centralDiscState = BLE_DISC_STATE_IDLE;

        }
    }
}

static void centralProcessGATTMsg(gattMsgEvent_t *pMsg)
{
    PRINT("pMsg->method :%02x\r\n",pMsg->method );
    PRINT("centralState :%02x\r\n",centralState );
    PRINT("centralDiscState :%02x\r\n",centralDiscState );
    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)))
    {
        if(pMsg->method == ATT_ERROR_RSP)
        {
            uint8_t status = pMsg->msg.errorRsp.errCode;

            PRINT("Read Error: %x\n", status);
        }
        else
        {
            PRINT("read len:%03d\r\ndata:",pMsg->msg.readRsp.len);
            hex_dump(pMsg->msg.readRsp.pValue, pMsg->msg.readRsp.len);
            // After a successful read, display the read value
//            PRINT("Read rsp: %x\n", *pMsg->msg.readRsp.pValue);
        }
        centralProcedureInProgress = FALSE;
    }
    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);
}
View Code

步骤五:在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);
}
}


...................

posted on 2023-10-23 10:53  WCH蓝牙应用分享  阅读(190)  评论(0编辑  收藏  举报