uni-app安卓appNFC读取RFID标签的UID

第一步:配置NFC权限
在这里插入图片描述
第二步:vue文件

<template>
  <view>
    <view class="uni-padding-wrap">
      NFC
      <view class="uni-common-mt" style="background: #fff; padding: 20upx">
        <text>
          UID:{{ UID }}
          {{ tip }}
        </text>
      </view>
    </view>
  </view>
</template>  

<script>
var NfcAdapter;
export default {
  data() {
    return {
      title: "redNFC",
      UID: "",
      msg: "",
      tip: "",
      data: [],
    };
  },
  onLoad() {
    console.log("onLoad");
  },
  onShow() {
    console.log("onShow");
    this.NFCInit();
  },
  onHide() {
    console.log("onHide");
    this.NFCReadUID();
  },
  methods: {
    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) {
        //TODO handle the exception
      }
    },
    NFCReadUID() {
      var main = plus.android.runtimeMainActivity();
      var _intent = main.getIntent();
      var _action = _intent.getAction();
      if (
        NfcAdapter.ACTION_NDEF_DISCOVERED == _action ||
        NfcAdapter.ACTION_TAG_DISCOVERED == _action ||
        NfcAdapter.ACTION_TECH_DISCOVERED == _action
      ) {
        var uid = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
        this.UID = this.Bytes2HexString(uid)//16进制
      }
    },
    //将byte[] 转为Hex,
    Bytes2HexString(arrBytes) {
      var str = [];
      for (var i = arrBytes.length - 1; i >= 0; i--) {
        var tmp;
        var num = arrBytes[i];
        if (num < 0) {
          //Java中数值是以补码的形式存在的,应用程序展示的十进制是补码对应真值。补码的存在主要为了简化计算机底层的运算,将减法运算直接当加法来做
          tmp = (255 + num + 1).toString(16);
        } else {
          tmp = num.toString(16);
        }
        if (tmp.length == 1) {
          tmp = "0" + tmp;
        }
        str += tmp;
      }
      return str;
    },
  },
};
</script>  

<style>
</style>  

第三步:16进制转10进制

this.UID = parseInt(this.Bytes2HexString(uid), 16); //16进制转10进制

注:此方法只是使用uni-app安卓app开发NFC读取RFID标签的UID,里面没有加入NFC写入RFID标签,如需使用NFC读取身份证,uni-app插件市场有

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐