检测蓝牙耳机是否连接
Detect if bluetooth headset connected
在 VOIP 应用程序上工作,在静音模式下,提示音或铃声应仅在蓝牙耳机上播放。如果已连接,可以在耳机上播放,但如果未连接耳机,虽然手机处于静音模式,但扬声器会播放音调。
有没有办法检测蓝牙耳机是否已连接,请大神解释一下。
感谢@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 class,它负责获取耳机配置文件、处理监听器、检查蓝牙是否已启用或 not.I 是否未使用广播接收器。
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;
}}
这是我的代码:
/** */
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 可以让您在执行应用程序时检测耳机何时 connected/disconnected。
要进行简单检查,请使用以下内容。请记住将 BLUETOOTH 权限添加到您的清单(非危险权限)
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
return (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET))
在 VOIP 应用程序上工作,在静音模式下,提示音或铃声应仅在蓝牙耳机上播放。如果已连接,可以在耳机上播放,但如果未连接耳机,虽然手机处于静音模式,但扬声器会播放音调。
有没有办法检测蓝牙耳机是否已连接,请大神解释一下。
感谢@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 class,它负责获取耳机配置文件、处理监听器、检查蓝牙是否已启用或 not.I 是否未使用广播接收器。
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;
}}
这是我的代码:
/** */
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 可以让您在执行应用程序时检测耳机何时 connected/disconnected。
要进行简单检查,请使用以下内容。请记住将 BLUETOOTH 权限添加到您的清单(非危险权限)
val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
return (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET))