按照接口文檔,整體流程都跑通了,就是獲取UUID列表的那幾個函數(shù)不穩(wěn)定;經(jīng)過調(diào)試,UUID數(shù)組指向的內(nèi)存地址明明有數(shù)據(jù),但是讀到的數(shù)組長度參數(shù)好幾次都是0,需要去多次連續(xù)調(diào)用才有可能有值,非常令人抓狂!
????????///? ????????///?獲取設(shè)備所有服務(wù) ????????///? ????????///?設(shè)備句柄 ????????///?設(shè)備服務(wù)存放數(shù)組 ????????///?服務(wù)個數(shù) ????????///? ????????[DllImport(@".\Lib\WCHBLEDLL.dll")] ????????public?extern?static?byte?WCHBLEGetAllServicesUUID(IntPtr?pDev,?IntPtr?pUUIDArry,?IntPtr?pUUIDArryLen);
截取了關(guān)鍵部分,有按照文檔的順序調(diào)用接口
??//4.?獲取設(shè)備服務(wù)標識ID ????????????ushort?tryGetCount?=?10,?k?=?0,?deviceUUIDsLength?=?0; ????????????while?(k++?<=?tryGetCount) ????????????{ ????????????????//?加點延時 ????????????????//Thread.Sleep(3000); ????????????????Console.WriteLine($"*******************第{k}次獲取服務(wù)UUID*******************"); ????????????????IntPtr?pDeviceServiceUUIDsBuff?=?Marshal.AllocHGlobal(Marshal.SizeOf()?*?100); ????????????????IntPtr?pDeviceServiceUUIDsLengthBuff?=?Marshal.AllocHGlobal(Marshal.SizeOf()); ????????????????var?getAllServicesUUIDResult?=?WCH_Lib.WCHBLEGetAllServicesUUID(deviceConnectHandle,?pDeviceServiceUUIDsBuff,?pDeviceServiceUUIDsLengthBuff); ????????????????Console.WriteLine($"查詢設(shè)備服務(wù)UUID,res?=?{getAllServicesUUIDResult}"); ????????????????unsafe ????????????????{ ????????????????????deviceUUIDsLength?=?*(ushort*)pDeviceServiceUUIDsLengthBuff; ????????????????????Console.WriteLine($"服務(wù)UUID數(shù)量?=?{deviceUUIDsLength}"); ????????????????????for?(ushort?i?=?0;?i?<?deviceUUIDsLength;?i++) ????????????????????{ ????????????????????????ushort?uuid?=?*(ushort*)(pDeviceServiceUUIDsBuff?+?i?*?Marshal.SizeOf()); ????????????????????????Console.WriteLine($"{uuid.ToString("X4")}"); ????????????????????} ????????????????} ????????????????Marshal.FreeHGlobal(pDeviceServiceUUIDsLengthBuff); ????????????????Marshal.FreeHGlobal(pDeviceServiceUUIDsBuff); ????????????????Console.WriteLine("**************************************************************"); ????????????????if(deviceUUIDsLength?>?0) ????????????????{ ????????????????????break; ????????????????} ????????????}
熱門產(chǎn)品 :
CH641: PD及無線充電專用MCU