Fly*_*iew 3 android bluetooth bluetooth-lowenergy android-5.0-lollipop
我是低功耗蓝牙 (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;
}
Run Code Online (Sandbox Code Playgroud)
你可以使用这个:
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
List<BluetoothDevice> devices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
for(BluetoothDevice device : devices) {
if(device.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的手表确实已连接,我无法回答。可能是您的手表只宣传其状态,而手机正在监听此广告。这取决于它是如何实现的。
| 归档时间: |
|
| 查看次数: |
13046 次 |
| 最近记录: |