4package org.qtproject.qt.android.nfc;
6import java.lang.Runnable;
10import android.app.PendingIntent;
12import android.content.IntentFilter;
15import android.content.IntentFilter.MalformedMimeTypeException;
19import android.content.pm.PackageManager;
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;
33 m_adapter = NfcAdapter.getDefaultAdapter(
context);
35 if (m_activity ==
null) {
36 Log.w(
TAG,
"New NFC tags will only be recognized with Android activities and not with Android services.");
40 if (m_adapter ==
null) {
47 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
49 m_pendingIntent = PendingIntent.getActivity(
52 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
56 static boolean startDiscovery()
58 if (m_adapter ==
null || m_activity ==
null
59 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
62 m_activity.runOnUiThread(
new Runnable() {
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);
73 filters[1].addDataType(
"*/*");
74 }
catch (MalformedMimeTypeException e) {
75 throw new RuntimeException(
"IntentFilter.addDataType() failed");
78 filters[2] =
new IntentFilter();
79 filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
81 {
"android.nfc.tech.Ndef"},
82 {
"android.nfc.tech.NdefFormatable"}
85 m_adapter.enableForegroundDispatch(m_activity, m_pendingIntent, filters, techList);
86 }
catch(IllegalStateException e) {
88 Log.d(
TAG,
"enableForegroundDispatch failed: " + e.toString());
95 static boolean stopDiscovery()
97 if (m_adapter ==
null || m_activity ==
null
98 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
101 m_activity.runOnUiThread(
new Runnable() {
105 m_adapter.disableForegroundDispatch(m_activity);
106 }
catch(IllegalStateException e) {
108 Log.d(
TAG,
"disableForegroundDispatch failed: " + e.toString());
115 static boolean isEnabled()
117 if (m_adapter ==
null) {
121 return m_adapter.isEnabled();
124 static boolean isSupported()
126 return (m_adapter !=
null);
129 static Intent getStartIntent()
131 Log.d(
TAG,
"getStartIntent");
132 if (m_activity ==
null)
return null;
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())) {
144 @SuppressWarnings(
"deprecation")
145 static Parcelable getTag(Intent intent)
147 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
148 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG,
Tag.class);
150 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
static const QString context()