|
|
|
@ -34,8 +34,8 @@ public class BluetoothStateManager {
|
|
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
private final BluetoothAdapter bluetoothAdapter;
|
|
|
|
|
private final BluetoothScoReceiver bluetoothScoReceiver;
|
|
|
|
|
private final BluetoothConnectionReceiver bluetoothConnectionReceiver;
|
|
|
|
|
private BluetoothScoReceiver bluetoothScoReceiver;
|
|
|
|
|
private BluetoothConnectionReceiver bluetoothConnectionReceiver;
|
|
|
|
|
private final BluetoothStateListener listener;
|
|
|
|
|
|
|
|
|
|
private BluetoothHeadset bluetoothHeadset = null;
|
|
|
|
@ -54,9 +54,7 @@ public class BluetoothStateManager {
|
|
|
|
|
|
|
|
|
|
requestHeadsetProxyProfile();
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
|
|
|
|
|
context.registerReceiver(bluetoothConnectionReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Intent sticky = context.registerReceiver(bluetoothScoReceiver, new IntentFilter(getScoChangeIntent()));
|
|
|
|
|
|
|
|
|
@ -68,16 +66,18 @@ public class BluetoothStateManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDestroy() {
|
|
|
|
|
if (bluetoothHeadset != null && bluetoothAdapter != null && Build.VERSION.SDK_INT >= 11) {
|
|
|
|
|
if (bluetoothHeadset != null && bluetoothAdapter != null) {
|
|
|
|
|
this.bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothHeadset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11 && bluetoothConnectionReceiver != null) {
|
|
|
|
|
if (bluetoothConnectionReceiver != null) {
|
|
|
|
|
context.unregisterReceiver(bluetoothConnectionReceiver);
|
|
|
|
|
bluetoothConnectionReceiver = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bluetoothScoReceiver != null) {
|
|
|
|
|
context.unregisterReceiver(bluetoothScoReceiver);
|
|
|
|
|
bluetoothScoReceiver = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.bluetoothHeadset = null;
|
|
|
|
@ -115,11 +115,7 @@ public class BluetoothStateManager {
|
|
|
|
|
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) return false;
|
|
|
|
|
if (!audioManager.isBluetoothScoAvailableOffCall()) return false;
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
|
return bluetoothHeadset != null && !bluetoothHeadset.getConnectedDevices().isEmpty();
|
|
|
|
|
} else {
|
|
|
|
|
return audioManager.isBluetoothScoOn() || audioManager.isBluetoothA2dpOn();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.w(TAG, e);
|
|
|
|
@ -137,7 +133,6 @@ public class BluetoothStateManager {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void requestHeadsetProxyProfile() {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11) {
|
|
|
|
|
this.bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() {
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
|
|
|
|
|
@Override
|
|
|
|
@ -172,7 +167,6 @@ public class BluetoothStateManager {
|
|
|
|
|
}
|
|
|
|
|
}, BluetoothProfile.HEADSET);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class BluetoothScoReceiver extends BroadcastReceiver {
|
|
|
|
|
@Override
|
|
|
|
@ -185,7 +179,7 @@ public class BluetoothStateManager {
|
|
|
|
|
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
|
|
|
|
|
|
|
|
|
|
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
|
|
|
|
|
if (Build.VERSION.SDK_INT >= 11 && bluetoothHeadset != null) {
|
|
|
|
|
if (bluetoothHeadset != null) {
|
|
|
|
|
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
|
|
|
|
|
|
|
|
|
|
for (BluetoothDevice device : devices) {
|
|
|
|
|