aboutsummaryrefslogtreecommitdiffstats
path: root/GPL.txt
blob: d511905c1647a1e311e8b20d5930a37a9c2531cd (plain) (blame)
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
<
// src/android/NfcWriterPlugin.java
package io.coinos.nfcwriter;

import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.NfcAdapter.ReaderCallback;
import android.os.Bundle;
import android.util.Log;

import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

@CapacitorPlugin(name = "NfcWriter")
public class NfcWriterPlugin extends Plugin {
  private NfcAdapter nfcAdapter;

  @Override
  public void load() {
    super.load();
    nfcAdapter = NfcAdapter.getDefaultAdapter(getContext());
  }

  @PluginMethod
  public void enableExclusiveNfcMode(PluginCall call) {
    if (nfcAdapter == null) {
      call.reject("NFC adapter not available");
      return;
    }

    nfcAdapter.enableReaderMode(
      getActivity(),
      new ReaderCallback() {
        @Override
        public void onTagDiscovered(Tag tag) {
          Log.d("NfcWriter", "Tag discovered");
          bridge.getWebView().post(() -> {
            bridge.getWebView().evaluateJavascript(
              "window.dispatchEvent(new CustomEvent('nativeNfcTagDiscovered'))",
              null
            );
          });
        }
      },
      NfcAdapter.FLAG_READER_NFC_A |
      NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK |
      NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS,
      null
    );

    call.resolve();
  }

  @PluginMethod
  public void disableExclusiveNfcMode(PluginCall call) {
    if (nfcAdapter != null) {
      nfcAdapter.disableReaderMode(getActivity());
    }
    call.resolve();
  }
}