列出已连接的蓝牙设备?
如何列出 Android 上所有已连接的蓝牙设备?
谢谢!
How can I list all connected bluetooth devices on android ?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何列出 Android 上所有已连接的蓝牙设备?
谢谢!
How can I list all connected bluetooth devices on android ?
thanks!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
从 API 14(Ice Cream)开始,Android 有一些新的 BluetoothAdapter 方法,包括:
public int getProfileConnectionState (int profile)
其中 profile 是
HEALTH、HEADSET、A2DP
检查响应,如果不是
STATE_DISCONNECTED
,则说明您有实时连接。以下是适用于任何 API 设备的代码示例:
As of API 14 (Ice Cream), Android has a some new BluetoothAdapter methods including:
public int getProfileConnectionState (int profile)
where profile is one of
HEALTH, HEADSET, A2DP
Check response, if it's not
STATE_DISCONNECTED
you know you have a live connection.Here is code example that will work on any API device:
BluetoothAdapter
:如果蓝牙未打开,您可以使用文档中不推荐的
btAdapter.enable()
或要求用户执行此操作:以编程方式在 Android 上启用蓝牙未连接的设备):
第四,您创建一个
BluetoothProfile.ServiceListener
,其中包含连接服务时触发的两个回调
断开连接:
现在您必须重复查询 Android SDK 中所有可用蓝牙配置文件的过程 (A2Dp、GATT、GATT_SERVER、手机、健康、SAP),您应该按以下步骤操作:
在
onServiceConnected
中,放置一个条件来检查当前是什么配置文件,以便我们将找到的设备添加到正确的集合中,然后使用:proxy.getDevicesMatchingConnectionStates(states)
来过滤掉未连接的设备:最后,最后要做的事情是开始查询过程:
source :https://stackoverflow.com/a/34790442/2715054
BluetoothAdapter
:If the Bluetooth is not turned out you can either use
btAdapter.enable()
which is not recommended in the documentation or ask the user to do it : Programmatically enabling bluetooth on Androidunconnected devices):
Fourth, you create a
BluetoothProfile.ServiceListener
whichcontains two callbacks triggered when a service is connected and
disconnected :
Now since you have to repeat the querying process for all available Bluetooth Profiles in the Android SDK (A2Dp, GATT, GATT_SERVER, Handset, Health, SAP) you should proceed as follow :
In
onServiceConnected
, place a condition that check what is the current profile so that we add the found devices into the correct collection and we use :proxy.getDevicesMatchingConnectionStates(states)
to filter out unconnected devices:And finally, the last thing to do is start the querying process :
source: https://stackoverflow.com/a/34790442/2715054
这样您就可以获得配对设备的列表。
So you get the list of paired devices.
Android 系统不允许您查询所有“当前”连接的设备。但是,您可以查询配对的设备。您将需要使用广播接收器来侦听 ACTION_ACL_{CONNECTED|DISCONNECTED} 事件以及 STATE_BONDED 事件,以更新应用程序状态以跟踪当前连接的内容。
Android system doesn't let you query for all "currently" connected devices. It however, you can query for paired devices. You will need to use a broadcast receiver to listen to ACTION_ACL_{CONNECTED|DISCONNECTED} events along with STATE_BONDED event to update your application states to track what's currently connected.
我找到了一个解决方案,它适用于 android 10
Kotlin
中调用此方法
在主线程Java
原始代码
I found a solution and it works on android 10
Kotlin
Call this method in main thread
Java
original code
请分析此类
在这里您将了解如何发现所有已连接(配对)的蓝牙设备。
Please analyze this class online.
Here you will find how to discover all connected (paired) Bluetooth devices.
步骤如下:
首先,您开始意图发现设备
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
为其注册一个广播接收器:
registerReceiver(mReceiver, filter);
关于mReceiver的定义:
并且在发现新设备时会自动填充该列表。
Well here are the steps:
First, you start intent to discover devices
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
Register a broadcast reciver for it:
registerReceiver(mReceiver, filter);
On the definition of mReceiver:
and the list will be automatically populated on new device discovery.