Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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.content.IntentFilter.MalformedMimeTypeException;
15import android.os.Build;
16import android.os.Parcelable;
17import android.util.Log;
18import android.content.pm.PackageManager;
19
20public class QtNfc
21{
22 static private final String TAG = "QtNfc";
23 static private NfcAdapter m_adapter = null;
24 static private PendingIntent m_pendingIntent = null;
25 static private Context m_context = null;
26 static private Activity m_activity = null;
27
28 static public void setContext(Context context)
29 {
30 m_context = context;
31 if (context instanceof Activity) m_activity = (Activity) context;
32 m_adapter = NfcAdapter.getDefaultAdapter(context);
33
34 if (m_activity == null) {
35 Log.w(TAG, "New NFC tags will only be recognized with Android activities and not with Android services.");
36 return;
37 }
38
39 if (m_adapter == null) {
40 return;
41 }
42
43 // Since Android 12 (API level 31) it's mandatory to specify mutability
44 // of PendingIntent. We need a mutable intent, which was a default
45 // option earlier.
46 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
47 : 0;
48 m_pendingIntent = PendingIntent.getActivity(
49 m_activity,
50 0,
51 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
52 flags);
53 }
54
55 static public boolean startDiscovery()
56 {
57 if (m_adapter == null || m_activity == null
58 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
59 return false;
60
61 m_activity.runOnUiThread(new Runnable() {
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 public 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 public void run() {
101 try {
102 m_adapter.disableForegroundDispatch(m_activity);
103 } catch(IllegalStateException e) {
104 // On Android we must call disableForegroundDispatch when the activity is in foreground, only.
105 Log.d(TAG, "disableForegroundDispatch failed: " + e.toString());
106 }
107 }
108 });
109 return true;
110 }
111
112 static public boolean isEnabled()
113 {
114 if (m_adapter == null) {
115 return false;
116 }
117
118 return m_adapter.isEnabled();
119 }
120
121 static public boolean isSupported()
122 {
123 return (m_adapter != null);
124 }
125
126 static public Intent getStartIntent()
127 {
128 Log.d(TAG, "getStartIntent");
129 if (m_activity == null) return null;
130
131 Intent intent = m_activity.getIntent();
132 if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ||
133 NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) ||
134 NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
135 return intent;
136 } else {
137 return null;
138 }
139 }
140
141 static public Parcelable getTag(Intent intent)
142 {
143 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
144 }
145}
static boolean stopDiscovery()
Definition QtNfc.java:93
static void setContext(Context context)
Definition QtNfc.java:28
static boolean startDiscovery()
Definition QtNfc.java:55
static Parcelable getTag(Intent intent)
Definition QtNfc.java:141
static void * context
#define TAG(x)
GLbitfield flags
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]