下面列出了android.media.AudioManager#SCO_AUDIO_STATE_CONNECTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onNotifySCOAudioStateChange(int scoAudioState) {
switch (scoAudioState) {
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
am.setBluetoothScoOn(true);
}
break;
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
Log.d("onNotifyHeadsetState",
"onNotifySCOAudioStateChange: " + headsetPlugReceiver.isBluetoothConnected());
if (headsetPlugReceiver.isBluetoothConnected()) {
startBluetoothSco();
}
break;
}
}
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(THIS_FILE, ">>> BT SCO state changed !!! ");
if(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED.equals(action)) {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR );
Log.d(THIS_FILE, "BT SCO state changed : " + status + " target is " + targetBt);
audioManager.setBluetoothScoOn(targetBt);
if(status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
isBluetoothConnected = true;
}else if(status == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
isBluetoothConnected = false;
}
if(btChangesListener != null) {
btChangesListener.onBluetoothStateChanged(status);
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int audioState = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
switch (audioState) {
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
mBluetoothScoOn = true;
mListener.onBluetoothScoConnected();
break;
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
case AudioManager.SCO_AUDIO_STATE_ERROR:
am.stopBluetoothSco();
mBluetoothScoOn = false;
mListener.onBluetoothScoDisconnected();
break;
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
Log.i(TAG, "onReceive");
synchronized (LOCK) {
if (getScoChangeIntent().equals(intent.getAction())) {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
if (bluetoothHeadset != null) {
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
for (BluetoothDevice device : devices) {
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;
if (wantsConnection) {
AudioManager audioManager = ServiceUtil.getAudioManager(context);
audioManager.setBluetoothScoOn(true);
}
}
}
}
}
}
}
}
handleBluetoothStateChange();
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
return;
}
Log.w(TAG, "onReceive");
synchronized (LOCK) {
if (getScoChangeIntent().equals(intent.getAction())) {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED && bluetoothHeadset != null) {
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
for (BluetoothDevice device : devices) {
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;
if (wantsConnection) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
audioManager.setBluetoothScoOn(true);
}
}
}
}
}
}
}
handleBluetoothStateChange();
}
/**
* Registers receiver for the broadcasted intent related the existence
* of a BT SCO channel. Indicates if BT SCO streaming is on or off.
*/
private void registerForBluetoothScoIntentBroadcast() {
IntentFilter filter = new IntentFilter(
AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
/** BroadcastReceiver implementation which handles changes in BT SCO. */
mBluetoothScoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int state = intent.getIntExtra(
AudioManager.EXTRA_SCO_AUDIO_STATE,
AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
if (DEBUG) {
logd("BroadcastReceiver.onReceive: a=" + intent.getAction()
+ ", s=" + state
+ ", sb=" + isInitialStickyBroadcast());
}
switch (state) {
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
mBluetoothScoState = STATE_BLUETOOTH_SCO_ON;
break;
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
if (mBluetoothScoState != STATE_BLUETOOTH_SCO_TURNING_OFF) {
// Bluetooth is probably powered off during the call.
// Update the existing device selection, but only if a specific
// device has already been selected explicitly.
if (deviceHasBeenRequested()) {
updateDeviceActivation();
}
}
mBluetoothScoState = STATE_BLUETOOTH_SCO_OFF;
break;
case AudioManager.SCO_AUDIO_STATE_CONNECTING:
// do nothing
break;
default:
loge("Invalid state");
}
if (DEBUG) {
reportUpdate();
}
}
};
ContextUtils.getApplicationContext().registerReceiver(mBluetoothScoReceiver, filter);
}
@SuppressWarnings({"deprecation", "synthetic-access"})
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
BluetoothDevice mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();
if (bluetoothClass != null) {
// Check if device is a headset. Besides the 2 below, are there other
// device classes also qualified as headset?
int deviceClass = bluetoothClass.getDeviceClass();
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE
|| deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
// start bluetooth Sco audio connection.
// Calling startBluetoothSco() always returns faIL here,
// that why a count down timer is implemented to call
// startBluetoothSco() in the onTick.
mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mIsCountDownOn = true;
mCountDown.start();
// override this if you want to do other thing when the device is connected.
onHeadsetConnected();
}
}
Log.d(TAG, mConnectedHeadset.getName() + " connected");
} else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
Log.d(TAG, "Headset disconnected");
if (mIsCountDownOn) {
mIsCountDownOn = false;
mCountDown.cancel();
}
mAudioManager.setMode(AudioManager.MODE_NORMAL);
// override this if you want to do other thing when the device is disconnected.
onHeadsetDisconnected();
} else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)) {
int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,
AudioManager.SCO_AUDIO_STATE_ERROR);
if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
mIsOnHeadsetSco = true;
if (mIsStarting) {
// When the device is connected before the application starts,
// ACTION_ACL_CONNECTED will not be received, so call onHeadsetConnected here
mIsStarting = false;
onHeadsetConnected();
}
if (mIsCountDownOn) {
mIsCountDownOn = false;
mCountDown.cancel();
}
// override this if you want to do other thing when Sco audio is connected.
onScoAudioConnected();
Log.d(TAG, "Sco connected");
} else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
Log.d(TAG, "Sco disconnected");
// Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()
// which at that stage we do not want to do anything. Thus the if condition.
if (!mIsStarting) {
mIsOnHeadsetSco = false;
// Need to call stopBluetoothSco(), otherwise startBluetoothSco()
// will not be successful.
mAudioManager.stopBluetoothSco();
// override this if you want to do other thing when Sco audio is disconnected.
onScoAudioDisconnected();
}
}
}
}