在Android 10及以上的版本中,可以使用 AudioDeviceInfo 来检查 SCO 是否连接。以下是示例代码:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] audioDevices = audioManager.getDevices(AudioManager.GET_DEVICES_ALL);
for (AudioDeviceInfo deviceInfo : audioDevices) {
if (deviceInfo.getType() == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
boolean isScoOn = audioManager.isBluetoothScoOn() && (audioManager.isBluetoothScoAvailableOffCall()
|| audioManager.getMode() == AudioManager.MODE_IN_COMMUNICATION);
// do something with isScoOn
break;
}
}
注:在上述示例代码中,我们通过循环所有的 AudioDeviceInfo,找到了 Bluetooth SCO 设备,并检查它是否连接。注意,我们还需要使用 isBluetoothScoAvailableOffCall
和 getMode
方法才能正确地检查 SCO 连接状态。
上一篇:AudioManager - 有没有一种方法可以获取相关的音频流?
下一篇:AudioManager.setCommunicationDevicetoTYPE_BUILTIN_SPEAKERnotworking