//輸出掃描結果 ????????????if(pEvent->discCmpl.numDevs?>?0) ????????????{ ????????????????int?i,?j; ????????????????//?Increment?index?of?current?result?(with?wraparound) ????????????????for(j?=?0;?j?<?pEvent->discCmpl.numDevs;?j++) ????????????????{ ????????????????????PRINT("Device?%d?:?",?j); ????????????????????for(i?=?0;?i?<?6;?i++) ????????????????????{ ????????????????????????PRINT("%x?",?pEvent->discCmpl.pDevList[j].addr[i]); ????????????????????} ????????????????????PRINT("\n"); ????????????????????PRINT("Data?%d?:?",?j); ????????????????????for(i?=?0;?i?<?pEvent->deviceInfo.dataLen;?i++) ????????????????????{ ????????????????????????PRINT("%x?",?pEvent->deviceInfo.pEvtData[i]); ????????????????????} ????????????????????PRINT("\n"); ????????????????} ????????????}
可以參考一下截圖里的代碼
不能理解怎樣的模仿,我下的包里的例程ObserverAddDeviceInfo函數(shù)只有這兩個參數(shù)uint8_t *pAddr, uint8_t addrType。
以下是我的模仿,并不能正確獲取廣播內容,還請給個明確的回復。
typedef?struct { ????uint8_t?eventType;?//!<?Indicates?advertising?event?type?used?by?the?advertiser:?@ref?GAP_ADVERTISEMENT_REPORT_TYPE_DEFINES ????uint8_t?addrType;?????????//!<?Address?Type:?@ref?GAP_ADDR_TYPE_DEFINES ????uint8_t?addr[B_ADDR_LEN];?//!<?Device's?Address ????uint8_t?dataLen;??????????????????//!<?Length?(in?bytes)?of?the?data?field?(evtData) ????uint8_t?*pEvtData;????????????????//!<?Data?field?of?advertisement?or?SCAN_RSP }?gapDevRec_t;
static?void?ObserverAddDeviceInfo(uint8_t?*pAddr,?uint8_t?addrType,uint8_t?*pData) { ????uint8_t?i; ????//?If?result?count?not?at?max ????if(ObserverScanRes?<?DEFAULT_MAX_SCAN_RES) ????{ ????????//?Check?if?device?is?already?in?scan?results ????????for(i?=?0;?i?<?ObserverScanRes;?i++) ????????{ ????????????if(tmos_memcmp(pAddr,?ObserverDevList[i].addr,?B_ADDR_LEN)) ????????????{ ????????????????return; ????????????} ????????} ????????//?Add?addr?to?scan?result?list ????????tmos_memcpy(ObserverDevList[ObserverScanRes].addr,?pAddr,?B_ADDR_LEN); ????????tmos_memcpy(ObserverDevList[ObserverScanRes].pEvtData,?pData,?sizeof(pData)); ????????ObserverDevList[ObserverScanRes].addrType?=?addrType; ????????ObserverDevList[ObserverScanRes].dataLen?=?sizeof(pData); ????????//?Increment?scan?result?count ????????ObserverScanRes++; ????} }
case?GAP_DEVICE_INFO_EVENT: ????????{ ????????????ObserverAddDeviceInfo(pEvent->deviceInfo.addr,?pEvent->deviceInfo.addrType,pEvent->deviceInfo.pEvtData); ????????} ????????break; ????????case?GAP_DEVICE_DISCOVERY_EVENT: ????????{ ????????????PRINT("Discovery?over...\n"); ????????????//輸出掃描結果 ????????????if(pEvent->discCmpl.numDevs?>?0) ????????????{ ????????????????int?i,?j; ????????????????//?Increment?index?of?current?result?(with?wraparound) ????????????????for(j?=?0;?j?<?pEvent->discCmpl.numDevs;?j++) ????????????????{ ????????????????????PRINT("Device?%d?:?",?j); ????????????????????for(i?=?0;?i?<?6;?i++) ????????????????????{ ????????????????????????PRINT("%x?",?pEvent->discCmpl.pDevList[j].addr[i]); ????????????????????} ????????????????????PRINT("\n"); ????????????????????PRINT("Data?len?:?%d",?pEvent->discCmpl.pDevList[j].dataLen); ????????????????????PRINT("Data?%d?:?",?j); ????????????????????for(i?=?0;?i?<?pEvent->discCmpl.pDevList[j].dataLen;?i++) ????????????????????{ ????????????????????????PRINT("%x?",?pEvent->discCmpl.pDevList[j].pEvtData[i]); ????????????????????} ????????????????????PRINT("\n"); ????????????????} ????????????} ????????????GAPRole_ObserverStartDiscovery(DEFAULT_DISCOVERY_MODE, ???????????????????????????????????????????DEFAULT_DISCOVERY_ACTIVE_SCAN, ???????????????????????????????????????????DEFAULT_DISCOVERY_WHITE_LIST); ????????????PRINT("Discovering...\n?"); ????????} ????????break;
我修改的部分在case GAP_DEVICE_INFO_EVENT:這個case語句中,在發(fā)現(xiàn)過程中對你需要的數(shù)據進行枚舉。
可以嘗試根據我提供的截圖進行修改。您修改的部分是在?case GAP_DEVICE_DISCOVERY_EVENT:這時已將是掃描結束了,羅列出掃描到的數(shù)據。
重復說一下? ? ??我下的包里的例程ObserverAddDeviceInfo函數(shù)只有這兩個參數(shù)uint8_t *pAddr, uint8_t addrType。?
我沒法參考你截圖里 使用 ObserverAddDeviceInfo函數(shù) ,?ObserverAddDeviceInfo函數(shù)復制下了廣播數(shù)據中的地址,你截圖里有傳入廣播包內容的數(shù)據,我沒辦法模仿。
不清楚各種數(shù)據結構體的用途,唯一知道是廣播的數(shù)據在case GAP_DEVICE_INFO_EVENT:進行復制,沒有解釋掃描結束輸出的結構體數(shù)據,頭文件里的是有結構體數(shù)據的定義但都是用來干嘛的,每次掃描都產生了什么數(shù)據,用戶層能用到什么數(shù)據?
目前我只能設置掃描白名單來獲取指定地址設置的廣播數(shù)據,當掃描到多個設備的廣播數(shù)據這怎么區(qū)分?
首先ObserverAddDeviceInfo里面的參數(shù)可以自行修改如static void ObserverAddDeviceInfo(uint8_t *pAddr, uint8_t addrType,uint8_t *pEvtData);按照截圖修改case GAP_DEVICE_INFO_EVENT:的程序;后面相應的語句再進行修改就可以了。結構體的對應的注釋都可以在ch58xble_lib.h里面看到。掃描到多個設備通過MAC地址來區(qū)分。
上面改的沒看?不是我不想修改。不要再繼續(xù)扯皮了,沒對掃描數(shù)據說明我怎么保存下我想要的數(shù)據,請給出掃描數(shù)據的說明。
你留個郵箱我把修改后的的工程發(fā)給你。掃描到的數(shù)據是廣播包,可以看這篇博客的講解BLE 藍牙廣播包結構分享 - debugdabiaoge - 博客園 (cnblogs.com)
這篇我看過了,這跟我需要知道TMOS廣播掃描到的數(shù)據怎么使用沒有半毛錢關系,你一直理解不了我想問什么可以換個人來解答,正在考慮要不要換其他家藍牙單片機做廣播掃描了,太難用了。郵箱:個人信息保護,已隱藏
獲取廣播包和掃描應答包在結構體中已包含了:
uint8_t *pEvtData;? ? ? ? ? ? ? ? //!< Data field of advertisement or SCAN_RSP
直接根據長度打印出來即可:
解決了,一眼難盡。從ObserverEventCB回調函數(shù)的switch分支GAP_DEVICE_INFO_EVENT里存下每次發(fā)現(xiàn)的設備信息,在掃描結束的時候打印出來,程序如下。
typedef?struct { ????uint8_t?eventType;?//!<?Indicates?advertising?event?type?used?by?the?advertiser:?@ref?GAP_ADVERTISEMENT_REPORT_TYPE_DEFINES ????uint8_t?addrType;?????????//!<?Address?Type:?@ref?GAP_ADDR_TYPE_DEFINES ????uint8_t?addr[B_ADDR_LEN];?//廣播的設備地址 ????int8_t?rssi;??????????????//RSSI值 ????uint8_t?dataLen;??????????//廣播的數(shù)據長度 ????uint8_t?advertisingData[31];//廣播的數(shù)據 }?scanDevRec_t; static?scanDevRec_t?ObserverDevList[DEFAULT_MAX_SCAN_RES]; static?uint8_t?cnt;//掃描發(fā)現(xiàn)設備??計數(shù) static?void?ObserverEventCB(gapRoleEvent_t?*pEvent) { ????switch(pEvent->gap.opcode) ????{ ????????case?GAP_DEVICE_INIT_DONE_EVENT: ????????{ ????????????GAPRole_ObserverStartDiscovery(DEFAULT_DISCOVERY_MODE, ???????????????????????????????????????????DEFAULT_DISCOVERY_ACTIVE_SCAN, ???????????????????????????????????????????DEFAULT_DISCOVERY_WHITE_LIST); ????????????PRINT("Discovering...\n"); ????????} ????????break; ????????case?GAP_DEVICE_INFO_EVENT: ????????{ ????????????//?從發(fā)現(xiàn)的設備里保存下設備地址?廣播的數(shù)據內容 ????????????ObserverDevList[cnt].addrType?=?pEvent->deviceInfo.addrType; ????????????ObserverDevList[cnt].rssi?=?pEvent->deviceInfo.rssi; ????????????ObserverDevList[cnt].dataLen?=?pEvent->deviceInfo.dataLen; ????????????tmos_memcpy(ObserverDevList[cnt].addr,?pEvent->deviceInfo.addr,?B_ADDR_LEN); ????????????tmos_memcpy(ObserverDevList[cnt].advertisingData,?pEvent->deviceInfo.pEvtData,?pEvent->deviceInfo.dataLen); ????????????cnt++; ????????} ????????break; ????????case?GAP_DEVICE_DISCOVERY_EVENT: ????????{ ????????????PRINT("Discovery?over...\n"); ????????????//打印掃描結果 ????????????if?(cnt?>?0) ????????????{ ????????????????unsigned?char?i,?j; ????????????????for?(i?=?0;?i?<?cnt;?i++) ????????????????{ ????????????????????PRINT("Device?%d?addr:",?i); ????????????????????for(j?=?0;?j?<?6;?j++) ????????????????????{ ????????????????????????PRINT("%x?",?ObserverDevList[i].addr[j]); ????????????????????} ????????????????????PRINT("\n"); ????????????????????PRINT("Device?%d?advertising?data:",?i); ????????????????????for(j?=?0;?j?<?ObserverDevList[i].dataLen;?j++) ????????????????????{ ????????????????????????PRINT("%x?",?ObserverDevList[i].advertisingData[j]); ????????????????????} ????????????????????PRINT("\n"); ????????????????} ????????????} ????????????else?{ ????????????????PRINT("No?device?found.\n"); ????????????} ????????????cnt?=?0; ????????????PRINT("---------------------------------------------------------------------------\n"); ????????????GAPRole_ObserverStartDiscovery(DEFAULT_DISCOVERY_MODE, ???????????????????????????????????????????DEFAULT_DISCOVERY_ACTIVE_SCAN, ???????????????????????????????????????????DEFAULT_DISCOVERY_WHITE_LIST); ????????????PRINT("Discovering...\n?"); ????????} ????????break; ????????default: ????????????break; ????} }
廣播掃描數(shù)據的結構體gapRoleEvent_t里的gapEventHdr_t gap;里存了在掃描過程中發(fā)現(xiàn)設備的事件,對gapEventHdr_t? ? opcode值判斷來執(zhí)行想要的動作。
樓主你貼的第二次代碼思路已經對了,可是打印的時候選錯數(shù)組了~