【发布时间】:2014-12-17 03:31:53
【问题描述】:
我是蓝牙低功耗 (LE) API 的新手。有没有办法检查 Android 设备当前是否连接到任何蓝牙 LE 设备并可能获取这些设备的列表?
BT-LE 设备是否真的“连接”了?我注意到当我的智能手表与我的 Nexus 5 配对/连接时,蓝牙图标在状态栏中(在 KitKat 上)没有变成白色/粗体,就像连接到经典蓝牙设备时一样。
我将以下代码用于经典设备。看起来我可以以相同的方式检查 GATT 和 GATT_SERVER,但它们总是返回断开连接。
更新:既然我已经将 Android Lollipop 刷到了我的 Nexus 5,我认为它一定是可能的,因为它用于 SmartLock,并且它以某种方式检测到我的 BT-LE Android 手表已连接。
private BluetoothAdapter getBTAdapter() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
return BluetoothAdapter.getDefaultAdapter();
else {
BluetoothManager bm = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
return bm.getAdapter();
}
}
public boolean isBluetoothConnected() {
if(mBluetoothAdapter == null ||
(mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_DISCONNECTED
&& mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.A2DP) == BluetoothProfile.STATE_DISCONNECTED
&& mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEALTH) == BluetoothProfile.STATE_DISCONNECTED)
) {
Utils.logDebug(TAG, "GATT_SERVER " + mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.GATT_SERVER));
Utils.logDebug(TAG, "GATT " + mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.GATT));
return false;
}
return true;
}
【问题讨论】:
标签: android bluetooth bluetooth-lowenergy android-5.0-lollipop