如何使用 BluetoothHeadset API 获取蓝牙连接的设备
Posted
技术标签:
【中文标题】如何使用 BluetoothHeadset API 获取蓝牙连接的设备【英文标题】:How to get bluetooth connected devices using BluetoothHeadset API 【发布时间】:2012-09-20 08:49:32 【问题描述】:我想获取蓝牙连接设备的列表...而不仅仅是配对设备。
我在 API 级别 11 中找到了BluetoothHeadset
API,它提供了方法 getConnectedDevices()
来获取连接的蓝牙设备列表。
如何使用此 API 获取蓝牙连接的设备列表?
【问题讨论】:
【参考方案1】:终于解决了。下面是一些使用BluetoothHeadset
API 获取蓝牙音频连接设备的代码 sn-ps。
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);
【讨论】:
这是一个很好的解决方案..正如上面评论中所说,恭喜;) 我正在使用类似的解决方案,但是当我通过后退按钮退出我的应用程序后尝试连接时,我看到了E/BluetoothA2dp﹕ Could not bind to Bluetooth A2DP Service with Intent act=android.bluetooth.IBluetoothA2dp cmp=com.android.bluetooth/.a2dp.A2dpService
。我确信这一定与没有正确清理有关,但由于我很小心地打电话给closeProfileProxy
,我不确定它可能是什么。这是你见过的吗?【参考方案2】:
首先你需要在 Androindmanifest.xml 中定义 Permission
<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() );
;
【讨论】:
我想获取连接到我的安卓手机的蓝牙设备列表...每次用户点击按钮时。 你打电话给谁connected devices
?你的意思是配对?
连接音频电话是什么意思?
我想获取状态为STATE_AUDIO_CONNECTED的蓝牙设备
好的,意思是你只想连接耳机之类的音频蓝牙设备,是吗?以上是关于如何使用 BluetoothHeadset API 获取蓝牙连接的设备的主要内容,如果未能解决你的问题,请参考以下文章