我们可以像配对经典蓝牙设备一样配对BLE设备吗?

时间:2014-05-13 14:06:57

标签: android bluetooth-lowenergy android-bluetooth

我使用与Bluetooth Low Energy (BLE)相同的代码来配对我用于Classic bluetooth配对的设备。我不确定代码是否适用于BLE,因为我现在没有BLE设备进行测试(只有客户可以测试)。

我的蓝牙经典配对代码 -

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
        if (device == null) return;

        Boolean isBonded = false;
        try {
            isBonded = createBond(device);           
        } catch (Exception e) {
            e.printStackTrace(); 
        }
        Log.i("Log", "The bond is created: "+isBonded);
}

public boolean createBond(BluetoothDevice btDevice)  
    throws Exception  
    { 
        Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
        Method createBondMethod = class1.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    }

此处每个列表项都是需要绑定的设备。

所以我们点击该项目然后通过上面的代码绑定它。 (这适用于经典蓝牙配对)。

相同的代码是否适用于BLE配对?

我没有找到任何与BLE设备配对的代码或任何与其配对相关的在线/官方资料。这很奇怪。

3 个答案:

答案 0 :(得分:1)

对于蓝牙BR / EDR和LE,绑定的工作方式大致相同,所以你应该做得很好。

在您的代码中,您可以执行以下操作:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
    if (device == null) return;

    device.createBond();

    Log.i("Log", "The bonding started");
}

我不确定Class内省和try / catch块会为你做什么。在得到ACTION_BOND_STATE_CHANGED事件之前,您无法知道绑定尝试的结果。它都在API中描述。

连接后始终执行绑定,并且BluetoothDevice对象不必连接。因此,您可以先执行device.connectGatt(...)调用,然后在收到onConnectionStateChange 事件后调用createBond。但我相信设备只会通过调用createBond来尝试连接。

答案 1 :(得分:0)

Android实现BLE,现在支持充当中央模式,这意味着如果你想像BT经典一样配对请求实现应该在你要连接的外围目标远程BLE设备上实现,BLE过程是扫描BLE服务,连接并启用通知和读/写特性。希望有所帮助,关心

答案 2 :(得分:0)

你只能在特殊的粘合模式下与BLE设备绑定(但设备可以一直在该模式下工作,我猜测)。阅读设备手册以了解这一点。绑定后,您可以作为Gatt-client连接到设备(您也可以创建Gatt服务器,如果提供Gatt-clien,设备将连接到您的服务器)