首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝牙SCO在来电后失败

蓝牙SCO在来电后失败
EN

Stack Overflow用户
提问于 2014-06-08 16:17:14
回答 5查看 2.1K关注 0票数 5

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

我能够成功地发送音频,

但是当有来电时,我需要断开与SCO的连接,以便应用程序音频不会干扰呼叫,

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

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

代码语言:javascript
复制
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()时,音频并不是从耳机中播放出来的,而是从电话扬声器中播放出来的。

EN

回答 5

Stack Overflow用户

发布于 2014-06-13 02:40:04

有没有可能上海合作组织的链接是在通话结束后中断的?如果是这种情况,那么SCO链路也必须随路由音频一起启动。

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

票数 1
EN

Stack Overflow用户

发布于 2014-06-14 03:18:28

您可能需要调用:

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

在您设置模式之后。

票数 1
EN

Stack Overflow用户

发布于 2014-06-17 07:23:36

在你的重新启动函数中,再次初始化所有的东西,看看它是否工作。如下所示:

代码语言:javascript
复制
public static void restartBluetooth(){
    enableBluetooth(getApplicationContext());
}

如果这是有效的,那么这意味着当调用结束时,由于某种原因,最后的初始化将丢失。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24104250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档