如何使用BluetoothHeadset API获取蓝牙连接设备

Pri*_*tel 13 android bluetooth bluetooth-sco

我想获得蓝牙连接设备列表......而不仅仅是配对设备.

BluetoothHeadsetAPI级别11中找到了API,它提供getConnectedDevices()了获取连接蓝牙设备列表的方法.

如何使用此API获取蓝牙连接设备列表?

Pri*_*tel 28

终于得到了解决方案.以下是使用BluetoothHeadsetAPI 获取蓝牙音频连接设备的一些代码段.

BluetoothHeadset mBluetoothHeadset;

// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET);
Run Code Online (Sandbox Code Playgroud)


// 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;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)


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


// finally Close proxy connection after use.
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
Run Code Online (Sandbox Code Playgroud)