我对蓝牙低功耗(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;
}