|
|
@@ -7,13 +7,13 @@ var isFirst = false; |
|
|
|
|
|
|
|
export function startScanNfcDevice(callBack) { |
|
|
|
isFirst = false; |
|
|
|
initNfcAdapter(function (res) { |
|
|
|
initNfcAdapter(function(res) { |
|
|
|
if (res.code == successCode) { |
|
|
|
console.log('初始化NFC适配器实例成功', res) |
|
|
|
//注册贴卡监听回调 |
|
|
|
nfcAdapter.onDiscovered(function (res) { |
|
|
|
nfcAdapter.onDiscovered(function(res) { |
|
|
|
console.log('监听回调到卡片了', res); |
|
|
|
if(!isFirst) { |
|
|
|
if (!isFirst) { |
|
|
|
if (res.techs.includes(nfcAdapter.tech.isoDep)) { //获取isoDep实例 |
|
|
|
console.log('发现' + nfcAdapter.tech.isoDep + '卡'); |
|
|
|
isoDep = nfcAdapter.getIsoDep(); |
|
|
@@ -28,10 +28,10 @@ export function startScanNfcDevice(callBack) { |
|
|
|
}); |
|
|
|
//开始监听贴卡 |
|
|
|
nfcAdapter.startDiscovery({ |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
console.log('监听贴卡成功', res) |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
console.log('监听贴卡失败', err) |
|
|
|
var msg = ""; |
|
|
|
if (err) { |
|
|
@@ -59,6 +59,7 @@ export function offDiscovered(callBack) { |
|
|
|
//注册贴卡监听回调 |
|
|
|
nfcAdapter.offDiscovered(discoverHandler); |
|
|
|
} |
|
|
|
|
|
|
|
function discoverHandler(res, callBack) { |
|
|
|
if (res.techs.includes(nfcAdapter.tech.isoDep)) { //获取isoDep实例 |
|
|
|
console.log('发现' + nfcAdapter.tech.isoDep + '卡'); |
|
|
@@ -73,10 +74,10 @@ function discoverHandler(res, callBack) { |
|
|
|
export function startDiscovery(callBack) { |
|
|
|
//开始监听贴卡 |
|
|
|
nfcAdapter.startDiscovery({ |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
console.log('监听贴卡成功', res) |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
console.log('监听贴卡失败', err) |
|
|
|
var msg = ""; |
|
|
|
if (err) { |
|
|
@@ -94,14 +95,14 @@ export function startDiscovery(callBack) { |
|
|
|
export function stopScanNfcDevice(callBack) { |
|
|
|
if (nfcAdapter != null) { |
|
|
|
nfcAdapter.stopDiscovery({ |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
callBack.call(this, { |
|
|
|
code: successCode, |
|
|
|
msg: "停止扫描成功", |
|
|
|
data: null |
|
|
|
}) |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
var msg = ""; |
|
|
|
if (err) { |
|
|
|
msg = nfcErrMsg(err.errCode) |
|
|
@@ -124,7 +125,7 @@ export function stopScanNfcDevice(callBack) { |
|
|
|
export function connectDevice(callBack) { |
|
|
|
if (isoDep != null) { |
|
|
|
isoDep.connect({ |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
console.log('连接卡片成功', res) |
|
|
|
callBack.call(this, { |
|
|
|
code: successCode, |
|
|
@@ -132,12 +133,12 @@ export function connectDevice(callBack) { |
|
|
|
data: null |
|
|
|
}) |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
var msg = ""; |
|
|
|
if (err) { |
|
|
|
msg = nfcErrMsg(err.errCode) |
|
|
|
} |
|
|
|
console.log("链接卡片失败:",err.errCode) |
|
|
|
console.log("链接卡片失败:", err.errCode) |
|
|
|
callBack.call(this, { |
|
|
|
code: errorCode, |
|
|
|
msg: msg, |
|
|
@@ -156,14 +157,14 @@ export function connectDevice(callBack) { |
|
|
|
export function disConnectDevice(callBack) { |
|
|
|
if (isoDep != null) { |
|
|
|
isoDep.close({ |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
callBack.call(this, { |
|
|
|
code: successCode, |
|
|
|
msg: "断开卡片成功", |
|
|
|
data: null |
|
|
|
}) |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
var msg = ""; |
|
|
|
if (err) { |
|
|
|
msg = nfcErrMsg(err.errCode) |
|
|
@@ -197,7 +198,7 @@ export function sendMessageToDevice(cmdArr, callBack) { |
|
|
|
var sendbuf = hexStringToArrayBuffer(cmdArr[i]); |
|
|
|
isoDep.transceive({ |
|
|
|
data: sendbuf, |
|
|
|
success: function (res) { |
|
|
|
success: function(res) { |
|
|
|
var resultCmd = arrayBufferToHexString(res.data); |
|
|
|
resultCmdArr.push(resultCmd); |
|
|
|
if (resultCmdArr.length == cmdArr.length) { |
|
|
@@ -208,7 +209,7 @@ export function sendMessageToDevice(cmdArr, callBack) { |
|
|
|
}) |
|
|
|
} |
|
|
|
}, |
|
|
|
fail: function (err) { |
|
|
|
fail: function(err) { |
|
|
|
if (i == 0) { |
|
|
|
console.log('发送数据失败', err); |
|
|
|
var msg = ""; |
|
|
@@ -233,7 +234,7 @@ export function removeObj() { |
|
|
|
|
|
|
|
|
|
|
|
function initNfcAdapter(callBack) { |
|
|
|
nfcAdapter = uni.getNFCAdapter() //获取NFC适配器实例 |
|
|
|
nfcAdapter = wx.getNFCAdapter() //获取NFC适配器实例 |
|
|
|
console.log("获取NFC适配器实例:", nfcAdapter); |
|
|
|
if (nfcAdapter != null) { |
|
|
|
callBack.call(this, { |
|
|
@@ -253,7 +254,7 @@ function initNfcAdapter(callBack) { |
|
|
|
//十六进制字符串转字节数组 |
|
|
|
function hexStringToArrayBuffer(str) { |
|
|
|
var hex = str; |
|
|
|
return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { |
|
|
|
return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function(h) { |
|
|
|
return parseInt(h, 16) |
|
|
|
})).buffer; |
|
|
|
}; |