- 【实现】NFC 标签打开小程序
【小程序官方】NFC 标签必须是 [代码]NFC Data Exchange Format (NDEF)[代码] 类型; 标签中需要包含两条 Record : URI RecordType (记录1) Name Format (TNF): [代码]0x01[代码] (Well-Known)Type: [代码]U[代码] Payload: 小程序 URL SchemeAndroid Application Record, AAR(记录2) Type Name Format (TNF): [代码]0x04[代码] (NFC Forum external type)Type: [代码]android.com:pkg[代码]Payload: 微信安卓包名 [代码]com.tencent.mm[代码] 1、查看标签是否为 NDEF 类型 [图片] 2、写入两条 Record const records = [ { id: str2ab('mini-ios'), tnf: 1, type: str2ab('U'), payload: str2ab('小程序 URL Scheme', [0]) }, { id: str2ab('mini-android'), tnf: 4, type: str2ab('android.com:pkg'), payload: str2ab('com.tencent.mm') } ] /** * String to ArrayBuffer * @param {String} text 字符串 * @param {String} extraBytes 额外的字节数组 */ export function str2ab (text, extraBytes) { const uriStr = encodeURIComponent(text) const bytes = [] for (let i = 0; i < uriStr.length; i++) { const code = uriStr.charAt(i) if (code === '%') { const hex = uriStr.slice(i + 1, i + 3) const hexVal = parseInt(hex, 16) bytes.push(hexVal) i += 2 } else { bytes.push(code.charCodeAt(0)) } } if (extraBytes) { bytes.unshift(...extraBytes) } return new Uint8Array(bytes).buffer } 3、总结 1)区分【获取 NFC 的小程序 scheme】 与 【获取 scheme 码】的 API,不能用错接口 2)注意两条 Record 写入次序:先写入 URI RecordType (记录1),再写入 Android Application Record, AAR(记录2); 未按照顺序写入时,安卓只打开微信,没有拉起小程序(原因:https://developer.android.google.cn/guide/topics/connectivity/nfc/nfc?hl=zh_cn) 3)小程序 URL Scheme 转换 ArrayBuffer 时,在字节数组首位需额外加上 0x00;(因为 URI 记录数据的第一个字节为协议字段,小程序 URL Scheme 没有对应协议,故用 0x00,其实微信官方在写小程序 writeNdefMessage API 时,可以把这个逻辑封装起来,减少开发者学习成本)
2023-08-09 - 鸿蒙版微信1.0.9,使用canvas导出图片全黑,微信1.0.8没有问题。
目前升级的微信,几乎所有的鸿蒙版本的canvas,使用wx.canvasToTempFilePath导出图片,都有问题。 1.0.8没有遇到这个问题
08-11