222
schangxiang@126.com
2025-05-20 6fe443c4d26f0ec5cbcc2864a0dd9ab781c59761
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<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>