Android 如何使用蓝牙耳机 API 获取蓝牙连接设备
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12509135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get bluetooth connected devices using BluetoothHeadset API
提问by Priyank Patel
I want to get list of bluetooth connected devices...not just paired devices.
我想获得蓝牙连接设备的列表......不仅仅是配对设备。
I found BluetoothHeadset
API in API level 11which provides method getConnectedDevices()
to get list of connected bluetooth devices.
我BluetoothHeadset
在API 级别 11 中找到了API,它提供getConnectedDevices()
了获取已连接蓝牙设备列表的方法。
How to get list of bluetooth connected devices using this API ?
如何使用此 API 获取蓝牙连接设备列表?
回答by Priyank Patel
Finally got the solution. Below are a few code snippets for getting Bluetooth audio connected devices using BluetoothHeadset
API.
终于得到了解决方案。下面是一些使用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);
回答by Lucifer
First of all you need to define Permission in Androindmanifest.xml
首先你需要在 Androindmanifest.xml 中定义 Permission
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
Search Connected Devices Activity,
搜索连接的设备活动,
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 Class
广播接收器类
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() );
}
}
};