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

13

我想获取已连接的蓝牙设备列表,而不仅仅是已配对的设备。

我发现在API 11级中提供了BluetoothHeadset API,其中提供了getConnectedDevices()方法来获取已连接的蓝牙设备列表。

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

2个回答

29

终于找到了解决方法。以下是一些使用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);

这是一个很好的解决方案,正如上面评论中所说的,恭喜 ;) - andrea.rinaldi
我正在使用类似的解决方案,但是当我尝试通过后退按钮退出我的应用程序后连接时,我看到了E/BluetoothA2dp﹕ Could not bind to Bluetooth A2DP Service with Intent { act=android.bluetooth.IBluetoothA2dp cmp=com.android.bluetooth/.a2dp.A2dpService }。我确定这一定与清理不当有关,但由于我已经注意调用了closeProfileProxy,所以我不确定它可能是什么。你见过这种情况吗? - head in the codes

1

首先,您需要在Androindmanifest.xml中定义权限。

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

搜索已连接设备活动,

private static BluetoothAdapter mBtAdapter;
private final static int REQUEST_ENABLE_BT = 1;

            // Register for broadcasts when a device is discovered
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            this.registerReceiver(mReceiver, filter);

            // Register for broadcasts when discovery has finished
            filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
            this.registerReceiver(mReceiver, filter);


            filter = new IntentFilter( BluetoothAdapter.ACTION_DISCOVERY_STARTED );
            this.registerReceiver( mReceiver, filter );

BroadCastReceiver 类

private final BroadcastReceiver mReceiver = new BroadcastReceiver() 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            String action = intent.getAction();

            // When discovery finds a device
            if ( BluetoothDevice.ACTION_FOUND.equals(action) ) 
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            }
        }
        catch ( Exception e )
        {
            logger.info( DateFormat.format( ConstantCodes.dateFormat ,new java.util.Date()).toString(),"Broadcast Error : " + e.toString(), ConstantCodes.SEARCH_ACTIVITY );
            System.out.println ( "Broadcast Error : " + e.toString() );
        }
    }
};

1
我想在用户每次点击按钮时获取连接到我的安卓手机的蓝牙设备列表。 - Priyank Patel
你说的“connected devices”指的是配对设备吗? - Lucifer
什么是连接到音频电话的意思? - Lucifer
我想获取状态为STATE_AUDIO_CONNECTED的蓝牙设备。 - Priyank Patel
好的,意味着您只想连接到类似耳机的音频蓝牙设备,是吗? - Lucifer
是的...但我不想连接...我只想找出已连接的音频蓝牙设备列表... - Priyank Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接