代码之家  ›  专栏  ›  技术社区  ›  Jitu

检测蓝牙耳机是否已连接

  •  7
  • Jitu  · 技术社区  · 7 年前

    使用VOIP应用程序时,在静音模式下,只能在蓝牙耳机上播放提示音或铃声。如果已连接,则可以在耳机上播放,但如果未连接耳机,则即使手机处于静音模式,扬声器上也会播放铃声。

    3 回复  |  直到 7 年前
        1
  •  5
  •   Codelicious    6 年前

    要进行简单检查,请使用以下命令。请记住将蓝牙权限添加到您的清单中(非危险权限)

    val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
    return (bluetoothAdapter != null && BluetoothProfile.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET))
    
        2
  •  4
  •   Alex Shevelev    6 年前

    这是我的代码:

    /** */
    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可以让你在应用程序运行时检测耳机的连接/断开。

        3
  •  2
  •   fdermishin    3 年前

    我就是这样处理的。

    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

    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;
            }}