安卓在數(shù)據(jù)回調(diào)時出現(xiàn)的掉包情況,(registerDataCallback被調(diào)用的次數(shù)和預(yù)期的不符)
private?void?registerDataCallback(UsbDevice?usbDevice){ ????try?{ ????????WCHUARTManager.getInstance().registerDataCallback(usbDevice,?new?IDataCallback()?{ ????????????@Override ????????????public?void?onData(int?serialNumber,?byte[]?buffer,?int?length)?{ ????????????????LogUtil.d(String.format(Locale.getDefault(),"serial?%d?receive?data?%d:%s",?serialNumber,length,?FormatUtil.bytesToHexString(buffer,?length))); ????????????????//1.注意回調(diào)的執(zhí)行線程與調(diào)用回調(diào)方法的線程屬于同一線程 ????????????????//2.此處所在的線程將是線程池中多個端點的讀取線程,可打印線程id查看 ????????????????//3.buffer是底層數(shù)組,如果此處將其傳給其他線程使用,例如通過runOnUiThread顯示數(shù)據(jù)在界面上, ????????????????//涉及到線程切換需要一定時間,buffer可能被讀到的新數(shù)據(jù)覆蓋,可以新建一個臨時數(shù)組保存數(shù)據(jù) ????????????????byte[]?data=new?byte[length]; ????????????????System.arraycopy(buffer,0,data,0,data.length); ????????????????if(FILE_TEST){ ????????????????????updateReadDataToFile(usbDevice,serialNumber,data,length); ????????????????}else?{ ????????????????????updateReadData(usbDevice,serialNumber,data,length); ????????????????} ????????????} ????????}); ????}?catch?(Exception?e)?{ ????????LogUtil.d(e.getMessage()); ????} }
請問這個是什么原因
熱門產(chǎn)品 :
CH585: 集成高速USB和NFC的藍(lán)牙MCU