forked from upstream-mirrors/OpenTracks
Simplified getting the BluetoothAdapter.
This commit is contained in:
+1
-1
@@ -99,5 +99,5 @@ dependencies {
|
|||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
androidTestImplementation 'androidx.test:rules:1.2.0'
|
androidTestImplementation 'androidx.test:rules:1.2.0'
|
||||||
androidTestImplementation 'androidx.test:runner:1.2.0'
|
androidTestImplementation 'androidx.test:runner:1.2.0'
|
||||||
androidTestImplementation 'org.mockito:mockito-android:3.4.4'
|
androidTestImplementation 'org.mockito:mockito-android:3.4.6'
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-6
@@ -50,8 +50,7 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
|
|||||||
|
|
||||||
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5 * UnitConversions.S_TO_MS;
|
public static final long MAX_SENSOR_DATE_SET_AGE_MS = 5 * UnitConversions.S_TO_MS;
|
||||||
|
|
||||||
private static final BluetoothAdapter bluetoothAdapter = BluetoothUtils.getDefaultBluetoothAdapter(TAG);
|
private final BluetoothAdapter bluetoothAdapter;
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final SharedPreferences sharedPreferences;
|
private final SharedPreferences sharedPreferences;
|
||||||
|
|
||||||
@@ -86,12 +85,10 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @param context the context
|
|
||||||
*/
|
|
||||||
public BluetoothRemoteSensorManager(Context context) {
|
public BluetoothRemoteSensorManager(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
sharedPreferences = PreferencesUtils.getSharedPreferences(context);
|
sharedPreferences = PreferencesUtils.getSharedPreferences(context);
|
||||||
|
bluetoothAdapter = BluetoothUtils.getAdapter(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -103,7 +100,6 @@ public class BluetoothRemoteSensorManager implements BluetoothConnectionManager.
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void stop() {
|
public synchronized void stop() {
|
||||||
// Disconnecting
|
|
||||||
heartRate.disconnect();
|
heartRate.disconnect();
|
||||||
cyclingCadence.disconnect();
|
cyclingCadence.disconnect();
|
||||||
|
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ public class SettingsActivity extends AppCompatActivity implements ChooseActivit
|
|||||||
private void updateBluetooth() {
|
private void updateBluetooth() {
|
||||||
// Disable Bluetooth preference if device does not have Bluetooth
|
// Disable Bluetooth preference if device does not have Bluetooth
|
||||||
BluetoothLeSensorPreference bluetoothPreference = findPreference(getString(R.string.settings_sensor_bluetooth_heart_rate_key));
|
BluetoothLeSensorPreference bluetoothPreference = findPreference(getString(R.string.settings_sensor_bluetooth_heart_rate_key));
|
||||||
bluetoothPreference.setVisible(BluetoothUtils.hasBluetooth(TAG));
|
bluetoothPreference.setVisible(BluetoothUtils.hasBluetooth(getContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUnits() {
|
private void updateUnits() {
|
||||||
|
|||||||
+1
-1
@@ -136,7 +136,7 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
|
|||||||
UUID sensorUUID = (UUID) getArguments().getSerializable(ARG_BLUETOOTH_UUID);
|
UUID sensorUUID = (UUID) getArguments().getSerializable(ARG_BLUETOOTH_UUID);
|
||||||
Log.i(TAG, "UUID: " + sensorUUID);
|
Log.i(TAG, "UUID: " + sensorUUID);
|
||||||
|
|
||||||
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getDefaultBluetoothAdapter(TAG);
|
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext());
|
||||||
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
|
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
|
||||||
Log.w(TAG, "Bluetooth adapter is present or not enabled.");
|
Log.w(TAG, "Bluetooth adapter is present or not enabled.");
|
||||||
Toast.makeText(getContext(), R.string.bluetooth_disabled, Toast.LENGTH_SHORT).show();
|
Toast.makeText(getContext(), R.string.bluetooth_disabled, Toast.LENGTH_SHORT).show();
|
||||||
|
|||||||
@@ -17,13 +17,12 @@ package de.dennisguse.opentracks.util;
|
|||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothGattCharacteristic;
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
import android.os.Handler;
|
import android.bluetooth.BluetoothManager;
|
||||||
import android.os.Looper;
|
import android.content.Context;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.content.sensor.SensorDataCycling;
|
import de.dennisguse.opentracks.content.sensor.SensorDataCycling;
|
||||||
@@ -43,49 +42,23 @@ public class BluetoothUtils {
|
|||||||
public static final UUID CYCLING_SPEED_CADENCE_SERVICE_UUID = new UUID(0x181600001000L, 0x800000805f9b34fbL);
|
public static final UUID CYCLING_SPEED_CADENCE_SERVICE_UUID = new UUID(0x181600001000L, 0x800000805f9b34fbL);
|
||||||
public static final UUID CYCLING_SPPED_CADENCE_MEASUREMENT_CHAR_UUID = new UUID(0x2A5B00001000L, 0x800000805f9b34fbL);
|
public static final UUID CYCLING_SPPED_CADENCE_MEASUREMENT_CHAR_UUID = new UUID(0x2A5B00001000L, 0x800000805f9b34fbL);
|
||||||
|
|
||||||
|
private static final String TAG = BluetoothUtils.class.getSimpleName();
|
||||||
|
|
||||||
private BluetoothUtils() {
|
private BluetoothUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public static BluetoothAdapter getAdapter(Context context) {
|
||||||
* If called from UI: use a background thread to get the default Bluetooth adapter.
|
BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||||
* TODO Check if this is necessary.
|
if (bluetoothManager == null) {
|
||||||
*/
|
Log.i(TAG, "BluetoothManager not available.");
|
||||||
public static BluetoothAdapter getDefaultBluetoothAdapter(final String TAG) {
|
return null;
|
||||||
// If from the main application thread, return directly
|
} else {
|
||||||
if (Thread.currentThread().equals(Looper.getMainLooper().getThread())) {
|
return bluetoothManager.getAdapter();
|
||||||
return BluetoothAdapter.getDefaultAdapter();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the default adapter from the main application thread
|
|
||||||
final ArrayList<BluetoothAdapter> adapters = new ArrayList<>(1);
|
|
||||||
final Object mutex = new Object();
|
|
||||||
|
|
||||||
Handler handler = new Handler(Looper.getMainLooper());
|
|
||||||
handler.post(() -> {
|
|
||||||
adapters.add(BluetoothAdapter.getDefaultAdapter());
|
|
||||||
synchronized (mutex) {
|
|
||||||
mutex.notify();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
while (adapters.isEmpty()) {
|
|
||||||
synchronized (mutex) {
|
|
||||||
try {
|
|
||||||
mutex.wait(UnitConversions.ONE_SECOND_MS);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
Log.e(TAG, "Interrupted while waiting for default bluetooth adapter", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (adapters.get(0) == null) {
|
|
||||||
Log.w(TAG, "No bluetooth adapter found.");
|
|
||||||
}
|
|
||||||
return adapters.get(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean hasBluetooth(final String TAG) {
|
public static boolean hasBluetooth(Context context) {
|
||||||
return BluetoothUtils.getDefaultBluetoothAdapter(TAG) != null;
|
return BluetoothUtils.getAdapter(context) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Integer parseHeartRate(BluetoothGattCharacteristic characteristic) {
|
public static Integer parseHeartRate(BluetoothGattCharacteristic characteristic) {
|
||||||
|
|||||||
Reference in New Issue
Block a user