此站点上的许多帖子(例如 this、this 或 this)讨论了如何检测 Android 上丢失的蓝牙连接。本质上,一个人可以注册 Action 的广播
BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED
和BluetoothAdapter.ACTION_ACL_DISCONNECTED
。
但是,当有多个 slave 时,我如何知道其中哪个断开连接或即将断开连接?
编辑:
- 我的用例是一款多人游戏。
BluetoothSocket
类有一个方法isConnected()
,这看起来很有用,但它需要 API 级别 14 (Android 4.0) 或更高版本。
最佳答案
在 Android 中,您可以了解设备的状态。
您可以使用 android.bluetooth.BluetoothProfile
界面。它提供了一个名为 public abstract List<BluetoothDevice> getDevicesMatchingConnectionStates (int[] states)
的 API
它返回所有蓝牙设备的列表。您可以指定状态,例如 STATE_CONNECTED, STATE_CONNECTING, STATE_DISCONNECTED, STATE_DISCONNECTING
在参数数组中。
有关更多信息,请参阅:
http://developer.android.com/reference/android/bluetooth/BluetoothProfile.html#getDevicesMatchingConnectionStates(int[])
关于安卓蓝牙 : how to detect which slave is disconnected when there are multiple slaves?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666903/