Android检查蓝牙是否已连接 [英] Android check if Bluetooth connected
本文介绍了Android检查蓝牙是否已连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在互联网上搜索了很长一段时间,但是找不到我想要的东西.
I am searching for this on the internet for quite a while, but I can't find what I am looking for.
如果我的设备已经连接到蓝牙设备(/在我启动我的应用程序之前),我该如何找到我的应用程序.
How can I find out with my app, if my device is already connected to a Bluetooth device (/ was before I start my app).
我希望有类似 bool BluetoothAdapter.isPaired();
推荐答案
如果仅对建立与任意蓝牙设备的连接感兴趣,则可以使用BluetoothAdapter.getProfileConnectionState(profile):
If you are only interested if a connection to an arbitrary bluetooth device is established you can use the BluetoothAdapter.getProfileConnectionState(profile):
adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
int[] profiles = {BluetoothProfile.A2DP, BluetoothProfile.HEADSET, BluetoothProfile.HEALTH};
boolean connectionExists = false;
for (int profileId : profiles) {
if (BluetoothAdapter.getProfileConnectionState(profileId) ==
BluetoothProfile.STATE_CONNECTED) {
connectionExists = true;
break;
}
}
}
这篇关于Android检查蓝牙是否已连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文