Skip to content

Commit

Permalink
show toast when rower could not be reset
Browse files Browse the repository at this point in the history
  • Loading branch information
svenmeier committed Jan 17, 2021
1 parent ada8a9e commit 4e8d0ae
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class BluetoothRower extends Rower {

private static final int CONNECT_TIMEOUT_MILLIS = 10000;

private static final int RESET_TIMEOUT_MILLIS = 3000;

/**
* Timeout after which we re-enable notifications.
*/
Expand Down Expand Up @@ -120,6 +122,10 @@ private void pop() {
this.connections.pop().close();
}

private boolean isCurrent(Connection connection) {
return this.connections.peek() == connection;
}

private interface Connection {

void open();
Expand Down Expand Up @@ -201,7 +207,7 @@ private boolean isEnabled() {

@Override
public void onReceive(Context context, Intent intent) {
if (connections.peek() == this && isEnabled()) {
if (isCurrent(this) && isEnabled()) {
proceed();
}
}
Expand Down Expand Up @@ -253,7 +259,7 @@ public void open() {
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
if (state == BluetoothAdapter.STATE_ON) {
if (connections.peek() == this) {
if (isCurrent( this)) {
proceed();
}
} else if (state == BluetoothAdapter.STATE_OFF) {
Expand Down Expand Up @@ -296,7 +302,7 @@ public void open() {

@Override
public final void onReceive(Context context, Intent intent) {
if (connections.peek() == this) {
if (isCurrent( this)) {
String address = intent.getStringExtra(BluetoothActivity.DEVICE_ADDRESS);

if (address == null) {
Expand Down Expand Up @@ -330,10 +336,14 @@ public void close() {
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private class GattConnection extends BlueWriter implements Connection, Runnable {
private class GattConnection extends BlueWriter implements Connection {

private final String address;

private ConnectionTimeout connectionTimeout = new ConnectionTimeout();

private ResetTimeout resetTimeout = new ResetTimeout();

private KeepAlive keepAlive = new KeepAlive();

private BluetoothAdapter adapter;
Expand All @@ -357,7 +367,7 @@ private class GattConnection extends BlueWriter implements Connection, Runnable
private void select() {
devicePreference.set(null);

handler.removeCallbacks(this);
handler.removeCallbacks(connectionTimeout);

pop(); // this
pop(); // previous selection
Expand All @@ -382,7 +392,7 @@ public synchronized void open() {
connected = device.connectGatt(context, false, this);
}

handler.postDelayed(this, CONNECT_TIMEOUT_MILLIS);
handler.postDelayed(connectionTimeout, CONNECT_TIMEOUT_MILLIS);
} catch (IllegalArgumentException invalid) {
select();
}
Expand Down Expand Up @@ -437,6 +447,9 @@ public void reset() {
if (connected != null && controlPoint != null) {
trace.onOutput("control-point resetting");
write(connected, controlPoint, OP_CODE_RESET);

handler.removeCallbacks(resetTimeout);
handler.postDelayed(resetTimeout, RESET_TIMEOUT_MILLIS);
}
}

Expand Down Expand Up @@ -652,14 +665,27 @@ public synchronized void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGa
/**
* @see BluetoothRower#CONNECT_TIMEOUT_MILLIS
*/
@Override
public void run() {
if (connected != null && rowerData == null) {
trace.comment("connection timeout");
private class ConnectionTimeout implements Runnable {
@Override
public void run() {
if (isCurrent( GattConnection.this) && rowerData == null) {
trace.comment("connection timeout");

toast(context.getString(R.string.bluetooth_rower_failed, connected.getDevice().getAddress()));
toast(context.getString(R.string.bluetooth_rower_failed, connected.getDevice().getAddress()));

select();
select();
}
}
}

private class ResetTimeout implements Runnable {
@Override
public void run() {
if (isCurrent(GattConnection.this) && resetting == true) {
trace.comment("reset timeout");

toast(context.getString(R.string.bluetooth_rower_reset_timeout));
}
}
}

Expand All @@ -685,7 +711,7 @@ public void onNotification() {
*/
@Override
public void run() {
if (rowerData != null) {
if (isCurrent(GattConnection.this) && rowerData != null) {
// re-enable notification
trace.comment("rower-data reenable notification");
enableNotification(connected, rowerData);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<string name="bluetooth_rower_not_found">Bluetooth kein Ruderer auf %s gefunden </string>
<string name="bluetooth_rower_disconnected">Bluetooth Ruderer Verbindung zu %s verloren</string>
<string name="bluetooth_rower_battery_level">Bluetooth Ruderer Batteriestand &lt; %d%%</string>
<string name="bluetooth_rower_reset_timeout">Bitte den Ruderer zurücksetzen</string>

<string name="preference_training_title">Training</string>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
<string name="bluetooth_rower_not_found">Bluetooth no rower found on %s</string>
<string name="bluetooth_rower_disconnected">Bluetooth rower connection lost to %s</string>
<string name="bluetooth_rower_battery_level">Bluetooth rower battery level &lt; %d%%</string>
<string name="bluetooth_rower_reset_timeout">Please reset your rower</string>

<string name="preference_training_title">Training</string>

Expand Down
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
8.2
- improved reset detection
- show toast when rower could not be reset

8.1
- use new gatt api where available
Expand Down

0 comments on commit 4e8d0ae

Please sign in to comment.