How to get bluetooth connected devices using BluetoothHeadset API

后端 未结 2 947
慢半拍i
慢半拍i 2020-11-30 09:10

I want to get list of bluetooth connected devices...not just paired devices.

I found BluetoothHeadset API in API level 11 which provide

2条回答
  •  Happy的楠姐
    2020-11-30 09:42

    Finally got the solution. Below are a few code snippets for getting Bluetooth audio connected devices using BluetoothHeadset API.

    BluetoothHeadset mBluetoothHeadset;
    
    // Get the default adapter
    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    
    // Establish connection to the proxy.
    mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
    


    // Define Service Listener of BluetoothProfile
    private BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.HEADSET) {
                mBluetoothHeadset = (BluetoothHeadset) proxy;
            }
        }
        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.HEADSET) {
                mBluetoothHeadset = null;
            }
        }
    };
    


    // call functions on mBluetoothHeadset to check if Bluetooth SCO audio is connected.
    List devices = mBluetoothHeadset.getConnectedDevices();                        
    for ( final BluetoothDevice dev : devices ) {           
         return mBluetoothHeadset.isAudioConnected(dev);
    }
    


    // finally Close proxy connection after use.
    mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
    

提交回复
热议问题