diff options
Diffstat (limited to 'src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java')
-rw-r--r-- | src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java b/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java index 47dcf1bf..345b87d3 100644 --- a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java +++ b/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, @@ -86,7 +89,7 @@ public class QtNfc //Log.d(TAG, "Pending intent:" + m_pendingIntent); - IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED); + IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); m_filters = new IntentFilter[]{ filter @@ -103,22 +106,26 @@ 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"); - IntentFilter[] filters = new IntentFilter[2]; + IntentFilter[] filters = new IntentFilter[3]; filters[0] = new IntentFilter(); - filters[0].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); + filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED); filters[0].addCategory(Intent.CATEGORY_DEFAULT); + filters[1] = new IntentFilter(); + filters[1].addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); + filters[1].addCategory(Intent.CATEGORY_DEFAULT); try { - filters[0].addDataType("*/*"); + filters[1].addDataType("*/*"); } catch (MalformedMimeTypeException e) { throw new RuntimeException("Check your mime type."); } // some tags will report as tech, even if they are ndef formated/formatable. - filters[1] = new IntentFilter(); - filters[1].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); + filters[2] = new IntentFilter(); + filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); String[][] techList = new String[][]{ {"android.nfc.tech.Ndef"}, {"android.nfc.tech.NdefFormatable"} @@ -136,7 +143,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 +161,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 +173,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()) || |