在静音模式下使用VOIP应用程序时,只能在蓝牙耳机上播放提示音或铃声。如果连接后可以在耳机上播放,但如果没有连接耳机,虽然手机处于静音模式,但扬声器会播放音调。
有人请解释是否有办法检测到蓝牙耳机已连接。
答案 0 :(得分:1)
感谢@cristallo的回答
这就是我处理它的方式。
连接到代理
bluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.HEADSET);
创建了一个监听器
private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener()
{
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy)
{
if (profile == BluetoothProfile.HEADSET)
{
mBluetoothHeadset = (BluetoothHeadset) proxy;
if(mBluetoothHeadset.getConnectedDevices().size()>0) {
IS_BLUETOOTH_CONNECTED = true;
Logs.d(TAG,"Bluetooth device is connected");
}
}
}
@Override
public void onServiceDisconnected(int profile)
{
if (profile == BluetoothProfile.HEADSET)
{
mBluetoothHeadset = null;
IS_BLUETOOTH_CONNECTED = false;
Logs.d(TAG,"Bluetooth device is disconnected");
}
}
};
摘自Detect programatically if headphone or bluetooth headset attached with android phone
博主Vipul创建了一个BluetoothHeadsetManager类,用于处理耳机配置文件,处理监听器,检查蓝牙是否启用。我还没有使用广播接收器。
switch (audioMgr.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
if(BluetoothHeadsetManager.IS_BLUETOOTH_CONNECTED) {
//play notification
}
break;
case AudioManager.RINGER_MODE_VIBRATE:
if(BluetoothHeadsetManager.IS_BLUETOOTH_CONNECTED) {
//play notification
}
break;
case AudioManager.RINGER_MODE_NORMAL:
//play ringtone
break;
default:
break;
}}
答案 1 :(得分:1)
这是我的代码:
/** */
class BluetoothStateMonitor(private val appContext: Context): BroadcastReceiver(), MonitorInterface {
var isHeadsetConnected = false
@Synchronized
get
@Synchronized
private set
/** Start monitoring */
override fun start() {
val bluetoothManager = appContext.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter.getProfileProxy(appContext, object:BluetoothProfile.ServiceListener {
/** */
override fun onServiceDisconnected(profile: Int) {
isHeadsetConnected = false
}
/** */
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile?) {
isHeadsetConnected = proxy!!.connectedDevices.size > 0
}
}, BluetoothProfile.HEADSET)
appContext.registerReceiver(this, IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED))
}
/** Stop monitoring */
override fun stop() {
appContext.unregisterReceiver(this)
}
/** For broadcast receiver */
override fun onReceive(context: Context?, intent: Intent?) {
val connectionState = intent!!.extras!!.getInt(BluetoothAdapter.EXTRA_CONNECTION_STATE)
when(connectionState) {
BluetoothAdapter.STATE_CONNECTED -> isHeadsetConnected = true
BluetoothAdapter.STATE_DISCONNECTED -> isHeadsetConnected = false
else -> {}
}
}
}
让我解释一下。您应该使用ProfileProxy和BroadcastReceiver。 ProfileProxy允许您在应用程序运行之前检测耳机连接时的情况。反过来,BroadcastReceiver可以让您检测在执行应用程序时连接/断开耳机的时间。
答案 2 :(得分:1)
要进行简单检查,请使用以下内容。请记住将BLUETOOTH权限添加到您的清单(非危险权限)
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
return (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET))
答案 3 :(得分:-1)
对于蓝牙耳机,您可以使用getConnectedDevices()
参考:http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html