我知道如何获取已配对设备列表,但如何确定它们是否已连接?
这肯定是可能的,因为我在手机的蓝牙设备列表中看到它们并且状态显示它们是否连接。
我知道如何获取已配对设备列表,但如何确定它们是否已连接?
这肯定是可能的,因为我在手机的蓝牙设备列表中看到它们并且状态显示它们是否连接。
将Bluetooth权限添加到您的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 应用程序。下面的解决方案适用于我。
Kotlin:
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
。 - Juhadevice.bluetoothClass.hasService(BluetoothClass.Service.AUDIO)=false
,三星Galaxy Watch4在这个通话中也显示为耳机。我正在尝试找出如何确定是否有任何支持音频的连接设备,但 isConnected
调用仅限于 SystemApi。目前,我可以看到所有已配对的设备,但无法确定它们是否已连接 - 因此我无法确定是否真正连接了耳机或者只是手表(即我有未连接的耳机)。有人有解决办法吗? - notmystyle在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
是否连接会得到不同的结果? - Gumby The Greenval m: Method = device.javaClass.getMethod("isConnected")
和 val connected = m.invoke(device)
。 - Gumby The Greenfun isConnected(device: BluetoothDevice): Boolean {
return try {
val m: Method = device.javaClass.getMethod(
"isConnected"
)
m.invoke(device) as Boolean
} catch (e: Exception) {
throw IllegalStateException(e)
}
}
- Takeya由于某些原因,Android Studio无法解析BluetoothAdapter.ACTION_ACL_CONNECTED。也许它在Android 4.2.2中已经被弃用了?
下面是对Skylarsutton代码的修改(非常感谢Skylarsutton提供的答案)。注册代码相同,接收器代码略有不同。我将其用于一个服务中,该服务更新一个连接的蓝牙标志,其他部分的应用引用该标志。
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...
}
};
BluetoothAdapter.getDefaultAdapter().isEnabled
-> 当蓝牙开启时返回 true。
val audioManager = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.isBluetoothScoOn
-> 当设备连接时返回 true。
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(BluetoothProfile.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
}