Removed handler between BluetoothConnectionManager and -RemoteSensorManager.

Toast messages should rather be sent from activities.
This commit is contained in:
Dennis Guse
2020-04-17 22:48:12 +02:00
parent b7b7bdf19b
commit bbe182cf82
3 changed files with 39 additions and 59 deletions
@@ -426,7 +426,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
}
/**
* Tries to fetch most recent {@link SensorDataSet} {@link de.dennisguse.opentracks.services.sensors.BluetoothRemoteSensorManager}.
* Tries to fetch most recent {@link SensorDataSet} from {@link de.dennisguse.opentracks.services.TrackRecordingService}.
*/
private void updateSensorDataUI() {
TrackRecordingServiceInterface trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
@@ -24,7 +24,6 @@ import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import androidx.annotation.NonNull;
@@ -40,19 +39,13 @@ import de.dennisguse.opentracks.util.BluetoothUtils;
*/
public class BluetoothConnectionManager {
// Message types sent to handler
static final int MESSAGE_CONNECTING = 1;
static final int MESSAGE_CONNECTED = 2;
static final int MESSAGE_READ = 3;
static final int MESSAGE_DISCONNECTED = 4;
private static final UUID HEART_RATE_MEASUREMENT_CHAR_UUID = new UUID(0x2A3700001000L, 0x800000805f9b34fbL);
private static final UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = new UUID(0x290200001000L, 0x800000805f9b34fbL);
private static final String TAG = BluetoothConnectionManager.class.getSimpleName();
private final Context context;
private final Handler handler;
private final EventCallback observer;
private SensorState sensorState;
@@ -67,14 +60,14 @@ public class BluetoothConnectionManager {
Log.d(TAG, "Connecting to sensor: " + gatt.getDevice());
setState(SensorState.CONNECTING);
handler.obtainMessage(MESSAGE_CONNECTING, gatt.getDevice().getName()).sendToTarget();
observer.connecting(gatt.getDevice().getName());
case BluetoothProfile.STATE_CONNECTED:
Log.d(TAG, "Connected to sensor: " + gatt.getDevice());
setState(SensorState.CONNECTED);
gatt.discoverServices();
handler.obtainMessage(MESSAGE_CONNECTED, gatt.getDevice().getName()).sendToTarget();
observer.connected(gatt.getDevice().getName());
break;
case BluetoothProfile.STATE_DISCONNECTING:
Log.d(TAG, "Disconnecting from sensor: " + gatt.getDevice());
@@ -84,7 +77,7 @@ public class BluetoothConnectionManager {
Log.d(TAG, "Disconnected from sensor: " + gatt.getDevice());
setState(SensorState.DISCONNECTED);
handler.obtainMessage(MESSAGE_DISCONNECTED, gatt.getDevice().getName()).sendToTarget();
observer.disconnected(gatt.getDevice().getName());
}
}
@@ -115,21 +108,15 @@ public class BluetoothConnectionManager {
String deviceName = gatt.getDevice().getName();
Log.d(TAG, "Received heart beat rate " + deviceName + ": " + heartRate);
SensorDataSet sensorDataSet = new SensorDataSet(heartRate, deviceName, gatt.getDevice().getAddress());
handler.obtainMessage(MESSAGE_READ, sensorDataSet).sendToTarget();
observer.onSensorDataReceived(new SensorDataSet(heartRate, deviceName, gatt.getDevice().getAddress()));
}
};
/**
* Constructor.
*
* @param handler a handler for sending messages back to the UI activity
*/
BluetoothConnectionManager(@NonNull Context context, @NonNull BluetoothDevice bluetoothDevice, @NonNull Handler handler) {
BluetoothConnectionManager(@NonNull Context context, @NonNull BluetoothDevice bluetoothDevice, EventCallback observer) {
this.context = context;
this.bluetoothDevice = bluetoothDevice;
this.handler = handler;
this.sensorState = SensorState.NONE;
this.observer = observer;
}
public synchronized void connect() {
@@ -164,4 +151,16 @@ public class BluetoothConnectionManager {
private synchronized void setState(SensorState sensorState) {
this.sensorState = sensorState;
}
interface EventCallback {
void connecting(String sensorName);
void connected(String sensorName);
void onSensorDataReceived(SensorDataSet sensorDataSet);
void disconnected(String sensorName);
}
}
@@ -20,9 +20,6 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
@@ -37,7 +34,7 @@ import de.dennisguse.opentracks.util.PreferencesUtils;
*
* @author Sandor Dornbush
*/
public class BluetoothRemoteSensorManager {
public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.EventCallback {
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5000;
@@ -49,39 +46,6 @@ public class BluetoothRemoteSensorManager {
private final SharedPreferences sharedPreferences;
// Handler that gets information back from the bluetoothConnectionManager
private final Handler messageHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
String toastMessage;
switch (message.what) {
case BluetoothConnectionManager.MESSAGE_CONNECTING:
//Ignore for now.
toastMessage = context.getString(R.string.settings_sensor_connecting, message.obj);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
break;
case BluetoothConnectionManager.MESSAGE_CONNECTED:
toastMessage = context.getString(R.string.settings_sensor_connected, message.obj);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
break;
case BluetoothConnectionManager.MESSAGE_READ:
if (!(message.obj instanceof SensorDataSet)) {
Log.e(TAG, "Received message did not contain a SensorDataSet.");
sensorDataSet = null;
} else {
sensorDataSet = (SensorDataSet) message.obj;
}
break;
case BluetoothConnectionManager.MESSAGE_DISCONNECTED:
toastMessage = context.getString(R.string.settings_sensor_disconnected, message.obj);
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
break;
default:
Log.e(TAG, "Got an undefined case. Please check.");
break;
}
}
};
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
@@ -166,7 +130,7 @@ public class BluetoothRemoteSensorManager {
disconnect();
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, messageHandler);
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, this);
bluetoothConnectionManager.connect();
}
@@ -176,4 +140,21 @@ public class BluetoothRemoteSensorManager {
bluetoothConnectionManager = null;
}
}
@Override
public void connecting(String sensorName) {
}
@Override
public void connected(String sensorName) {
}
@Override
public void onSensorDataReceived(SensorDataSet sensorDataSet) {
this.sensorDataSet = sensorDataSet;
}
@Override
public void disconnected(String sensorName) {
}
}