nfcA.transceive获取的结果ArrayBuffer解析成字符串乱码?
目标:手机nfc识别ic卡获取ic卡的信息;
问题:nfcA.transceive获取的结果ArrayBuffer解析成字符串乱码,用decodeURIComponent报错:url malformed
手机:华为p30,EMUI:11.0.0,安卓10
ps:使用名叫“nfc读写”的小程序识别卡片可以得到一个类似id的的信息,如AD2A04AF
代码如下: const app = getApp();
var NFCAdapter = wx.getNFCAdapter();
Page({
data: {
id: ""
},
onShow() {
NFCAdapter.startDiscovery({
success: res => {
this.init();
},
fail: error => {
console.log(error)
}
})
},
str2ab(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
},
init() {
NFCAdapter.onDiscovered(callback => {
if (callback.techs.includes(NFCAdapter.tech.nfcA)) {
const nfcA = NFCAdapter.getNfcA();
app.nfcA = nfcA;
nfcA.connect({
success: res => {
nfcA.transceive({
data: this.str2ab('GET DATA'),
success: res => {
console.log(res.data) //打印结果 <ArrayBuffer:byteLength=16>
let k = String.fromCharCode.apply(null, new Uint16Array(res.data));
console.log(k) // k为乱码
console.log(decodeURIComponent(escape(k))) // 这里报错:url malformed
},
fail: error => {
console.log(error)
}
})
}
})
}
})
},
})