Android 获取已连接蓝牙设备的列表

Android 获取已连接蓝牙设备的列表,android,bluetooth,bluetooth-lowenergy,android-5.0-lollipop,Android,Bluetooth,Bluetooth Lowenergy,Android 5.0 Lollipop,我不熟悉蓝牙低能量(LE)API。是否有办法检查Android设备当前是否连接到任何蓝牙LE设备,并可能获得这些设备的列表 BT-LE设备真的“连接”了吗?我注意到,当我的智能手表与Nexus 5配对/连接时,状态栏(KitKat上)中的蓝牙图标不会像连接到经典蓝牙设备时那样变为白色/粗体 我使用下面的代码来制作经典设备。看起来我可以用同样的方法检查GATT和GATT_服务器,但它们总是返回断开连接的状态 private BluetoothAdapter getBTAdapter() {

我不熟悉蓝牙低能量(LE)API。是否有办法检查Android设备当前是否连接到任何蓝牙LE设备,并可能获得这些设备的列表

BT-LE设备真的“连接”了吗?我注意到,当我的智能手表与Nexus 5配对/连接时,状态栏(KitKat上)中的蓝牙图标不会像连接到经典蓝牙设备时那样变为白色/粗体

我使用下面的代码来制作经典设备。看起来我可以用同样的方法检查GATT和GATT_服务器,但它们总是返回断开连接的状态

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;
}

更新:现在我已经把安卓棒棒糖闪到我的Nexus5上了,我想这一定是可能的,因为它被用于SmartLock,并且它检测到我的BT-LE安卓手表正在连接

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;
}
private BluetoothAdapter getBTAdapter(){
if(android.os.Build.VERSION.SDK\u INT
您可以使用以下功能:

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) {
     ...
  }
}
BluetoothManager BluetoothManager=(BluetoothManager)mContext.getSystemService(Context.BLUETOOTH\u服务);
列出设备=bluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
用于(蓝牙设备:设备){
if(device.getType()==BluetoothDevice.device\u TYPE\u LE){
...
}
}

我无法回答你的手表是否真的连上了。可能是您的手表只显示其状态,而手机正在收听此广告。这取决于它是如何实现的。

您的BTLE设备可以在系统中用不同的配置文件注册。Android的API只公开了一些配置文件(GATT、GATT_服务器、耳机等)。但是,也有其他配置文件可以在系统中注册,例如输入设备。调用getConnectedDevices()仅适用于GATT和GATT_服务器配置文件,但如果您的设备注册为任何其他配置文件,则该方法将不起作用。

谢谢您的建议。我试过了,但不幸的是,它没有返回任何设备,即使智能手表已“连接”。我认为BT-LE设备不是一直“连接”的,而是来回通知设备……是的。这是可能的。永久保持连接会增加电池消耗,因此它会更好地设计为周期性的…所以现在我已经将Android棒棒糖闪存到我的Nexus 5上,我认为这一定是可能的,因为它用于SmartLock,它不知何故检测到我的BT-LE Android手表已连接。别忘了检查BluetoothDevice。DEVICE_TYPE_DUAL我也有一个类似的问题-上面的代码不返回任何内容,既不返回LE设备,也不返回普通设备,即使状态栏中的蓝牙图标显示设备已连接。可能是耳机模式(扬声器)?Fitbit也没有显示。