網(wǎng)站屏蔽國內(nèi)ip個人seo外包
問題:連接藍牙后,調(diào)用小程序writeBLECharacteristicValue,返回傳輸數(shù)據(jù)成功,查詢硬件響應發(fā)現(xiàn)沒有存儲進去?
解決:一直以為是這個write方法的問題,找了很多相關貼,后續(xù)進行硬件日志查詢,發(fā)現(xiàn)傳輸?shù)臄?shù)據(jù)確實傳成功了,但是只傳輸了二分之一。
原因:微信小程序?qū)τ趥鬏擵alue有默認字節(jié)限制,默認是20,傳輸內(nèi)容超過了20,所以只傳過去了前20個字節(jié)。超過字節(jié)限制,不會報錯,也會報傳輸成功。
行動:查詢小程序字節(jié)限制(wx.getBLEMTU),對傳輸內(nèi)容做分包處理再傳輸
注意:在使用微信小程序getBLEMTU獲取mtu時,一定要減3!!!!!!
function stringToAsciiCodesAndSplit(str: string, mtuSize = 20): Uint8Array[] {// 將字符串轉(zhuǎn)換為 ASCII 碼的 ArrayBufferconst asciiCodes: number[] = []for (let i = 0; i < str.length; i++) {asciiCodes.push(str.charCodeAt(i))}const uint8Array = new Uint8Array(asciiCodes)console.log('uint8Array.buffer', uint8Array.buffer)// 定義一個 packets 數(shù)組,它將存儲多個 Uint8Array 類型的元素const packets: Uint8Array[] = []// 根據(jù) MTU字節(jié) 大小拆分數(shù)據(jù)for (let i = 0; i < uint8Array.length; i += mtuSize) {packets.push(uint8Array.slice(i, i + mtuSize))}return packets
}
const command = `atxxxxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyyyy`
//mtu為字節(jié)限制const buffer = stringToAsciiCodesAndSplit(command, mtu)buffer.forEach((packet, index) => {// 將每個包轉(zhuǎn)換為 ArrayBufferconst addBuffer = packet.bufferTaro.writeBLECharacteristicValue({deviceId: deviceId,serviceId: serviceId,characteristicId: characteristicId,value: addBuffer,success: function (res) {console.log(`第 ${index + 1} 個WIFI添加包發(fā)送成功:`, res)},fail: function (err) {console.log(`第 ${index + 1} 個WIFI添加包發(fā)送失敗:`, err)}})})