Android如何在耳机已连接时通过听筒耳机接听电话?

时间:2013-11-18 08:51:54

标签: android telephonymanager android-audiomanager headset

耳机设备已连接到我的手机。现在正在拨打电话,我想要接听电话只需使用电话而非耳机。怎么做?

任何帮助都将不胜感激!

1 个答案:

答案 0 :(得分:1)

再说一遍,看看这些代码。你应该听一下 PhoneStateListener的CALL_STATE_OFFHOOK 并在延迟时间调用 setHandsetPhone()

private void setHandsetPhone()
{
    if (isBluetoothAvailable() && isBluetoothAudioConnected())
    {
        disconnectBluetoothAudio();
    }
    //am == AudioManager
    if (am.isSpeakerphoneOn())
    {
        am.setSpeakerphoneOn(false);
    }
}

private boolean isBluetoothAvailable()
{
    printLog("isBluetoothAvailable()...");
    // Check if there's a connected headset, using the BluetoothHeadset API.
    boolean isConnected = false;
    if (mBluetoothHeadset != null)
    {
        List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();

        if (deviceList.size() > 0)
        {
            isConnected = true;
        }
    }
    return isConnected;
}

/**
 * @return true if a BT Headset is available, and its audio is currently
 *         connected.
 */
private boolean isBluetoothAudioConnected()
{
    if (mBluetoothHeadset == null)
    {
        return false;
    }
    List<BluetoothDevice> deviceList = mBluetoothHeadset.getConnectedDevices();

    if (deviceList.isEmpty())
    {
        return false;
    }
    BluetoothDevice device = deviceList.get(0);
    boolean isAudioOn = mBluetoothHeadset.isAudioConnected(device);
    return isAudioOn;
}

private void disconnectBluetoothAudio()
{
    if (mBluetoothHeadset != null)
    {
        Class clazz = mBluetoothHeadset.getClass();
        try
        {
            Method method = clazz.getMethod("disconnectAudio",
                    new Class[] {});
            System.out.println("result-" + method.invoke(mBluetoothHeadset));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}