如何使用蓝牙耳机 API 获取蓝牙连接设备 [英] How to get bluetooth connected devices using BluetoothHeadset API

查看:52
本文介绍了如何使用蓝牙耳机 API 获取蓝牙连接设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I found BluetoothHeadset API in API level 11 which provides method getConnectedDevices() to get list of connected bluetooth devices.

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

How to get list of bluetooth connected devices using this API ?

推荐答案

终于有了解决方案.下面是一些使用 BluetoothHeadset API 获取蓝牙音频连接设备的代码片段.

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<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();                        
for ( final BluetoothDevice dev : devices ) {           
     return mBluetoothHeadset.isAudioConnected(dev);
}


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

这篇关于如何使用蓝牙耳机 API 获取蓝牙连接设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆