gpt4 book ai didi

android - 来电后蓝牙 SCO 失败

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:55:15 26 4
gpt4 key购买 nike

我正在尝试通过 SCO 发送应用程序的所有音频。

我能够成功发送音频,

但是当来电时,我需要断开与 SCO 的连接,这样应用程序的音频就不会干扰通话,

问题是,当我在通话后尝试将音频重新路由到 SCO 时,它不起作用。

这是我用来将音频发送到 SCO 的代码:

public class BluetoothManager {
// For Bluetooth connectvity
private static String TAG = "BluetoothManager";
private static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private static AudioManager aM;

/**
* Set the audio manager of the device.
* @param c: The context this method is called from
*/
public static void setAudioManager(Context c) {
aM = (android.media.AudioManager)c.getSystemService(Context.AUDIO_SERVICE);
}

/**
* Check if a Bluetooth headset is connected. If so, route audio to Bluetooth SCO.
*/
private static void initializeAudioMode(Context context) {
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (profile == BluetoothProfile.HEADSET) {
BluetoothHeadset bh = (BluetoothHeadset) proxy;
List<BluetoothDevice> devices = bh.getConnectedDevices();
if (devices.size() > 0) {
enableBluetoothSCO();
}
}
mBluetoothAdapter.closeProfileProxy(profile, proxy);
}
public void onServiceDisconnected(int profile) {}
};
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
}

/**
* Bluetooth Connectvity
* The following methods are associated with enabling/disabling Bluetooth.
* In the future we may want to disable other sources of audio.
*/
private static void enableBluetoothSCO() {
aM.setMode(AudioManager.MODE_IN_CALL);
aM.startBluetoothSco();
aM.setBluetoothScoOn(true);
}

/** Right now, this simply enables Bluetooth */
@SuppressLint("NewApi")
public static boolean enableBluetooth(Context c) {
// If there is an adapter, enable it if not already enabled
if (mBluetoothAdapter != null) {

if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
}

setAudioManager(c);
initializeAudioMode(c);
Log.e(TAG, "SCO: " + aM.isBluetoothScoOn());
Log.e(TAG, "A2DP: " + aM.isSpeakerphoneOn());
return true;
} else {
Log.v(TAG, "There is no bluetooth adapter");
return false;
}
}

/** Right now, this simply disables Bluetooth */
public static void disableBluetooth() {
// If there is an adapter, disabled it if not already disabled
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
} else {
Log.v(TAG, "There is no bluetooth adapter");
}
}

public static void restartBluetooth(){
aM.setMode(AudioManager.MODE_IN_CALL);

}
public static void stopBluetooth(){
aM.setMode(AudioManager.MODE_NORMAL);

}

}

当我正确调用 stopBluetooth() 时,应用程序的音频不再发送到耳机,

但是当我调用 restartBluetooth() 时,音频不是按预期从耳机播放的,而是从手机扬声器播放的。

最佳答案

有没有可能是通话结束后SCO链接挂了?如果是这种情况,则还必须在路由音频的同时启动 SCO 链接。

你试过在restartBluetooth()中调用enableBluetoothSCO()

关于android - 来电后蓝牙 SCO 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24104250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com