关于微信小程序NFC发送指令功能

  • Post author:
  • Post category:小程序


先获取使用wx.getNFCAdapter获取实例

IsoDep支持ISO-DEP (ISO 14443-4)标准的读写(有不同的芯片,文档有支持其他的),刚好对应我所发指令的卡片,获取实例的getIsoDep

开始startDiscovery->监听onDiscovered,传入要监听的函数

根据文档IsoDep的操作

连接->复位->发送指令-》获取到ArrayBuffer转成十六制,再根据具体需求转换,如果读取的是中文要转gbk,图片直接十六进制转base64

代码如下

	getNFC() {
				let that = this
				const nfc = wx.getNFCAdapter()
				this.nfc = nfc
				let isoDep = nfc.getIsoDep()
				this.isoDep = isoDep
				nfc.startDiscovery({
					success(res) {
						nfc.onDiscovered(discoverHandler)
					},
					fail(err) {
						if (!err.errCode) {
							wx.showToast({
								title: '请检查NFC功能是否正常!',
								icon: 'none'
							})
							return
						}
						switch (err.errCode) {
							case 13000:
								wx.showToast({
									title: '设备不支持NFC!',
									icon: 'none'
								})
								break;
							case 13001:
								wx.showToast({
									title: '系统NFC开关未打开!',
									icon: 'none'
								})
								break;
							case 13019:
								wx.showToast({
									title: '用户未授权!',
									icon: 'none'
								})
								break;
							case 13010:
								wx.showToast({
									title: '未知错误!',
									icon: 'none'
								})
								break;
						}
					}
				})

				function discoverHandler(res) {
					let {
						techs
					} = res
					if (techs.includes('ISO-DEP')) {
						wx.showToast({
							title: '贴卡成功,可进行通信操作',
							icon: 'none'
						})
						
						that.WriteNFC()
					} else {
						wx.showToast({
							title: '该设备不支持ISO-DEP标准',
							icon: 'none'
						})
					}
				}
			},
			async WriteNFC(x, y) {
				let that = this
				console.log('调用命令')
				await this.connect()
			},
			// 连接
			connect() {
				let that = this
				console.log('连接')
				this.isoDep.connect({
					success: res => {
						that.getHistoricalBytes()
					},
					fail: error => {
						console.log(error)
						return
					}
				})
			},
			async getHistoricalBytes() {
				let that = this
				console.log('复位')
				this.isoDep.getHistoricalBytes({
					success: res => {
						that.instructionsList()
					},
					fail: error => {
						console.log(error)
						return
					}
				})
			},
	
			async instructionsList() {
				// 发送的指令集合,根据具体要求写
				await this.transceiveFunc('00a40000023f00') 
			},

			transceiveFunc(instructions) {
				let that = this
				const sendData = new Uint8Array(instructions.match(/[\da-f]{2}/gi).map(function(h) {
					return parseInt(h, 16)
				}))
				return new Promise((resolve, rejected) => {
					this.isoDep.transceive({
						data: sendData.buffer,
						success: res => {
							console.log('传输成功')
							let resbuf = res.data
							let str = that.ab2str(resbuf)
							resolve()
						},
						fail: error => {
							console.log(error)
							rejected('错误')
						}
					})
				})
			},

			ab2str(buffer) {
				const hexArr = Array.prototype.map.call(
					new Uint8Array(buffer),
					function(bit) {
						return ('00' + bit.toString(16)).slice((-2))
					})
				return hexArr.join('')
			},



版权声明:本文为m0_67080394原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。