From b42f7623ee75b651f6e756a73d0f8f385f337220 Mon Sep 17 00:00:00 2001 From: Lars Schmertmann Date: Thu, 26 Jan 2017 15:02:37 +0100 Subject: Allow using nfc when running as a service With this change it will be possible to use a tag injected from outside when running as a service. Intent newIntent = new Intent(); newIntent.putExtra(NfcAdapter.EXTRA_TAG, tag); QtNative.onNewIntent(newIntent); Task-number: QTBUG-57646 Change-Id: I628d4357f023a0926e7d61914b39278342ac7161 --- .../src/org/qtproject/qt5/android/nfc/QtNfc.java | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git x/qtconnectivity/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java y/qtconnectivity/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java index 47dcf1bf..25c560f8 100644 --- x/qtconnectivity/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java +++ y/qtconnectivity/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java @@ -62,22 +62,25 @@ public class QtNfc static public NfcAdapter m_adapter = null; static public PendingIntent m_pendingIntent = null; static public IntentFilter[] m_filters; - static public Activity m_activity; + static public Context m_context = null; + static public Activity m_activity = null; static public void setContext(Context context) { - if (!(context instanceof Activity)) { - Log.w(TAG, "NFC only works with Android activities and not in Android services. " + - "NFC has been disabled."); + m_context = context; + if (context instanceof Activity) m_activity = (Activity) context; + m_adapter = NfcAdapter.getDefaultAdapter(context); + + if (m_activity == null) { + Log.w(TAG, "New NFC tags will only be recognized with Android activities and not with Android services."); return; } - m_activity = (Activity)context; - m_adapter = NfcAdapter.getDefaultAdapter(m_activity); if (m_adapter == null) { //Log.e(TAG, "No NFC available"); return; } + m_pendingIntent = PendingIntent.getActivity( m_activity, 0, @@ -103,7 +106,8 @@ public class QtNfc static public boolean start() { - if (m_adapter == null) return false; + if (m_adapter == null || m_activity == null) return false; + m_activity.runOnUiThread(new Runnable() { public void run() { //Log.d(TAG, "Enabling NFC"); @@ -136,7 +140,8 @@ public class QtNfc static public boolean stop() { - if (m_adapter == null) return false; + if (m_adapter == null || m_activity == null) return false; + m_activity.runOnUiThread(new Runnable() { public void run() { //Log.d(TAG, "Disabling NFC"); @@ -153,11 +158,11 @@ public class QtNfc static public boolean isAvailable() { - m_adapter = NfcAdapter.getDefaultAdapter(m_activity); if (m_adapter == null) { //Log.e(TAG, "No NFC available (Adapter is null)"); return false; } + return m_adapter.isEnabled(); } @@ -165,6 +170,7 @@ public class QtNfc { Log.d(TAG, "getStartIntent"); if (m_activity == null) return null; + Intent intent = m_activity.getIntent(); if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) || NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction()) || -- 2.11.0