<template>
|
<view>
|
<view class="uni-padding-wrap">
|
NFC
|
<view class="uni-common-mt" style="background: #fff; padding: 20upx">
|
<text>UID: {{ UID }}</text>
|
<text>状态:{{ tip }}</text>
|
<text>消息:{{ msg }}</text>
|
</view>
|
</view>
|
<button @click="writeData">writeData</button>
|
</view>
|
</template>
|
|
<script>
|
var NfcAdapter;
|
|
export default {
|
props: {
|
// 定义一个 UID 属性,可以用来接收父组件传递的数据
|
uid: {
|
type: String,
|
default: ''
|
}
|
},
|
data() {
|
return {
|
title: 'redNFC',
|
UID: '',
|
msg: '',
|
tip: '',
|
readyWriteData: false
|
};
|
},
|
watch: {
|
uid(newVal) {
|
// 当父组件更新 UID 时,同步到本地数据
|
this.UID = newVal;
|
}
|
},
|
onLoad() {
|
console.log('onLoad');
|
},
|
onShow() {
|
console.log('onShow');
|
this.NFCInit();
|
},
|
onHide() {
|
console.log('onHide');
|
this.NFCReadUID();
|
},
|
methods: {
|
clearInfo() {
|
this.UID = '';
|
this.tip = '';
|
this.msg = '';
|
},
|
writeData() {
|
// Your existing writeData logic
|
},
|
NFCReadUID2() {
|
// Your existing NFCReadUID2 logic
|
},
|
hexStringToBytes(str) {
|
var bytes = [];
|
for (var i = 0; i < str.length; i += 2) {
|
bytes.push(parseInt(str.substring(i, i + 2), 16));
|
}
|
return bytes;
|
},
|
NFCInit() {
|
try {
|
var main = plus.android.runtimeMainActivity();
|
var Intent = plus.android.importClass('android.content.Intent');
|
var Activity = plus.android.importClass('android.app.Activity');
|
var PendingIntent = plus.android.importClass('android.app.PendingIntent');
|
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
|
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
|
var _nfcAdapter = NfcAdapter.getDefaultAdapter(main);
|
|
var ndef = new IntentFilter('android.nfc.action.NDEF_DISCOVERED'); //NfcAdapter.ACTION_NDEF_DISCOVERED
|
var tag = new IntentFilter('android.nfc.action.TAG_DISCOVERED'); //NfcAdapter.ACTION_TECH_DISCOVERED
|
var tech = new IntentFilter('android.nfc.action.TECH_DISCOVERED');
|
var intentFiltersArray = [ndef, tag, tech];
|
|
var techListsArray = [
|
['android.nfc.tech.Ndef'],
|
['android.nfc.tech.IsoDep'],
|
['android.nfc.tech.NfcA'],
|
['android.nfc.tech.NfcB'],
|
['android.nfc.tech.NfcF'],
|
['android.nfc.tech.Nfcf'],
|
['android.nfc.tech.NfcV'],
|
['android.nfc.tech.NdefFormatable'],
|
['android.nfc.tech.MifareClassi'],
|
['android.nfc.tech.MifareUltralight']
|
];
|
|
var _intent = new Intent(main, main.getClass());
|
_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
var pendingIntent = PendingIntent.getActivity(main, 0, _intent, 0);
|
|
if (_nfcAdapter == null) {
|
this.tip = '本设备不支持NFC!';
|
} else if (_nfcAdapter.isEnabled() == false) {
|
this.tip = 'NFC功能未打开!';
|
} else {
|
this.tip = 'NFC正常';
|
_nfcAdapter.enableForegroundDispatch(main, pendingIntent, IntentFilter, techListsArray);
|
}
|
} catch (e) {
|
console.log(e);
|
}
|
},
|
NFCReadUID() {
|
console.log(1234567890)
|
var main = plus.android.runtimeMainActivity();
|
var _intent = main.getIntent();
|
var _action = _intent.getAction();
|
|
console.log('NFCReadUID action:', _action);
|
|
if (NfcAdapter.ACTION_NDEF_DISCOVERED == _action || NfcAdapter.ACTION_TAG_DISCOVERED == _action || NfcAdapter.ACTION_TECH_DISCOVERED == _action) {
|
var Tag = plus.android.importClass('android.nfc.Tag');
|
var tagFromIntent = _intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
var uid = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
|
var text = _intent.getByteArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
|
var rawMsgs = _intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
|
|
this.UID = this.Bytes2HexString(uid);
|
this.msg = text;
|
|
// 触发自定义事件,将 UID 传递给父组件
|
this.$emit('update:uid', this.UID);
|
|
uni.showToast({
|
title: text + "~~" + this.UID,
|
icon: 'none'
|
});
|
}
|
},
|
Bytes2HexString(arrBytes) {
|
var str = '';
|
for (var i = 0; i < arrBytes.length; i++) {
|
var tmp;
|
var num = arrBytes[i];
|
if (num < 0) {
|
tmp = (255 + num + 1).toString(16);
|
} else {
|
tmp = num.toString(16);
|
}
|
if (tmp.length == 1) {
|
tmp = '0' + tmp;
|
}
|
str += tmp;
|
}
|
return str;
|
},
|
toast(content) {
|
uni.showToast({
|
title: content,
|
icon: 'none'
|
});
|
}
|
}
|
};
|
</script>
|
|
<style></style>
|