下面列出了android.media.AudioManager#isBluetoothScoOn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void handleWiredHeadsetChange(Intent intent) {
Log.i(TAG, "handleWiredHeadsetChange...");
if ((activePeer != null) &&
(activePeer.getState() == CallState.CONNECTED ||
activePeer.getState() == CallState.DIALING ||
activePeer.getState() == CallState.RECEIVED_BUSY ||
activePeer.getState() == CallState.REMOTE_RINGING))
{
AudioManager audioManager = ServiceUtil.getAudioManager(this);
boolean present = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
if (present && audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setBluetoothScoOn(false);
} else if (!present && !audioManager.isSpeakerphoneOn() && !audioManager.isBluetoothScoOn() && localCameraState.isEnabled()) {
audioManager.setSpeakerphoneOn(true);
}
sendMessage(viewModelStateFor(activePeer), activePeer, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled, isRemoteVideoOffer);
}
}
private void handleWiredHeadsetChange(Intent intent) {
ALog.d(TAG, "handleWiredHeadsetChange...");
CallState currentState = this.callState.get();
if (currentState == CallState.STATE_CONNECTED ||
currentState == CallState.STATE_DIALING ||
currentState == CallState.STATE_REMOTE_RINGING) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(this);
boolean present = intent.getBooleanExtra(EXTRA_AVAILABLE, false);
if (present && audioManager.isSpeakerphoneOn()) {
audioManager.setSpeakerphoneOn(false);
audioManager.setBluetoothScoOn(false);
} else if (!present && !audioManager.isSpeakerphoneOn() && !audioManager.isBluetoothScoOn() && localCameraState.isEnabled()) {
audioManager.setSpeakerphoneOn(true);
}
if (recipient != null) {
sendMessage(viewModelStateFor(callState.get()), recipient, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled);
}
}
}
public void stop(boolean playDisconnected) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
incomingRinger.stop();
outgoingRinger.stop();
if (playDisconnected) {
soundPool.play(disconnectedSoundId, 1.0f, 1.0f, 0, 0, 1.0f);
}
if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
}
audioManager.setSpeakerphoneOn(false);
audioManager.setMicrophoneMute(false);
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.abandonAudioFocus(null);
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
@Override
public void execute(AudioManager audioManager) {
if (voiceMediator.isBlueToothHeadSet()) {
if (!voiceMediator.isSuportA2DP()) {
if (audioManager.getMode() != AudioManager.MODE_NORMAL) {
Log.e(TAG, "playInChannel>>setMode(AudioManager.MODE_NORMAL)");
audioManager.setMode(AudioManager.MODE_NORMAL);
}
if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
}
} else {
if (!audioManager.isBluetoothA2dpOn()) {
Log.e(TAG, "playInChannel>>setBluetoothA2dpOn(true)");
audioManager.setBluetoothA2dpOn(true);
}
}
}
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
@SuppressLint("NewApi")
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if (isHeadsetPlugged || am.isSpeakerphoneOn() || (isBluetoothHeadsetConnected() && am.isBluetoothScoOn())) {
return;
}
boolean newIsNear = event.values[0] < Math.min(event.sensor.getMaximumRange(), 3);
if (newIsNear != isProximityNear) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("proximity " + newIsNear);
}
isProximityNear = newIsNear;
try{
if(isProximityNear){
proximityWakelock.acquire();
}else{
proximityWakelock.release(1); // this is non-public API before L
}
}catch(Exception x){
FileLog.e(x);
}
}
}
}
public int getCurrentAudioRoute(){
if(USE_CONNECTION_SERVICE){
if(systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
switch(systemCallConnection.getCallAudioState().getRoute()){
case CallAudioState.ROUTE_BLUETOOTH:
return AUDIO_ROUTE_BLUETOOTH;
case CallAudioState.ROUTE_EARPIECE:
case CallAudioState.ROUTE_WIRED_HEADSET:
return AUDIO_ROUTE_EARPIECE;
case CallAudioState.ROUTE_SPEAKER:
return AUDIO_ROUTE_SPEAKER;
}
}
return audioRouteToSet;
}
if(audioConfigured){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
if(am.isBluetoothScoOn())
return AUDIO_ROUTE_BLUETOOTH;
else if(am.isSpeakerphoneOn())
return AUDIO_ROUTE_SPEAKER;
else
return AUDIO_ROUTE_EARPIECE;
}
return audioRouteToSet;
}
private LockManager.PhoneState getInCallPhoneState() {
AudioManager audioManager = ServiceUtil.getAudioManager(this);
if (audioManager.isSpeakerphoneOn() || audioManager.isBluetoothScoOn() || audioManager.isWiredHeadsetOn()) {
return LockManager.PhoneState.IN_HANDS_FREE_CALL;
} else {
return LockManager.PhoneState.IN_CALL;
}
}
public void startIncomingRinger(@Nullable Uri ringtoneUri, boolean vibrate) {
AudioManager audioManager = ServiceUtil.getAudioManager(context);
boolean speaker = !audioManager.isWiredHeadsetOn() && !audioManager.isBluetoothScoOn();
audioManager.setMode(AudioManager.MODE_RINGTONE);
audioManager.setMicrophoneMute(false);
audioManager.setSpeakerphoneOn(speaker);
incomingRinger.start(ringtoneUri, vibrate);
}
public void startIncomingRinger() {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
boolean speaker = !audioManager.isWiredHeadsetOn() && !audioManager.isBluetoothScoOn();
audioManager.setMode(AudioManager.MODE_RINGTONE);
audioManager.setMicrophoneMute(false);
audioManager.setSpeakerphoneOn(speaker);
incomingRinger.start();
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
/**
* 检查通信是否使用蓝牙 SCO
* @return {@code true} yes, {@code false} no
*/
public static boolean isBluetoothScoOn() {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
return audioManager.isBluetoothScoOn();
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "isBluetoothScoOn");
}
}
return false;
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
public boolean isSpeakerphoneOn(){
if(USE_CONNECTION_SERVICE && systemCallConnection!=null && systemCallConnection.getCallAudioState()!=null){
int route=systemCallConnection.getCallAudioState().getRoute();
return hasEarpiece() ? route==CallAudioState.ROUTE_SPEAKER : route==CallAudioState.ROUTE_BLUETOOTH;
}else if(audioConfigured && !USE_CONNECTION_SERVICE){
AudioManager am=(AudioManager) getSystemService(AUDIO_SERVICE);
return hasEarpiece() ? am.isSpeakerphoneOn() : am.isBluetoothScoOn();
}
return speakerphoneStateToSet;
}
private void handleSetEnableVideo(Intent intent) {
boolean enable = intent.getBooleanExtra(EXTRA_ENABLE, false);
AudioManager audioManager = ServiceUtil.getAudioManager(this);
if (activePeer == null) {
Log.w(TAG, "handleSetEnableVideo(): Ignoring for inactive call.");
return;
}
Log.i(TAG, "handleSetEnableVideo(): call_id: " + activePeer.getCallId());
if (activePeer.getState() != CallState.CONNECTED) {
enableVideoOnCreate = enable;
if (enableVideoOnCreate &&
!audioManager.isSpeakerphoneOn() &&
!audioManager.isBluetoothScoOn() &&
!audioManager.isWiredHeadsetOn())
{
audioManager.setSpeakerphoneOn(true);
}
return;
}
try {
callManager.setVideoEnable(enable);
} catch (CallException e) {
callFailure("setVideoEnable() failed: ", e);
return;
}
localCameraState = camera.getCameraState();
if (localCameraState.isEnabled()) {
lockManager.updatePhoneState(LockManager.PhoneState.IN_VIDEO);
} else {
lockManager.updatePhoneState(getInCallPhoneState());
}
if (localCameraState.isEnabled() &&
!audioManager.isSpeakerphoneOn() &&
!audioManager.isBluetoothScoOn() &&
!audioManager.isWiredHeadsetOn())
{
audioManager.setSpeakerphoneOn(true);
}
sendMessage(viewModelStateFor(activePeer), activePeer, localCameraState, remoteVideoEnabled, bluetoothAvailable, microphoneEnabled, isRemoteVideoOffer);
}