mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-11 13:33:07 +02:00
+5
-4
@@ -105,9 +105,10 @@ public class BluetoothConnectionManager {
|
|||||||
@Override
|
@Override
|
||||||
public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) {
|
public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) {
|
||||||
int heartRate = BluetoothUtils.parseHeartRate(characteristic);
|
int heartRate = BluetoothUtils.parseHeartRate(characteristic);
|
||||||
|
String deviceName = gatt.getDevice().getName();
|
||||||
|
|
||||||
Log.d(TAG, "Received heart beat rate: " + heartRate);
|
Log.d(TAG, "Received heart beat rate " + deviceName + ": " + heartRate);
|
||||||
SensorDataSet sensorDataSet = new SensorDataSet(heartRate, gatt.getDevice().getName(), gatt.getDevice().getAddress());
|
SensorDataSet sensorDataSet = new SensorDataSet(heartRate, deviceName, gatt.getDevice().getAddress());
|
||||||
handler.obtainMessage(MESSAGE_READ, sensorDataSet).sendToTarget();
|
handler.obtainMessage(MESSAGE_READ, sensorDataSet).sendToTarget();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -144,8 +145,8 @@ public class BluetoothConnectionManager {
|
|||||||
bluetoothGatt = null;
|
bluetoothGatt = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isSameBluetoothDevice(BluetoothDevice bluetoothDevice) {
|
public synchronized boolean isSameBluetoothDevice(String address) {
|
||||||
return this.bluetoothDevice.equals(bluetoothDevice);
|
return this.bluetoothDevice.getAddress().equals(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized SensorState getSensorState() {
|
synchronized SensorState getSensorState() {
|
||||||
|
|||||||
+53
-19
@@ -19,6 +19,7 @@ package de.dennisguse.opentracks.services.sensors;
|
|||||||
import android.bluetooth.BluetoothAdapter;
|
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.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
@@ -47,6 +48,7 @@ public class BluetoothRemoteSensorManager {
|
|||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
|
private final SharedPreferences sharedPreferences;
|
||||||
// Handler that gets information back from the bluetoothConnectionManager
|
// Handler that gets information back from the bluetoothConnectionManager
|
||||||
private final Handler messageHandler = new Handler(Looper.getMainLooper()) {
|
private final Handler messageHandler = new Handler(Looper.getMainLooper()) {
|
||||||
@Override
|
@Override
|
||||||
@@ -55,6 +57,8 @@ public class BluetoothRemoteSensorManager {
|
|||||||
switch (message.what) {
|
switch (message.what) {
|
||||||
case BluetoothConnectionManager.MESSAGE_CONNECTING:
|
case BluetoothConnectionManager.MESSAGE_CONNECTING:
|
||||||
//Ignore for now.
|
//Ignore for now.
|
||||||
|
toastMessage = context.getString(R.string.settings_sensor_connecting, message.obj);
|
||||||
|
Toast.makeText(context, toastMessage, Toast.LENGTH_LONG).show();
|
||||||
break;
|
break;
|
||||||
case BluetoothConnectionManager.MESSAGE_CONNECTED:
|
case BluetoothConnectionManager.MESSAGE_CONNECTED:
|
||||||
toastMessage = context.getString(R.string.settings_sensor_connected, message.obj);
|
toastMessage = context.getString(R.string.settings_sensor_connected, message.obj);
|
||||||
@@ -78,6 +82,24 @@ public class BluetoothRemoteSensorManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
|
||||||
|
if (bluetoothConnectionManager != null && key == null || key.equals(PreferencesUtils.getKey(context, R.string.bluetooth_sensor_key))) {
|
||||||
|
String address = PreferencesUtils.getString(context, R.string.bluetooth_sensor_key, PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT);
|
||||||
|
if (address.equals(PreferencesUtils.BLUETOOTH_SENSOR_DEFAULT)) {
|
||||||
|
stop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (bluetoothConnectionManager.isSameBluetoothDevice(address)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnect();
|
||||||
|
startCurrentSensor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private SensorDataSet sensorDataSet = null;
|
private SensorDataSet sensorDataSet = null;
|
||||||
private BluetoothConnectionManager bluetoothConnectionManager;
|
private BluetoothConnectionManager bluetoothConnectionManager;
|
||||||
@@ -87,6 +109,7 @@ public class BluetoothRemoteSensorManager {
|
|||||||
*/
|
*/
|
||||||
BluetoothRemoteSensorManager(Context context) {
|
BluetoothRemoteSensorManager(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
sharedPreferences = PreferencesUtils.getSharedPreferences(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BluetoothAdapter getDefaultBluetoothAdapter() {
|
private static BluetoothAdapter getDefaultBluetoothAdapter() {
|
||||||
@@ -126,7 +149,34 @@ public class BluetoothRemoteSensorManager {
|
|||||||
return adapters.get(0);
|
return adapters.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startSensor() {
|
public void start() {
|
||||||
|
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
|
startCurrentSensor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
disconnect();
|
||||||
|
sharedPreferences.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
public SensorDataSet getSensorDataSet() {
|
||||||
|
return sensorDataSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSensorDataSetValid() {
|
||||||
|
SensorDataSet sensorDataSet = getSensorDataSet();
|
||||||
|
if (sensorDataSet == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return sensorDataSet.isRecent(MAX_SENSOR_DATE_SET_AGE_MS);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startCurrentSensor() {
|
||||||
if (!isEnabled()) {
|
if (!isEnabled()) {
|
||||||
Log.w(TAG, "Bluetooth not enabled.");
|
Log.w(TAG, "Bluetooth not enabled.");
|
||||||
return;
|
return;
|
||||||
@@ -151,32 +201,16 @@ public class BluetoothRemoteSensorManager {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
stopSensor();
|
disconnect();
|
||||||
|
|
||||||
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, messageHandler);
|
bluetoothConnectionManager = new BluetoothConnectionManager(context, device, messageHandler);
|
||||||
bluetoothConnectionManager.connect();
|
bluetoothConnectionManager.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopSensor() {
|
private void disconnect() {
|
||||||
if (bluetoothConnectionManager != null) {
|
if (bluetoothConnectionManager != null) {
|
||||||
bluetoothConnectionManager.disconnect();
|
bluetoothConnectionManager.disconnect();
|
||||||
bluetoothConnectionManager = null;
|
bluetoothConnectionManager = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
|
|
||||||
}
|
|
||||||
|
|
||||||
public SensorDataSet getSensorDataSet() {
|
|
||||||
return sensorDataSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSensorDataSetValid() {
|
|
||||||
SensorDataSet sensorDataSet = getSensorDataSet();
|
|
||||||
if (sensorDataSet == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return sensorDataSet.isRecent(MAX_SENSOR_DATE_SET_AGE_MS);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -39,7 +39,7 @@ public class RemoteSensorManagerFactory {
|
|||||||
public static BluetoothRemoteSensorManager getSystemSensorManager(Context context) {
|
public static BluetoothRemoteSensorManager getSystemSensorManager(Context context) {
|
||||||
releaseSystemSensorManager();
|
releaseSystemSensorManager();
|
||||||
remoteSensorManagerSystem = getSensorManager(context);
|
remoteSensorManagerSystem = getSensorManager(context);
|
||||||
remoteSensorManagerSystem.startSensor();
|
remoteSensorManagerSystem.start();
|
||||||
return remoteSensorManagerSystem;
|
return remoteSensorManagerSystem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ public class RemoteSensorManagerFactory {
|
|||||||
*/
|
*/
|
||||||
public static void releaseSystemSensorManager() {
|
public static void releaseSystemSensorManager() {
|
||||||
if (remoteSensorManagerSystem != null) {
|
if (remoteSensorManagerSystem != null) {
|
||||||
remoteSensorManagerSystem.stopSensor();
|
remoteSensorManagerSystem.stop();
|
||||||
}
|
}
|
||||||
remoteSensorManagerSystem = null;
|
remoteSensorManagerSystem = null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1273,12 +1273,16 @@ limitations under the License.
|
|||||||
Character limit: ~23 (1 line)">
|
Character limit: ~23 (1 line)">
|
||||||
Bluetooth sensor
|
Bluetooth sensor
|
||||||
</string>
|
</string>
|
||||||
<string name="settings_sensor_connected" translation_description="A message when a sensor is connected.
|
<string name="settings_sensor_connecting" translation_description="A message when a sensor is connecting.
|
||||||
|
|
||||||
%1$s sensor name. E.g., 'Polar Bluetooth'.">
|
%1$s sensor name. E.g., 'Polar Bluetooth'.">
|
||||||
Connected to %1$s
|
Connected to %1$s
|
||||||
</string>
|
</string>
|
||||||
<string name="settings_sensor_disconnected" translation_description="A message when a sensor is connected.
|
<string name="settings_sensor_connected" translation_description="A message when a sensor is connected.
|
||||||
|
%1$s sensor name. E.g., 'Polar Bluetooth'.">
|
||||||
|
Connected to %1$s
|
||||||
|
</string>
|
||||||
|
<string name="settings_sensor_disconnected" translation_description="A message when a sensor is disconnected.
|
||||||
|
|
||||||
%1$s sensor name. E.g., 'Polar Bluetooth'.">
|
%1$s sensor name. E.g., 'Polar Bluetooth'.">
|
||||||
Disconnected from %1$s
|
Disconnected from %1$s
|
||||||
|
|||||||
Reference in New Issue
Block a user