From c59d1d87bd91d6251d1114b1f0f18088f6271e9a Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Thu, 14 Nov 2019 18:07:44 +0100 Subject: [PATCH] Bluetooth: move getting adapter to BluetoothUtils. --- .../sensors/BluetoothRemoteSensorManager.java | 43 +------------------ .../opentracks/util/BluetoothUtils.java | 41 ++++++++++++++++++ 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/services/sensors/BluetoothRemoteSensorManager.java b/src/main/java/de/dennisguse/opentracks/services/sensors/BluetoothRemoteSensorManager.java index aceef293a..dd452e3a3 100644 --- a/src/main/java/de/dennisguse/opentracks/services/sensors/BluetoothRemoteSensorManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/sensors/BluetoothRemoteSensorManager.java @@ -26,12 +26,10 @@ import android.os.Message; import android.util.Log; import android.widget.Toast; -import java.util.ArrayList; - import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.content.sensor.SensorDataSet; +import de.dennisguse.opentracks.util.BluetoothUtils; import de.dennisguse.opentracks.util.PreferencesUtils; -import de.dennisguse.opentracks.util.UnitConversions; /** * Bluetooth LE sensor manager. @@ -45,7 +43,7 @@ public class BluetoothRemoteSensorManager { private static final String TAG = BluetoothConnectionManager.class.getSimpleName(); - private static final BluetoothAdapter bluetoothAdapter = getDefaultBluetoothAdapter(); + private static final BluetoothAdapter bluetoothAdapter = BluetoothUtils.getDefaultBluetoothAdapter(TAG); private final Context context; @@ -114,43 +112,6 @@ public class BluetoothRemoteSensorManager { sharedPreferences = PreferencesUtils.getSharedPreferences(context); } - private static BluetoothAdapter getDefaultBluetoothAdapter() { - // If from the main application thread, return directly - if (Thread.currentThread().equals(Looper.getMainLooper().getThread())) { - return BluetoothAdapter.getDefaultAdapter(); - } - - // Get the default adapter from the main application thread - final ArrayList adapters = new ArrayList<>(1); - final Object mutex = new Object(); - - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - @Override - public void run() { - adapters.add(BluetoothAdapter.getDefaultAdapter()); - synchronized (mutex) { - mutex.notify(); - } - } - }); - - while (adapters.isEmpty()) { - synchronized (mutex) { - try { - mutex.wait(UnitConversions.ONE_SECOND); - } 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 void start() { sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); startCurrentSensor(); diff --git a/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java b/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java index 9528d0ac7..28b35f54a 100644 --- a/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/BluetoothUtils.java @@ -19,7 +19,11 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGattCharacteristic; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -33,6 +37,43 @@ public class BluetoothUtils { private BluetoothUtils() { } + public static BluetoothAdapter getDefaultBluetoothAdapter(final String TAG) { + // If from the main application thread, return directly + if (Thread.currentThread().equals(Looper.getMainLooper().getThread())) { + return BluetoothAdapter.getDefaultAdapter(); + } + + // Get the default adapter from the main application thread + final ArrayList adapters = new ArrayList<>(1); + final Object mutex = new Object(); + + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + @Override + public void run() { + adapters.add(BluetoothAdapter.getDefaultAdapter()); + synchronized (mutex) { + mutex.notify(); + } + } + }); + + while (adapters.isEmpty()) { + synchronized (mutex) { + try { + mutex.wait(UnitConversions.ONE_SECOND); + } 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); + } + /** * Populates the device names and the device addresses with all the suitable * bluetooth devices.