Wait for an incoming connection before showing an error Toast

This commit is contained in:
Torsten Grote
2021-04-30 16:01:54 -03:00
parent 683af1ec3a
commit 552b9ef21a

View File

@@ -217,22 +217,20 @@ class BluetoothConnecter implements EventListener {
DuplexTransportConnection conn = bluetoothPlugin
.discoverAndConnectForSetup(uuid);
if (conn == null) {
if (!isConnectedViaBluetooth(contactId)) {
LOG.warning("Failed to connect");
showToast(R.string.toast_connect_via_bluetooth_error);
} else {
LOG.info("Failed to connect, but contact connected");
}
return;
waitAfterConnectionFailed();
} else {
LOG.info("Could connect, handling connection");
connectionManager
.manageOutgoingConnection(contactId, ID, conn);
showToast(R.string.toast_connect_via_bluetooth_success);
}
connectionManager.manageOutgoingConnection(contactId, ID, conn);
showToast(R.string.toast_connect_via_bluetooth_success);
} finally {
eventBus.removeListener(this);
}
});
}
@IoExecutor
private boolean waitForBluetoothActive() {
long left = BT_ACTIVE_TIMEOUT;
final long sleep = 250;
@@ -250,6 +248,31 @@ class BluetoothConnecter implements EventListener {
return (bluetoothPlugin.getState() == ACTIVE);
}
/**
* Wait for an incoming connection before showing an error Toast.
*/
@IoExecutor
private void waitAfterConnectionFailed() {
long left = BT_ACTIVE_TIMEOUT;
final long sleep = 250;
try {
while (left > 0) {
if (isConnectedViaBluetooth(contactId)) {
LOG.info("Failed to connect, but contact connected");
// no Toast needed here, as it gets shown when
// ConnectionOpenedEvent is received
return;
}
Thread.sleep(sleep);
left -= sleep;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
LOG.warning("Failed to connect");
showToast(R.string.toast_connect_via_bluetooth_error);
}
private void showToast(@StringRes int res) {
androidExecutor.runOnUiThread(() ->
Toast.makeText(app, res, Toast.LENGTH_LONG).show()