简体   繁体   English

我们可以像配对经典蓝牙设备一样以相同的方式配对BLE设备吗?

[英]Can we pair BLE Device the same way as we pair Classic Bluetooth Device?

I am using the same code for Bluetooth Low Energy (BLE) to pair the devices as I used for Classic bluetooth pairing. 我使用与Bluetooth Low Energy (BLE)相同的代码来配对设备,就像我用于Classic bluetooth配对一样。 I am not sure that the code will work for BLE or not as I am not having BLE device to test at the moment or later (only client can test). 我不确定代码是否适用于BLE,因为我现在或以后没有BLE设备可以测试(仅客户端可以测试)。

My code for Bluetooth Classic pairing - 我的蓝牙经典配对代码 -

@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();  
    }

Here each list item is the device that needs to be bonded. 在此,每个列表项都是需要绑定的设备。

So we click on the item and then it gets bonded by the above code. 因此,我们单击该项目,然后将其与上面的代码绑定。 ( This is for Classic Bluetooth Pairing ). 这是用于经典蓝牙配对的 )。

Does the same code work for BLE pairing ? BLE配对是否使用相同的代码?

I haven't found any pairing code for BLE devices or any online/official stuff related to their pairing. 我还没有找到BLE设备的任何配对代码或与其配对相关的任何在线/官方资料。 That's strange. 那很奇怪。

Bonding works mostly the same way for Bluetooth BR/EDR and the LE so you should be good. 对于蓝牙BR / EDR和LE,绑定的工作方式几乎相同,因此您应该不错。

In your code you can do just: 在您的代码中,您可以执行以下操作:

@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");
}

I'm not sure what the Class introspection and try/catch block is going to do for you. 我不确定Class内省和try / catch块将为您做什么。 And you cannot know the outcome of the bonding attempt until you get the ACTION_BOND_STATE_CHANGED event. 而且,直到获得ACTION_BOND_STATE_CHANGED事件,您才能知道绑定尝试的结果。 It's all described in the API. 所有这些都在API中进行了描述。

A bond is always performed after connecting and a BluetoothDevice object isn't necessarily connected. 绑定始终在连接后执行,并且不一定连接BluetoothDevice对象。 So you can do a device.connectGatt(...) call first and then call createBond after receiving the onConnectionStateChange event. 因此,您可以先执行device.connectGatt(...)调用,然后在接收到onConnectionStateChange事件后调用createBond。 But I do believe the device will attempt to connect just by calling createBond. 但是我确实相信该设备将仅通过调用createBond尝试进行连接。

Android implements BLE, by now is supported acting as a central mode which means that if you want to pair like BT classic the pairing request implementation should be implemented on the peripheral target remote BLE device that you want to connect, BLE process is scanning for BLE services, connecting and enable notifications and read/write characteristics. Android实施BLE,目前已作为中心模式受支持,这意味着,如果您想像BT classic一样配对,则配对请求实现应在要连接的外围目标远程BLE设备上实施,BLE进程正在扫描BLE服务,连接并启用通知和读/写特性。 Hope to help, regards 希望能对您有所帮助

You can bond with BLE device only in special bonding mode (but device can keep working in that mode all the time, my wild guess). 您只能在特殊的绑定模式下与BLE设备绑定(但我的猜测是设备可以一直在该模式下工作)。 Read device manual to figure out this. 阅读设备手册以解决此问题。 After bonding you can connect to device as Gatt-client (you can also create Gatt-server and device will connect to your server as Gatt-clien if provided) 绑定后,您可以作为Gatt客户端连接到设备(也可以创建Gatt服务器,并且设备将作为Gatt-clien连接到服务器)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM