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 Activity m_activity =
null;
31 m_adapter = NfcAdapter.getDefaultAdapter(
context);
33 if (m_activity ==
null) {
34 Log.w(
TAG,
"New NFC tags will only be recognized with Android activities and not with Android services.");
38 if (m_adapter ==
null) {
45 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
47 m_pendingIntent = PendingIntent.getActivity(
50 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
54 static boolean startDiscovery()
56 if (m_adapter ==
null || m_activity ==
null
57 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
60 m_activity.runOnUiThread(
new Runnable() {
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);
71 filters[1].addDataType(
"*/*");
72 }
catch (MalformedMimeTypeException e) {
73 throw new RuntimeException(
"IntentFilter.addDataType() failed");
76 filters[2] =
new IntentFilter();
77 filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED);
79 {
"android.nfc.tech.Ndef"},
80 {
"android.nfc.tech.NdefFormatable"}
83 m_adapter.enableForegroundDispatch(m_activity, m_pendingIntent, filters, techList);
84 }
catch(IllegalStateException e) {
86 Log.d(
TAG,
"enableForegroundDispatch failed: " + e.toString());
93 static boolean stopDiscovery()
95 if (m_adapter ==
null || m_activity ==
null
96 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
99 m_activity.runOnUiThread(
new Runnable() {
103 m_adapter.disableForegroundDispatch(m_activity);
104 }
catch(IllegalStateException e) {
106 Log.d(
TAG,
"disableForegroundDispatch failed: " + e.toString());
113 static boolean isEnabled()
115 if (m_adapter ==
null) {
119 return m_adapter.isEnabled();
122 static boolean isSupported()
124 return (m_adapter !=
null);
127 static Intent getStartIntent()
129 Log.d(
TAG,
"getStartIntent");
130 if (m_activity ==
null)
return null;
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())) {
142 @SuppressWarnings(
"deprecation")
143 static Parcelable getTag(Intent intent)
145 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
146 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG,
Tag.class);
148 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
static const QString context()