Stabilize bluetooth a bit.
This commit is contained in:
parent
c00b0727e3
commit
62297f1f98
1 changed files with 17 additions and 13 deletions
|
@ -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,12 +202,6 @@ public class BluetoothStateManager {
|
||||||
|
|
||||||
for (BluetoothDevice device : devices) {
|
for (BluetoothDevice device : devices) {
|
||||||
if (bluetoothHeadset.isAudioConnected(device)) {
|
if (bluetoothHeadset.isAudioConnected(device)) {
|
||||||
int deviceClass = device.getBluetoothClass().getDeviceClass();
|
|
||||||
|
|
||||||
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
|
|
||||||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
|
|
||||||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
|
|
||||||
{
|
|
||||||
scoConnection = ScoConnection.CONNECTED;
|
scoConnection = ScoConnection.CONNECTED;
|
||||||
|
|
||||||
if (wantsConnection) {
|
if (wantsConnection) {
|
||||||
|
@ -205,7 +210,6 @@ public class BluetoothStateManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue