Android如何在耳机已连接的情况下通过手机听筒接听电话?
已将耳机设备连接到我的手机。现在电话即将打来,我只想接听电话,不要使用耳机。如何做到这一点Android如何在耳机已连接的情况下通过手机听筒接听电话?,android,telephonymanager,android-audiomanager,headset,Android,Telephonymanager,Android Audiomanager,Headset,已将耳机设备连接到我的手机。现在电话即将打来,我只想接听电话,不要使用耳机。如何做到这一点 任何帮助都将不胜感激 不要再说了,看看这些代码。你应该听PhoneStateListener的CALL\u STATE\u OFFHOOK并在延迟时间调用setHandsetPhone() private void setHandsetPhone() { if (isBluetoothAvailable() && isBluetoothAudioConnected()) {
任何帮助都将不胜感激 不要再说了,看看这些代码。你应该听PhoneStateListener的CALL\u STATE\u 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();
}
}
}
private void setHandsetPhone()
{
if(isBluetoothAvailable()&&isBluetoothAudioConnected())
{
断开BluetoothAudio()的连接;
}
//am==音频管理器
if(am.isSonerPhoneOn())
{
上午9时30分(假);
}
}
私有布尔值isBluetoothAvailable()
{
打印日志(“isBluetoothAvailable()…”;
//使用蓝牙耳机API检查是否有已连接的耳机。
布尔值未连接=假;
如果(mBluetoothHeadset!=空)
{
List deviceList=mBluetoothHeadset.getConnectedDevices();
如果(deviceList.size()>0)
{
断开连接=正确;
}
}
返回不连接;
}
/**
*@如果BT耳机可用,且其音频当前为
*连接。
*/
私有布尔值isBluetoothAudioConnected()
{
如果(mBluetoothHeadset==null)
{
返回false;
}
List deviceList=mBluetoothHeadset.getConnectedDevices();
if(deviceList.isEmpty())
{
返回false;
}
BluetoothDevice=deviceList.get(0);
布尔值isAudioOn=mblueothHeadset.isAudioConnected(设备);
返回音频;
}
私有void disconnectBluetoothAudio()
{
如果(mBluetoothHeadset!=空)
{
Class clazz=mBluetoothHeadset.getClass();
尝试
{
Method=clazz.getMethod(“断开音频连接”),
新类[]{});
System.out.println(“结果-”+method.invoke(mblueothHeadset));
}
捕获(例外e)
{
e、 printStackTrace();
}
}
}