【发布时间】:2011-01-17 17:13:45
【问题描述】:
我了解如何获取已配对设备的列表,但如何判断它们是否已连接?
这一定是可能的,因为我看到它们列在我手机的蓝牙设备列表中,并说明了它们的连接状态。
【问题讨论】:
我了解如何获取已配对设备的列表,但如何判断它们是否已连接?
这一定是可能的,因为我看到它们列在我手机的蓝牙设备列表中,并说明了它们的连接状态。
【问题讨论】:
将蓝牙权限添加到您的 AndroidManifest,
<uses-permission android:name="android.permission.BLUETOOTH" />
然后使用意图过滤器收听ACTION_ACL_CONNECTED、ACTION_ACL_DISCONNECT_REQUESTED 和ACTION_ACL_DISCONNECTED 广播:
public void onCreate() {
...
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
... //Device found
}
else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
... //Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
... //Done searching
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
... //Device is about to disconnect
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
... //Device has disconnected
}
}
};
几点说明:
【讨论】:
在我的用例中,我只想查看是否为 VoIP 应用连接了蓝牙耳机。以下解决方案对我有用。
科特林:
fun isBluetoothHeadsetConnected(): Boolean {
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
return (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled
&& mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED)
}
Java:
public static boolean isBluetoothHeadsetConnected() {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()
&& mBluetoothAdapter.getProfileConnectionState(BluetoothHeadset.HEADSET) == BluetoothHeadset.STATE_CONNECTED;
}
当然你需要蓝牙权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
【讨论】:
getProfileConnectionState(BluetoothHeadset.HEADSET) 调用显示错误“调用需要可能被用户拒绝的权限:代码应显式检查权限是否可用(使用 checkPermission)或显式处理潜在的 SecurityException”。所需的权限是Manifest.permission.BLUETOOTH_CONNECT。
device.bluetoothClass.hasService(BluetoothClass.Service.AUDIO)=false。我试图弄清楚如何确定是否有任何连接的设备支持音频,但isConnected 调用仅是 SystemApi。目前我可以看到所有绑定的设备,但无法判断它们是否已连接 - 所以我无法确定耳机是否真的连接或只是手表(即我有未连接的耳机)。有人对此有解决方案吗?
由于某种原因,Android Studio 无法解析 BluetoothAdapter.ACTION_ACL_CONNECTED。也许它在 Android 4.2.2 中已被弃用?
这是对 Skylarsutton 代码的修改(非常感谢Skylarsutton for his answer。)。注册码相同;接收器代码略有不同。我在一项服务中使用它来更新应用程序其他部分引用的蓝牙连接标志。
public void onCreate() {
//...
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(BTReceiver, filter);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Do something if connected
Toast.makeText(getApplicationContext(), "BT Connected", Toast.LENGTH_SHORT).show();
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Do something if disconnected
Toast.makeText(getApplicationContext(), "BT Disconnected", Toast.LENGTH_SHORT).show();
}
//else if...
}
};
【讨论】:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothDevice.java中的BluetoothDevice系统API中有一个isConnected函数。
如果您想知道当前是否连接了有界(配对)设备,以下功能对我来说很好:
public static boolean isConnected(BluetoothDevice device) {
try {
Method m = device.getClass().getMethod("isConnected", (Class[]) null);
boolean connected = (boolean) m.invoke(device, (Object[]) null);
return connected;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
【讨论】:
bluetoothManager.getConnectionState(device, BluetoothProfile.GATT) == BluetoothProfile.STATE_CONNECTED是否有不同的结果?
val m: Method = device.javaClass.getMethod("isConnected") 和 val connected = m.invoke(device)。
fun isConnected(device: BluetoothDevice): Boolean { return try { val m: Method = device.javaClass.getMethod( "isConnected" ) m.invoke(device) as Boolean } catch (e: Exception) { throw IllegalStateException(e) } }
此代码适用于耳机配置文件,可能也适用于其他配置文件。
首先您需要提供一个配置文件监听器(Kotlin 代码):
private val mProfileListener = object : BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
if (profile == BluetoothProfile.HEADSET)
mBluetoothHeadset = proxy as BluetoothHeadset
}
override fun onServiceDisconnected(profile: Int) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null
}
}
}
然后在检查蓝牙时:
mBluetoothAdapter.getProfileProxy(context, mProfileListener, BluetoothProfile.HEADSET)
if (!mBluetoothAdapter.isEnabled) {
return Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
}
调用 onServiceConnected 需要一些时间。之后,您可以从以下位置获取已连接耳机设备的列表:
mBluetoothHeadset!!.connectedDevices
【讨论】:
我真的在寻找一种方法来获取设备的连接状态,而不是监听连接事件。这对我有用:
BluetoothManager bm = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> devices = bm.getConnectedDevices(BluetoothGatt.GATT);
int status = -1;
for (BluetoothDevice device : devices) {
status = bm.getConnectionState(device, BLuetoothGatt.GATT);
// compare status to:
// BluetoothProfile.STATE_CONNECTED
// BluetoothProfile.STATE_CONNECTING
// BluetoothProfile.STATE_DISCONNECTED
// BluetoothProfile.STATE_DISCONNECTING
}
【讨论】:
BluetoothAdapter.getDefaultAdapter().isEnabled ->
蓝牙打开时返回 true。
val audioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.isBluetoothScoOn ->
当设备连接时返回 true
【讨论】: