5

我正在尝试通过 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(),音频播放不是按预期从耳机播放,而是从手机扬声器播放。

4

5 回答 5

1

在您的重新启动函数中再次初始化所有内容,看看它是否有效。像这样:

public static void restartBluetooth(){
    enableBluetooth(getApplicationContext());
}

如果这有效,则意味着当调用结束时,最后一次初始化由于某种原因丢失。

于 2014-06-16T23:23:36.290 回答
1

通话结束后SCO链接是否可能断开?如果是这种情况,则还必须启动 SCO 链接以及路由音频。

您是否尝试过在 restartBluetooth() 中调用 enableBluetoothSCO()

于 2014-06-12T18:40:04.280 回答
1

您可能需要致电:

aM.startBluetoothSco(); aM.setBluetoothScoOn(true);

设置模式后。

于 2014-06-13T19:18:28.223 回答
1

对于任何对此仍有疑问的人,有一些事情需要做。您需要做的第一件事是跟踪电话状态。你可以在这里看到如何做到这一点: 如何知道电话已经结束?

当状态为空闲时,表示来电已结束。现在,如果您此时尝试重新连接蓝牙,您会发现它仍然无法正常工作,因为调用“释放”蓝牙设备需要一段时间(大约 2 秒)。因此,您有两个选择,稍等片刻然后尝试重新连接,或者您可以将另一个侦听器添加到 BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED。

然后,您可以添加一个全局布尔值isIdle,当 TelephonyManager.CALL_STATE_IDLE 为 true 或 TelephonyManager.CALL_STATE_OFFHOOK 时为 false(否则您将在来电期间重新连接到蓝牙)。此时当BluetoothHeadset.STATE_DISCONNECTED和isIdle为真时,再重新连接蓝牙。

@Override public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals((BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED))){
        int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
        switch(state) {
            case BluetoothHeadset.STATE_AUDIO_DISCONNECTED:
                if (isIdle){
                    //reconnect bluetooth 
                }
                break;
        }
    }
    if(("OFFHOOK").equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))) {
        isIdle = false;
        // turn bluetooth off
    }
    if(("IDLE").equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))) {
        isIdle = true;
    }
}
于 2020-06-13T00:18:18.633 回答
0

谷歌文档是这样说的

“电话应用程序始终优先使用 SCO 连接进行电话。如果在电话通话时调用此方法,它将被忽略。同样,如果在应用程序使用 SCO 连接时接收或发送呼叫,应用程序的连接将丢失,呼叫结束时不会自动返回。”

因此,当通话断开时,您必须通过调用startBluetoothSco()重新建立连接

于 2015-02-11T06:59:50.360 回答