Skip to content

Commit

Permalink
NFC: catch exceptions, add background enable
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 16, 2024
1 parent e116f6a commit c5f69a4
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions app/src/main/java/com/eveningoutpost/dexdrip/nfc/NFControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private static int getReaderFlags() { // if 0 then nothing enabled
}

if (GlucoMen.isEnabled()) {
flags |= NfcAdapter.FLAG_READER_NFC_V
| NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
flags |= NfcAdapter.FLAG_READER_NFC_V
| NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
}

return flags;
Expand All @@ -58,7 +58,11 @@ private static Bundle getOptionsBundle() {
}
}

public static void initNFC(final Activity context, final boolean disable) {
public static void initNFCbackground(final Activity context, final boolean disable) {
new Thread(() -> NFControl.initNFC(context, disable)).start();
}

public static synchronized void initNFC(final Activity context, final boolean disable) {
UserError.Log.d(TAG, "InitNFC start");
val mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
val flags = disable ? 0 : getReaderFlags();
Expand Down Expand Up @@ -93,11 +97,19 @@ public static void initNFC(final Activity context, final boolean disable) {
}

UserError.Log.d(TAG, "Enabling reader mode with flags: " + flags);
mNfcAdapter.enableReaderMode(context, new TagMultiplexer(context), flags, getOptionsBundle());
try {
mNfcAdapter.enableReaderMode(context, new TagMultiplexer(context), flags, getOptionsBundle());
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception enabling reader mode: " + e);
}
} else {
if (mNfcAdapter != null) {
UserError.Log.d(TAG, "Disabling reader mode");
mNfcAdapter.disableReaderMode(context);
try {
mNfcAdapter.disableReaderMode(context);
} catch (Exception e) {
UserError.Log.e(TAG, "Got exception disabling reader mode: " + e);
}
}
}
}
Expand Down

0 comments on commit c5f69a4

Please sign in to comment.