forked from upstream-mirrors/OpenTracks
Removed handler between BluetoothConnectionManager and -RemoteSensorManager.
Toast messages should rather be sent from activities.
This commit is contained in:
@@ -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() {
|
private void updateSensorDataUI() {
|
||||||
TrackRecordingServiceInterface trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
|
TrackRecordingServiceInterface trackRecordingService = trackRecordingServiceConnection.getServiceIfBound();
|
||||||
|
|||||||
+19
-20
@@ -24,7 +24,6 @@ import android.bluetooth.BluetoothGattDescriptor;
|
|||||||
import android.bluetooth.BluetoothGattService;
|
import android.bluetooth.BluetoothGattService;
|
||||||
import android.bluetooth.BluetoothProfile;
|
import android.bluetooth.BluetoothProfile;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Handler;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
@@ -40,19 +39,13 @@ import de.dennisguse.opentracks.util.BluetoothUtils;
|
|||||||
*/
|
*/
|
||||||
public class BluetoothConnectionManager {
|
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 HEART_RATE_MEASUREMENT_CHAR_UUID = new UUID(0x2A3700001000L, 0x800000805f9b34fbL);
|
||||||
private static final UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = new UUID(0x290200001000L, 0x800000805f9b34fbL);
|
private static final UUID CLIENT_CHARACTERISTIC_CONFIG_UUID = new UUID(0x290200001000L, 0x800000805f9b34fbL);
|
||||||
|
|
||||||
private static final String TAG = BluetoothConnectionManager.class.getSimpleName();
|
private static final String TAG = BluetoothConnectionManager.class.getSimpleName();
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final Handler handler;
|
private final EventCallback observer;
|
||||||
|
|
||||||
private SensorState sensorState;
|
private SensorState sensorState;
|
||||||
|
|
||||||
@@ -67,14 +60,14 @@ public class BluetoothConnectionManager {
|
|||||||
Log.d(TAG, "Connecting to sensor: " + gatt.getDevice());
|
Log.d(TAG, "Connecting to sensor: " + gatt.getDevice());
|
||||||
setState(SensorState.CONNECTING);
|
setState(SensorState.CONNECTING);
|
||||||
|
|
||||||
handler.obtainMessage(MESSAGE_CONNECTING, gatt.getDevice().getName()).sendToTarget();
|
observer.connecting(gatt.getDevice().getName());
|
||||||
case BluetoothProfile.STATE_CONNECTED:
|
case BluetoothProfile.STATE_CONNECTED:
|
||||||
Log.d(TAG, "Connected to sensor: " + gatt.getDevice());
|
Log.d(TAG, "Connected to sensor: " + gatt.getDevice());
|
||||||
setState(SensorState.CONNECTED);
|
setState(SensorState.CONNECTED);
|
||||||
|
|
||||||
gatt.discoverServices();
|
gatt.discoverServices();
|
||||||
|
|
||||||
handler.obtainMessage(MESSAGE_CONNECTED, gatt.getDevice().getName()).sendToTarget();
|
observer.connected(gatt.getDevice().getName());
|
||||||
break;
|
break;
|
||||||
case BluetoothProfile.STATE_DISCONNECTING:
|
case BluetoothProfile.STATE_DISCONNECTING:
|
||||||
Log.d(TAG, "Disconnecting from sensor: " + gatt.getDevice());
|
Log.d(TAG, "Disconnecting from sensor: " + gatt.getDevice());
|
||||||
@@ -84,7 +77,7 @@ public class BluetoothConnectionManager {
|
|||||||
Log.d(TAG, "Disconnected from sensor: " + gatt.getDevice());
|
Log.d(TAG, "Disconnected from sensor: " + gatt.getDevice());
|
||||||
setState(SensorState.DISCONNECTED);
|
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();
|
String deviceName = gatt.getDevice().getName();
|
||||||
|
|
||||||
Log.d(TAG, "Received heart beat rate " + deviceName + ": " + heartRate);
|
Log.d(TAG, "Received heart beat rate " + deviceName + ": " + heartRate);
|
||||||
SensorDataSet sensorDataSet = new SensorDataSet(heartRate, deviceName, gatt.getDevice().getAddress());
|
observer.onSensorDataReceived(new SensorDataSet(heartRate, deviceName, gatt.getDevice().getAddress()));
|
||||||
handler.obtainMessage(MESSAGE_READ, sensorDataSet).sendToTarget();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
BluetoothConnectionManager(@NonNull Context context, @NonNull BluetoothDevice bluetoothDevice, EventCallback observer) {
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param handler a handler for sending messages back to the UI activity
|
|
||||||
*/
|
|
||||||
BluetoothConnectionManager(@NonNull Context context, @NonNull BluetoothDevice bluetoothDevice, @NonNull Handler handler) {
|
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.bluetoothDevice = bluetoothDevice;
|
this.bluetoothDevice = bluetoothDevice;
|
||||||
this.handler = handler;
|
|
||||||
this.sensorState = SensorState.NONE;
|
this.sensorState = SensorState.NONE;
|
||||||
|
this.observer = observer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void connect() {
|
public synchronized void connect() {
|
||||||
@@ -164,4 +151,16 @@ public class BluetoothConnectionManager {
|
|||||||
private synchronized void setState(SensorState sensorState) {
|
private synchronized void setState(SensorState sensorState) {
|
||||||
this.sensorState = sensorState;
|
this.sensorState = sensorState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
interface EventCallback {
|
||||||
|
|
||||||
|
void connecting(String sensorName);
|
||||||
|
|
||||||
|
void connected(String sensorName);
|
||||||
|
|
||||||
|
void onSensorDataReceived(SensorDataSet sensorDataSet);
|
||||||
|
|
||||||
|
void disconnected(String sensorName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-38
@@ -20,9 +20,6 @@ import android.bluetooth.BluetoothAdapter;
|
|||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.os.Message;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
@@ -37,7 +34,7 @@ import de.dennisguse.opentracks.util.PreferencesUtils;
|
|||||||
*
|
*
|
||||||
* @author Sandor Dornbush
|
* @author Sandor Dornbush
|
||||||
*/
|
*/
|
||||||
public class BluetoothRemoteSensorManager {
|
public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.EventCallback {
|
||||||
|
|
||||||
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5000;
|
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5000;
|
||||||
|
|
||||||
@@ -49,39 +46,6 @@ public class BluetoothRemoteSensorManager {
|
|||||||
|
|
||||||
private final SharedPreferences sharedPreferences;
|
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() {
|
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||||
@@ -166,7 +130,7 @@ public class BluetoothRemoteSensorManager {
|
|||||||
|
|
||||||
disconnect();
|
disconnect();
|
||||||
|
|
||||||
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, messageHandler);
|
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, this);
|
||||||
bluetoothConnectionManager.connect();
|
bluetoothConnectionManager.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,4 +140,21 @@ public class BluetoothRemoteSensorManager {
|
|||||||
bluetoothConnectionManager = null;
|
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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user