kopia lustrzana https://github.com/ryukoposting/Signal-Android
Only try to connect to bluetooth a limited number of times in a call.
rodzic
b589449c34
commit
cdf8e4e1ed
|
@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
public class BluetoothStateManager {
|
public class BluetoothStateManager {
|
||||||
|
|
||||||
private static final String TAG = Log.tag(BluetoothStateManager.class);
|
private static final String TAG = Log.tag(BluetoothStateManager.class);
|
||||||
|
private static final int MAX_SCO_CONNECTION_ATTEMPTS = 2;
|
||||||
|
|
||||||
private enum ScoConnection {
|
private enum ScoConnection {
|
||||||
DISCONNECTED,
|
DISCONNECTED,
|
||||||
|
@ -44,6 +45,7 @@ public class BluetoothStateManager {
|
||||||
private final AtomicBoolean destroyed;
|
private final AtomicBoolean destroyed;
|
||||||
|
|
||||||
private volatile ScoConnection scoConnection = ScoConnection.DISCONNECTED;
|
private volatile ScoConnection scoConnection = ScoConnection.DISCONNECTED;
|
||||||
|
private int scoConnectionAttempts = 0;
|
||||||
|
|
||||||
private BluetoothHeadset bluetoothHeadset = null;
|
private BluetoothHeadset bluetoothHeadset = null;
|
||||||
private boolean wantsConnection = false;
|
private boolean wantsConnection = false;
|
||||||
|
@ -105,8 +107,13 @@ public class BluetoothStateManager {
|
||||||
this.wantsConnection = enabled;
|
this.wantsConnection = enabled;
|
||||||
|
|
||||||
if (wantsConnection && isBluetoothAvailable() && scoConnection == ScoConnection.DISCONNECTED) {
|
if (wantsConnection && isBluetoothAvailable() && scoConnection == ScoConnection.DISCONNECTED) {
|
||||||
|
if (scoConnectionAttempts > MAX_SCO_CONNECTION_ATTEMPTS) {
|
||||||
|
Log.w(TAG, "We've already attempted to start SCO too many times. Won't try again.");
|
||||||
|
} else {
|
||||||
|
scoConnectionAttempts++;
|
||||||
audioManager.startBluetoothSco();
|
audioManager.startBluetoothSco();
|
||||||
scoConnection = ScoConnection.IN_PROGRESS;
|
scoConnection = ScoConnection.IN_PROGRESS;
|
||||||
|
}
|
||||||
} else if (!wantsConnection && scoConnection == ScoConnection.CONNECTED) {
|
} else if (!wantsConnection && scoConnection == ScoConnection.CONNECTED) {
|
||||||
audioManager.stopBluetoothSco();
|
audioManager.stopBluetoothSco();
|
||||||
audioManager.setBluetoothScoOn(false);
|
audioManager.setBluetoothScoOn(false);
|
||||||
|
@ -210,6 +217,7 @@ public class BluetoothStateManager {
|
||||||
for (BluetoothDevice device : devices) {
|
for (BluetoothDevice device : devices) {
|
||||||
if (bluetoothHeadset.isAudioConnected(device)) {
|
if (bluetoothHeadset.isAudioConnected(device)) {
|
||||||
scoConnection = ScoConnection.CONNECTED;
|
scoConnection = ScoConnection.CONNECTED;
|
||||||
|
scoConnectionAttempts = 0;
|
||||||
|
|
||||||
if (wantsConnection) {
|
if (wantsConnection) {
|
||||||
AudioManager audioManager = ServiceUtil.getAudioManager(context);
|
AudioManager audioManager = ServiceUtil.getAudioManager(context);
|
||||||
|
@ -232,6 +240,12 @@ public class BluetoothStateManager {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
Log.i(TAG, "onReceive");
|
Log.i(TAG, "onReceive");
|
||||||
|
if (intent.getAction().equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
|
||||||
|
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
|
||||||
|
if (state == BluetoothHeadset.STATE_CONNECTED) {
|
||||||
|
scoConnectionAttempts = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
handleBluetoothStateChange();
|
handleBluetoothStateChange();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue