简体   繁体   English

如何首次与BLE设备连接(Android)

[英]How to connect with BLE device for first time (Android)

I was going through BluetoothGatt.java and found the method boolean connect(Boolean autoConnect, BluetoothGattCallback callback, Handler handler) 我正在研究BluetoothGatt.java,发现方法boolean connect(Boolean autoConnect,BluetoothGattCallback回调,Handler handler)

The documentation just above this method specifies that this is used to initiate a connection with BLE devices. 该方法上方的文档指定该方法用于启动与BLE设备的连接。

However, the official Android documentation states that to connect with a BLE device boolean connect() should be used. 但是,Android官方文档指出,要与BLE设备连接,应使用boolean connect()

The documentation for this connect() method states that this is used to reconnect back to a device. 此connect()方法的文档指出,该方法用于重新连接回设备。

I am confused here because gatt.connect() is sometimes unreliable (the callback for the device connected is not called even though the BLE device is in range but connects when I try to connect in the second or third attempt.) 我在这里感到困惑是因为gatt.connect()有时是不可靠的(即使BLE设备在范围内,连接的设备的回调也不会被调用,但是当我尝试进行第二次或第三次尝试连接时会连接。)

Would it be better to use the method mentioned earlier to increase connection chances during first connection attempt? 使用前面提到的方法来增加第一次连接尝试的机会会更好吗?

Can anyone share some information regarding this? 谁能分享一些有关此的信息?

However, the official Android documentation states that to connect with a BLE device Boolean connect() should be used. 但是,Android官方文档指出,要与BLE设备连接,应使用布尔值connect()。

Above method is the Bluetooth Gatt method that will help you connect with ble device . 以上方法是Bluetooth Gatt方法,可帮助您连接ble设备。 after successful connection , BluetoothGatt will call BluetoothGattCallback , that have different override methods . 成功连接后,BluetoothGatt将调用具有不同覆盖方法的BluetoothGattCallback

As per My implementation , I discovered the device using BluetoothAdapter.LeScanCallback that is used for lower version. 按照我的实现,我发现使用用于较低版本的BluetoothAdapter.LeScanCallback的设备。 After that :- 之后 :-

 private void addDeviceItem(BluetoothDevice device, int rssi) {
 String penAddress = device.getAddress();
mBluetoothLeService.connect(penAddress ); 
} 


public boolean connect(final String address) {
        if (mBluetoothAdapter == null || address == null) {
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        }

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        if (device == null) {
            Log.w(TAG, "Device not found.  Unable to connect.");
            return false;
        }
        // We want to directly connect to the device, so we are setting the autoConnect
        // parameter to false.
        mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
        // refreshDeviceCache(mBluetoothGatt);
        Log.d(TAG, "Trying to create a new connection.");
        mConnectionState = STATE_CONNECTING;
        return true;
    }

I will always connect with the device , after finish with Bluetooth , you have to disconnect with device by calling Gatt.disconnect(). 我将始终与设备连接,完成蓝牙后,您必须通过调用Gatt.disconnect()与设备断开连接。 then again make connection using above code. 然后再次使用上面的代码进行连接。

My answer at Which correct flag of autoConnect in connectGatt of BLE? 我的答案是BLE的connectGatt中的autoConnect哪个正确标志? should explain everything. 应该解释一切。

Basically, a "direct connect" has a higher duty of the scan window / interval than an "auto connect". 基本上,“直接连接”比“自动连接”具有更高的扫描窗口/时间间隔。 That's why an auto connect can take very long time if you have a long advertising interval on the peripheral. 因此,如果外围设备上的广告间隔较长,则自动连接可能会花费很长时间。

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

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