简体   繁体   中英

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)

The documentation just above this method specifies that this is used to initiate a connection with BLE devices.

However, the official Android documentation states that to connect with a BLE device boolean connect() should be used.

The documentation for this connect() method states that this is used to reconnect back to a device.

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.)

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.

Above method is the Bluetooth Gatt method that will help you connect with ble device . after successful connection , BluetoothGatt will call BluetoothGattCallback , that have different override methods .

As per My implementation , I discovered the device using BluetoothAdapter.LeScanCallback that is used for lower version. 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(). then again make connection using above code.

My answer at Which correct flag of autoConnect in connectGatt of BLE? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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