4package org.qtproject.qt.android.nfc;
6import java.lang.Runnable;
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;
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;
32 m_adapter = NfcAdapter.getDefaultAdapter(
context);
34 if (m_activity ==
null) {
35 Log.w(
TAG,
"New NFC tags will only be recognized with Android activities and not with Android services.");
39 if (m_adapter ==
null) {
46 int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE
48 m_pendingIntent = PendingIntent.getActivity(
51 new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
57 if (m_adapter ==
null || m_activity ==
null
58 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
61 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);
72 }
catch (MalformedMimeTypeException
e) {
73 throw new RuntimeException(
"IntentFilter.addDataType() failed");
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"}
83 m_adapter.enableForegroundDispatch(m_activity, m_pendingIntent,
filters, techList);
84 }
catch(IllegalStateException
e) {
86 Log.d(
TAG,
"enableForegroundDispatch failed: " +
e.toString());
95 if (m_adapter ==
null || m_activity ==
null
96 || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC))
99 m_activity.runOnUiThread(
new Runnable() {
102 m_adapter.disableForegroundDispatch(m_activity);
103 }
catch(IllegalStateException
e) {
105 Log.d(
TAG,
"disableForegroundDispatch failed: " +
e.toString());
114 if (m_adapter ==
null) {
118 return m_adapter.isEnabled();
123 return (m_adapter !=
null);
128 Log.d(
TAG,
"getStartIntent");
129 if (m_activity ==
null)
return null;
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())) {
141 static public Parcelable
getTag(Intent intent)
143 return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
static boolean stopDiscovery()
static boolean isSupported()
static void setContext(Context context)
static boolean isEnabled()
static boolean startDiscovery()
static Intent getStartIntent()
static Parcelable getTag(Intent intent)
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]