Skip to content

Commit

Permalink
Try to catch more weird connect errors on some devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
ikarus23 committed Oct 29, 2023
1 parent 6a57189 commit ad4aee4
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1176,31 +1176,37 @@ public boolean isConnectedButTagLost() {
public void connect() throws Exception {
final AtomicBoolean error = new AtomicBoolean(false);

if (mMFC == null || mMFC.getTag() == null) {
error.set(true);
}

// Do not connect if already connected.
if (isConnected()) {
if (error.get() == false && isConnected()) {
return;
}

// Connect in a worker thread. (connect() might be blocking).
Thread t = new Thread(() -> {
if (error.get() == false) {
Thread t = new Thread(() -> {
try {
mMFC.connect();
} catch (IOException | IllegalStateException | SecurityException ex) {
// The SecurityException is thrown when an old stored Tag object is used.
// It would be best to change the workflow/logic of this app.
// https://stackoverflow.com/questions/75695662/securityexception-while-ndef-connect-on-android-13
// https://issuetracker.google.com/issues/272552420
// https://github.com/status-im/status-mobile/issues/14815
error.set(true);
}
});
t.start();

// Wait for the connection (max 500millis).
try {
mMFC.connect();
} catch (IOException | IllegalStateException | SecurityException ex) {
// The SecurityException is thrown when an old stored Tag object is used.
// It would be best to change the workflow/logic of this app.
// https://stackoverflow.com/questions/75695662/securityexception-while-ndef-connect-on-android-13
// https://issuetracker.google.com/issues/272552420
// https://github.com/status-im/status-mobile/issues/14815
t.join(500);
} catch (InterruptedException ex) {
error.set(true);
}
});
t.start();

// Wait for the connection (max 500millis).
try {
t.join(500);
} catch (InterruptedException ex) {
error.set(true);
}

// If there was an error log it and throw an exception.
Expand All @@ -1217,7 +1223,8 @@ public void close() {
try {
mMFC.close();
}
catch (IOException e) {
catch (IOException | SecurityException ex) {
// See connect()
Log.d(LOG_TAG, "Error on closing tag.");
}
}
Expand Down

0 comments on commit ad4aee4

Please sign in to comment.