Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QtNfc.java
Go to the documentation of this file.
1// Copyright (C) 2016 Centria research and development
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4package org.qtproject.qt.android.nfc;
5
6import java.lang.Runnable;
7
8import android.content.Context;
9import android.app.Activity;
10import android.app.PendingIntent;
11import android.content.Intent;
12import android.content.IntentFilter;
13import android.nfc.NfcAdapter;
14import android.nfc.Tag;
15import android.content.IntentFilter.MalformedMimeTypeException;
16import android.os.Build;
17import android.os.Parcelable;
18import android.util.Log;
19import android.content.pm.PackageManager;
20
21class QtNfc
22{
23 static private final String TAG = "QtNfc";
24 static private NfcAdapter m_adapter = null;
25 static private PendingIntent m_pendingIntent = null;
26 static private Context m_context = null;
27 static private Activity m_activity = null;
28
29 static void setContext(Context context)
30 {
31 m_context = context;
32 if (context instanceof Activity) m_activity = (Activity) context;
33 m_adapter = NfcAdapter.getDefaultAdapter(context);
34
35 if (m_activity == null) {
36 Log.w(TAG, "New NFC tags will only be recognized with Android activities and not with Android services.");
37 return;
38 }
39
40 if (m_adapter == null) {
41 return;
42 }
43
44 // Since Android 12 (API level 31) it's mandatory to specify mutability
45 // of PendingIntent. We need a mutable intent, which was a default
46 // option earlier.
47 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
48 : 0;
49 m_pendingIntent = PendingIntent.getActivity(
50 m_activity,
51 0,
52 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
53 flags);
54 }
55
56 static boolean startDiscovery()
57 {
58 if (m_adapter == null || m_activity == null
59 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
60 return false;
61
62 m_activity.runOnUiThread(new Runnable() {
63 @Override
64 public void run() {
65 IntentFilter[] filters = new IntentFilter[3];
66 filters[0] = new IntentFilter();
67 filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
68 filters[0].addCategory(Intent.CATEGORY_DEFAULT);
69 filters[1] = new IntentFilter();
70 filters[1].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
71 filters[1].addCategory(Intent.CATEGORY_DEFAULT);
72 try {
73 filters[1].addDataType("*/*");
74 } catch (MalformedMimeTypeException e) {
75 throw new RuntimeException("IntentFilter.addDataType() failed");
76 }
77 // some tags will report as tech, even if they are ndef formatted/formattable.
78 filters[2] = new IntentFilter();
79 filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
80 String[][] techList = new String[][]{
81 {"android.nfc.tech.Ndef"},
82 {"android.nfc.tech.NdefFormatable"}
83 };
84 try {
85 m_adapter.enableForegroundDispatch(m_activity, m_pendingIntent, filters, techList);
86 } catch(IllegalStateException e) {
87 // On Android we must call enableForegroundDispatch when the activity is in foreground, only.
88 Log.d(TAG, "enableForegroundDispatch failed: " + e.toString());
89 }
90 }
91 });
92 return true;
93 }
94
95 static boolean stopDiscovery()
96 {
97 if (m_adapter == null || m_activity == null
98 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
99 return false;
100
101 m_activity.runOnUiThread(new Runnable() {
102 @Override
103 public void run() {
104 try {
105 m_adapter.disableForegroundDispatch(m_activity);
106 } catch(IllegalStateException e) {
107 // On Android we must call disableForegroundDispatch when the activity is in foreground, only.
108 Log.d(TAG, "disableForegroundDispatch failed: " + e.toString());
109 }
110 }
111 });
112 return true;
113 }
114
115 static boolean isEnabled()
116 {
117 if (m_adapter == null) {
118 return false;
119 }
120
121 return m_adapter.isEnabled();
122 }
123
124 static boolean isSupported()
125 {
126 return (m_adapter != null);
127 }
128
129 static Intent getStartIntent()
130 {
131 Log.d(TAG, "getStartIntent");
132 if (m_activity == null) return null;
133
134 Intent intent = m_activity.getIntent();
135 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
136 NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) ||
137 NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
138 return intent;
139 } else {
140 return null;
141 }
142 }
143
144 @SuppressWarnings("deprecation")
145 static Parcelable getTag(Intent intent)
146 {
147 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
148 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag.class);
149
150 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
151 }
152}
QPainter Context
static const QString context()
Definition java.cpp:398
quint32 Tag
#define TAG(x)
GLbitfield flags