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 Activity m_activity = null;
27
28 static void setContext(Context context)
29 {
30 if (context instanceof Activity) m_activity = (Activity) context;
31 m_adapter = NfcAdapter.getDefaultAdapter(context);
32
33 if (m_activity == null) {
34 Log.w(TAG, "New NFC tags will only be recognized with Android activities and not with Android services.");
35 return;
36 }
37
38 if (m_adapter == null) {
39 return;
40 }
41
42 // Since Android 12 (API level 31) it's mandatory to specify mutability
43 // of PendingIntent. We need a mutable intent, which was a default
44 // option earlier.
45 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
46 : 0;
47 m_pendingIntent = PendingIntent.getActivity(
48 m_activity,
49 0,
50 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
51 flags);
52 }
53
54 static boolean startDiscovery()
55 {
56 if (m_adapter == null || m_activity == null
57 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
58 return false;
59
60 m_activity.runOnUiThread(new Runnable() {
61 @Override
62 public void run() {
63 IntentFilter[] filters = new IntentFilter[3];
64 filters[0] = new IntentFilter();
65 filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED);
66 filters[0].addCategory(Intent.CATEGORY_DEFAULT);
67 filters[1] = new IntentFilter();
68 filters[1].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
69 filters[1].addCategory(Intent.CATEGORY_DEFAULT);
70 try {
71 filters[1].addDataType("*/*");
72 } catch (MalformedMimeTypeException e) {
73 throw new RuntimeException("IntentFilter.addDataType() failed");
74 }
75 // some tags will report as tech, even if they are ndef formatted/formattable.
76 filters[2] = new IntentFilter();
77 filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
78 String[][] techList = new String[][]{
79 {"android.nfc.tech.Ndef"},
80 {"android.nfc.tech.NdefFormatable"}
81 };
82 try {
83 m_adapter.enableForegroundDispatch(m_activity, m_pendingIntent, filters, techList);
84 } catch(IllegalStateException e) {
85 // On Android we must call enableForegroundDispatch when the activity is in foreground, only.
86 Log.d(TAG, "enableForegroundDispatch failed: " + e.toString());
87 }
88 }
89 });
90 return true;
91 }
92
93 static boolean stopDiscovery()
94 {
95 if (m_adapter == null || m_activity == null
96 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
97 return false;
98
99 m_activity.runOnUiThread(new Runnable() {
100 @Override
101 public void run() {
102 try {
103 m_adapter.disableForegroundDispatch(m_activity);
104 } catch(IllegalStateException e) {
105 // On Android we must call disableForegroundDispatch when the activity is in foreground, only.
106 Log.d(TAG, "disableForegroundDispatch failed: " + e.toString());
107 }
108 }
109 });
110 return true;
111 }
112
113 static boolean isEnabled()
114 {
115 if (m_adapter == null) {
116 return false;
117 }
118
119 return m_adapter.isEnabled();
120 }
121
122 static boolean isSupported()
123 {
124 return (m_adapter != null);
125 }
126
127 static Intent getStartIntent()
128 {
129 Log.d(TAG, "getStartIntent");
130 if (m_activity == null) return null;
131
132 Intent intent = m_activity.getIntent();
133 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
134 NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) ||
135 NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
136 return intent;
137 } else {
138 return null;
139 }
140 }
141
142 @SuppressWarnings("deprecation")
143 static Parcelable getTag(Intent intent)
144 {
145 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
146 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG, Tag.class);
147
148 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
149 }
150}
QPainter Context
static const QString context()
Definition java.cpp:396
quint32 Tag
#define TAG(x)
GLbitfield flags