|
@@ -1,4 +1,11 @@
|
|
|
-<template></template>
|
|
|
+<template>
|
|
|
+ <view class="nfc-container">
|
|
|
+ <view class="nfc-container-image">
|
|
|
+ <image class="nfc-gif" src="@/static/images/common/nfc-flash.gif" mode="aspectFit" style="width: 230px"></image>
|
|
|
+ </view>
|
|
|
+ <view class="nfc-container-text">{{ commonStore.nfcWaiting }}</view>
|
|
|
+ </view>
|
|
|
+</template>
|
|
|
|
|
|
<script setup>
|
|
|
import config from "@/config";
|
|
@@ -8,298 +15,18 @@ import { ref, reactive, computed, getCurrentInstance, toRefs, inject, watch } fr
|
|
|
import { useStores, commonStores, xunJianStores } from "@/store/modules/index";
|
|
|
|
|
|
const { proxy } = getCurrentInstance();
|
|
|
-const xunJianStore = xunJianStores();
|
|
|
+const commonStore = commonStores();
|
|
|
|
|
|
const data = reactive({});
|
|
|
const {} = toRefs(data);
|
|
|
|
|
|
-var NfcAdapter;
|
|
|
-var NdefRecord;
|
|
|
-var NdefMessage;
|
|
|
-var waiting;
|
|
|
-var readyWriteData = false; //开启写
|
|
|
-var readyRead = false; //开启读
|
|
|
-var techListsArray = [
|
|
|
- ["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.MifareClassic"],
|
|
|
- ["android.nfc.tech.MifareUltralight"],
|
|
|
-];
|
|
|
-
|
|
|
-function initNFC() {
|
|
|
- if (uni.getSystemInfoSync().platform == "android") {
|
|
|
- listenNFCStatus();
|
|
|
- }
|
|
|
-}
|
|
|
-function readNFC(callback) {
|
|
|
- if (uni.getSystemInfoSync().platform == "android") {
|
|
|
- readData(callback);
|
|
|
- }
|
|
|
-}
|
|
|
-function closeNFC() {
|
|
|
- if (uni.getSystemInfoSync().platform == "android") {
|
|
|
- closeReadAndWrite();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function listenNFCStatus() {
|
|
|
- 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);
|
|
|
-
|
|
|
- if (nfcAdapter == null) {
|
|
|
- uni.showToast({
|
|
|
- title: "设备不支持NFC!",
|
|
|
- icon: "none",
|
|
|
- });
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- if (!nfcAdapter.isEnabled()) {
|
|
|
- uni.showToast({
|
|
|
- title: "请在系统设置中先启用NFC功能!",
|
|
|
- icon: "none",
|
|
|
- });
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- var intent = new Intent(main, main.getClass());
|
|
|
- intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
- var pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
|
|
|
- var ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
|
|
|
- ndef.addDataType("*/*");
|
|
|
- var intentFiltersArray = [ndef];
|
|
|
-
|
|
|
- plus.globalEvent.addEventListener(
|
|
|
- "newintent",
|
|
|
- function () {
|
|
|
- // 轮询调用 NFC
|
|
|
- setTimeout(handle_nfc_data1, 1000);
|
|
|
- },
|
|
|
- false
|
|
|
- );
|
|
|
- plus.globalEvent.addEventListener(
|
|
|
- "pause",
|
|
|
- function (e) {
|
|
|
- if (nfcAdapter) {
|
|
|
- //关闭前台调度系统
|
|
|
- //恢复默认状态
|
|
|
- nfcAdapter.disableForegroundDispatch(main);
|
|
|
- }
|
|
|
- },
|
|
|
- false
|
|
|
- );
|
|
|
- plus.globalEvent.addEventListener(
|
|
|
- "resume",
|
|
|
- function (e) {
|
|
|
- if (nfcAdapter) {
|
|
|
- //开启前台调度系统
|
|
|
- // 优于所有其他NFC
|
|
|
- nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
|
|
|
- }
|
|
|
- },
|
|
|
- false
|
|
|
- );
|
|
|
- nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
|
|
|
- } catch (e) {
|
|
|
- console.error(e);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function handle_nfc_data1() {
|
|
|
- NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
|
|
|
- NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
|
|
|
- var main = plus.android.runtimeMainActivity();
|
|
|
- var intent = main.getIntent();
|
|
|
- //console.log("action type:" + intent.getAction());
|
|
|
- if ("android.nfc.action.TECH_DISCOVERED" == intent.getAction()) {
|
|
|
- if (readyWriteData) {
|
|
|
- //__write(intent);
|
|
|
- readyWriteData = false;
|
|
|
- } else if (readyRead) {
|
|
|
- __read(intent);
|
|
|
- readyRead = false;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function showToast(msg) {
|
|
|
- plus.nativeUI.toast(msg);
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @NFC写入
|
|
|
- * @param {*} intent
|
|
|
- * @returns
|
|
|
- */
|
|
|
-function __write(intent) {
|
|
|
- try {
|
|
|
- waiting.setTitle("请勿移开标签\n正在写入...");
|
|
|
- var text = document.getElementById("text").value;
|
|
|
- console.log("text=" + text);
|
|
|
-
|
|
|
- var textBytes = plus.android.invoke(text, "getBytes");
|
|
|
- var textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, plus.android.invoke("text/plain", "getBytes"), plus.android.invoke("", "getBytes"), textBytes);
|
|
|
- var message = new NdefMessage([textRecord]);
|
|
|
- var Ndef = plus.android.importClass("android.nfc.tech.Ndef");
|
|
|
- var NdefFormatable = plus.android.importClass("android.nfc.tech.NdefFormatable");
|
|
|
- var tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
|
- var ndef = Ndef.get(tag);
|
|
|
- if (ndef != null) {
|
|
|
- var size = message.toByteArray().length;
|
|
|
- console.log("size=" + size);
|
|
|
- ndef.connect();
|
|
|
- if (!ndef.isWritable()) {
|
|
|
- showToast("tag不允许写入");
|
|
|
- waiting.close();
|
|
|
- return;
|
|
|
- }
|
|
|
- if (ndef.getMaxSize() < size) {
|
|
|
- showToast("文件大小超出容量");
|
|
|
- waiting.close();
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- ndef.writeNdefMessage(message);
|
|
|
- waiting.close();
|
|
|
- showToast("写入数据成功.");
|
|
|
- return;
|
|
|
- } else {
|
|
|
- var format = NdefFormatable.get(tag);
|
|
|
- if (format != null) {
|
|
|
- try {
|
|
|
- format.connect();
|
|
|
- format.format(message);
|
|
|
- showToast("格式化tag并且写入message");
|
|
|
- waiting.close();
|
|
|
- return;
|
|
|
- } catch (e) {
|
|
|
- showToast("格式化tag失败.");
|
|
|
- waiting.close();
|
|
|
- return;
|
|
|
- }
|
|
|
- } else {
|
|
|
- showToast("Tag不支持NDEF");
|
|
|
- waiting.close();
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (e) {
|
|
|
- console.log("error=" + e);
|
|
|
- waiting.close();
|
|
|
- alert("写入失败");
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @NFC读取
|
|
|
- * @param {*} intent
|
|
|
- * @returns
|
|
|
- */
|
|
|
-function __read(intent) {
|
|
|
- try {
|
|
|
- var content = "";
|
|
|
- waiting.setTitle("请勿移开标签\n正在读取数据...");
|
|
|
- var tag = plus.android.importClass("android.nfc.Tag");
|
|
|
- tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
|
|
|
- var bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
|
|
|
- waiting.close();
|
|
|
- var tagid = bytesToHexString(tag.getId());
|
|
|
-
|
|
|
- uni.$emit("tagid", tagid); //将值存储监听器
|
|
|
- proxy.$tab.navigateBack(1); //返回上一级页面
|
|
|
- closeNFC();
|
|
|
- } catch (e) {
|
|
|
- uni.showToast({
|
|
|
- title: e,
|
|
|
- icon: "none",
|
|
|
- });
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-function bytesToHexString(inarray) {
|
|
|
- var i, j, x;
|
|
|
- var hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
|
|
|
- var out = "";
|
|
|
- for (j = 0; j < inarray.length; ++j) {
|
|
|
- x = parseInt(inarray[j]) & 0xff;
|
|
|
- i = (x >> 4) & 0x0f;
|
|
|
- out += hex[i];
|
|
|
- i = x & 0x0f;
|
|
|
- out += hex[i];
|
|
|
- }
|
|
|
- return out;
|
|
|
-}
|
|
|
-
|
|
|
-function reverseTwo(str) {
|
|
|
- var str1 = "";
|
|
|
- for (var i = 1; i <= str.length; i++) {
|
|
|
- str1 += str[i - 1];
|
|
|
- if (i % 2 == 0) {
|
|
|
- if (i == str.length) {
|
|
|
- break;
|
|
|
- }
|
|
|
- str1 += ":";
|
|
|
- }
|
|
|
- }
|
|
|
- var str2 = "";
|
|
|
- for (var i = str1.split(":").length - 1; i >= 0; i--) {
|
|
|
- str2 += str1.split(":")[i];
|
|
|
- }
|
|
|
- return str2;
|
|
|
-}
|
|
|
-
|
|
|
-if (uni.getSystemInfoSync().platform == "android") {
|
|
|
- //plus.globalEvent.addEventListener('plusready', listenNFCStatus, false);
|
|
|
-}
|
|
|
-
|
|
|
-function writeData() {
|
|
|
- var textEle = plus.globalEvent.getElementById("text");
|
|
|
- if (!textEle.value) {
|
|
|
- uni.showToast({
|
|
|
- title: "请输入要写入的内容!",
|
|
|
- icon: "none",
|
|
|
- });
|
|
|
- return;
|
|
|
- }
|
|
|
- readyWriteData = true;
|
|
|
- waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!");
|
|
|
-}
|
|
|
-
|
|
|
-function readData() {
|
|
|
- readyRead = true;
|
|
|
- waiting = plus.nativeUI.showWaiting("请将NFC标签靠近!", {
|
|
|
- modal: false,
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-function closeReadAndWrite() {
|
|
|
- readyWriteData = false;
|
|
|
- readyRead = false;
|
|
|
-
|
|
|
- if (waiting) {
|
|
|
- waiting.close();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
onLoad((options) => {
|
|
|
// 开启nfc监听
|
|
|
- initNFC();
|
|
|
- readNFC();
|
|
|
+ proxy.$nfc.readNFC();
|
|
|
});
|
|
|
|
|
|
onUnload(() => {
|
|
|
- closeNFC();
|
|
|
+ proxy.$nfc.closeNFC();
|
|
|
});
|
|
|
|
|
|
onReady(() => {});
|
|
@@ -310,4 +37,17 @@ onShow(() => {
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
-<style lang="scss" scoped></style>
|
|
|
+<style lang="scss" scoped>
|
|
|
+.nfc-container {
|
|
|
+ &-image {
|
|
|
+ display: flex;
|
|
|
+ width: 100%;
|
|
|
+ justify-content: center;
|
|
|
+ margin: 55% auto 40px auto;
|
|
|
+ }
|
|
|
+ &-text {
|
|
|
+ text-align: center;
|
|
|
+ font-size: 16px;
|
|
|
+ }
|
|
|
+}
|
|
|
+</style>
|