Stabilize bluetooth a bit.

This commit is contained in:
Alex Hart 2020-12-07 17:33:14 -04:00 committed by GitHub
parent c00b0727e3
commit 62297f1f98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -41,8 +41,9 @@ public class BluetoothStateManager {
private final BluetoothStateListener listener; private final BluetoothStateListener listener;
private final AtomicBoolean destroyed; private final AtomicBoolean destroyed;
private volatile ScoConnection scoConnection = ScoConnection.DISCONNECTED;
private BluetoothHeadset bluetoothHeadset = null; private BluetoothHeadset bluetoothHeadset = null;
private ScoConnection scoConnection = ScoConnection.DISCONNECTED;
private boolean wantsConnection = false; private boolean wantsConnection = false;
public BluetoothStateManager(@NonNull Context context, @Nullable BluetoothStateListener listener) { public BluetoothStateManager(@NonNull Context context, @Nullable BluetoothStateListener listener) {
@ -110,7 +111,17 @@ public class BluetoothStateManager {
} }
private void handleBluetoothStateChange() { private void handleBluetoothStateChange() {
if (listener != null && !destroyed.get()) listener.onBluetoothStateChanged(isBluetoothAvailable()); if (!destroyed.get()) {
boolean isBluetoothAvailable = isBluetoothAvailable();
if (!isBluetoothAvailable) {
setWantsConnection(false);
}
if (listener != null) {
listener.onBluetoothStateChanged(isBluetoothAvailable);
}
}
} }
private boolean isBluetoothAvailable() { private boolean isBluetoothAvailable() {
@ -191,18 +202,11 @@ public class BluetoothStateManager {
for (BluetoothDevice device : devices) { for (BluetoothDevice device : devices) {
if (bluetoothHeadset.isAudioConnected(device)) { if (bluetoothHeadset.isAudioConnected(device)) {
int deviceClass = device.getBluetoothClass().getDeviceClass(); scoConnection = ScoConnection.CONNECTED;
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || if (wantsConnection) {
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO || AudioManager audioManager = ServiceUtil.getAudioManager(context);
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) audioManager.setBluetoothScoOn(true);
{
scoConnection = ScoConnection.CONNECTED;
if (wantsConnection) {
AudioManager audioManager = ServiceUtil.getAudioManager(context);
audioManager.setBluetoothScoOn(true);
}
} }
} }
} }