遇到一个检查蓝牙设备连接状态问题,困扰许久。在网上查询良久,尝试了多种方案,也没有很好的解决,最终经“高人指点”,小有所获,在此小记。
网上查询的主要方法为以下两种:
1.getProfileConnectionState()
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
int state = adapter .getProfileConnectionState(int profile)
调用BluetoothAdapter 中的,getProfileConnectionState()方法通过检查是否有使用
HEALTH
HEADSET
A2DP
三种profile的设备存在来判断当前是否存在设备连接。详细内容请参考下文
android 获取蓝牙各种连接状态
2.getConnectionState()
Class<BluetoothAdapter> bluetoothAdapterClass = BluetoothAdapter.class;
try {
Method method = bluetoothAdapterClass.getDeclaredMethod("getConnectionState", (Class[]) null);