C# 調(diào)用WCHBLEDLL庫,WCH_Lib.WCHBLEGetAllServicesUUID函數(shù)很不穩(wěn)定

按照接口文檔,整體流程都跑通了,就是獲取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;
????????????????}
????????????}


留個郵箱,我們相關(guān)工程師與你聯(lián)系。


個人信息保護,已隱藏



已發(fā)送至郵箱,請查收。


已發(fā)送至郵箱,請查收。


只有登錄才能回復,可以選擇微信賬號登錄

国产91精品新入口,国产成人综合网在线播放,九热这里只有精品,本道在线观看,美女视频a美女视频,韩国美女激情视频,日本美女pvp视频