直接上代码
js
// pages/demo-09/demo-09.js
Page({
/**
* 页面的初始数据
*/
data: {
Nfcadapter:null,
nfcMsg:"",
writeMsg:"0",
},
readNfcMsg(e){
var that = this;
var adapter = this.data.Nfcadapter;
if (adapter.tech.mifareClassic) {
console.log('发现' + adapter.tech.mifareClassic + '卡');
let mifareClassic = adapter.getMifareClassic();
mifareClassic.connect({
success: res => {
console.log("设备已连接", res)
console.log("开始拼接验密指令。。。");
var arr = [0x60, 0x09, 0x11, 0x22, 0x33, 0x44, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; //第二位0x09是指第二扇区第二块 (小程序没有扇区的概念,但是nfc中扇区是连续的,所以有x个扇区就在小程序中变为了有4*x个块,块的序号从0开始)
var arrayBuffer = new Uint8Array(arr).buffer
console.log("解密指令为:", arrayBuffer);
mifareClassic.transceive({
data: arrayBuffer,
success: function (res) {
console.log('发送数据并解密成功, 接收数据如下:', res);
},
fail: function (err) {
console.log('发送数据失败A', err);
}
})
}
})
mifareClassic.isConnected({
success: function (isConnected) {
console.log('成功连接', isConnected);
var arr02 = [0x30, 0x09]; //第二位0x09是指第二扇区第二块 如果验密时的块号与这里的块号不一致会报错
var arrayBuffer02 = new Uint8Array(arr02).buffer;
console.log(arrayBuffer02);
mifareClassic.transceive({
data: arrayBuffer02,
success: function (res) {
console.log('读取数据:', res);
// let aid = parseInt(ab2hex(res.data), 16);
// console.log(aid);
var str =that.ab2str(res.data);
console.log(str);
that.setData({
nfcMsg:str
})
mifareClassic.close({
success: res => {
console.log('清除标签连接成功');
},
fail: error => {
console.error('清除标签连接失败');
},
complete: res => {
console.log(res);
}
});
},
fail: function (err) {
console.log('失败', err);
}
})
}
});
}
},
inputMsg(e){
this.setData({
writeMsg:e.detail.value
})
console.log(this.data.writeMsg);
},
writeNfcMsg(e){
var that =this;
var adapter = this.data.Nfcadapter;
if (adapter.tech.mifareClassic){
console.log('发现' + adapter.tech.mifareClassic + '卡');
let mifareClassic = adapter.getMifareClassic();
mifareClassic.connect({
success: res => {
console.log("设备已连接", res)
console.log("开始拼接验密指令。。。");
var arr = [0x60, 0x09, 0x11, 0x22, 0x33, 0x44, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; //第二位0x09是指第二扇区第二块
var arrayBuffer = new Uint8Array(arr).buffer
console.log("解密指令为:", arrayBuffer);
mifareClassic.transceive({
data: arrayBuffer,
success: function (res) {
console.log('发送数据并解密成功, 接收数据如下:', res);
},
fail: function (err) {
console.log('发送数据失败A', err);
}
})
}
})
mifareClassic.isConnected({
success: function (isConnected) {
console.log('成功连接', isConnected);
var buff= that.stringToArrayBuffer(that.data.writeMsg);
console.log(buff);
var remedyBuff = that.remedyArrayBuff(buff);
console.log(remedyBuff);
mifareClassic.transceive({
data: remedyBuff,
success: function (res) {
console.log('读取数据:', res);
mifareClassic.close({
success: res => {
console.log('清除标签连接成功');
},
fail: error => {
console.error('清除标签连接失败');
},
complete: res => {
console.log(res);
}
});
},
fail: function (err) {
console.log('失败', err);
}
})
}
});
}
},
stringToArrayBuffer :function (str) {
// 首先将字符串转为16进制
let val = ''
for (let i = 0; i < str.length; i++) {
if (val === '') {
val = str.charCodeAt(i).toString(16)
} else {
val += ',' + str.charCodeAt(i).toString(16)
}
console.log(val)
}
// 将16进制转化为ArrayBuffer
return new Uint8Array(
val.match(/[\da-f]{2}/gi).map(function (h) {
return parseInt(h, 16)
})
).buffer
},
ab2hex : function(buffer) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2);
}
);
return hexArr.join('');
},
ab2str(buf) {
var x1 = new Uint8Array(buf);
console.log(x1);
var index =0;
for(var i =0 ;i<x1.length;i++){
if(x1[i]!=0){
index++
}
}
var buf2 = new ArrayBuffer(index);
var x2 =new Uint8Array(buf2);
for(var i =0 ;i<index;i++){
x2[x2.length-1-i]=x1[x1.length-1-i]
}
console.log(buf2)
return String.fromCharCode.apply(null, new Uint8Array(buf2));
},
/**
* 生命周期函数--监听页面加载
*/
//补齐指令位和快位和16位数据,总共18位(数据位必须是16位所以要补齐)
remedyArrayBuff(buff1){
var x1 = new Uint8Array(buff1);
console.log(x1);
var buf2 = new ArrayBuffer(18);
var x2 =new Uint8Array(buf2);
x2[0]=160; //表示0xA0 写入指令
x2[1]=9; //表示0x09 表示写入的块是第2扇区第2块(小程序没有扇区的概念,但是nfc中扇区是连续的,所以有x个扇区就在小程序中变为了有4*x个块) 如果验密时的块号与这里的块号不一致会报错
for(var i =0;i<x1.length;i++){
x2[x2.length-1-i]=x1[x1.length-1-i];
}
console.log(x2);
console.log(buf2);
return buf2;
},
onLoad: function (options) {
this.data.Nfcadapter = wx.getNFCAdapter();
const adapter = this.data.Nfcadapter;
console.log("获取NFC实例", adapter)
adapter.onDiscovered(res => {
console.log("读取到卡片了", res);
let tagId = res.id;
console.log("获取到tagID:", tagId);
})
adapter.startDiscovery({
fail(err) {
console.log('failed to discover:', err)
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
wxml
<view class="readText">
<text>{{nfcMsg}}</text>
</view>
<view class="readnfc">
<button bindtap="readNfcMsg">读</button>
</view>
<view class="inputText">
<input type="text" type="text" bindblur="inputMsg" value="{{writeMsg}}"/>
</view>
<view class="readnfc">
<button bindtap="writeNfcMsg">写</button>
</view>
没有wxss