2
votes

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?

2
you are getting BLE connection drops during first attempt ?Gautam
Yes, Sometimes I am unable to connect during the first attempt, even though the BLE device is in range. And since it happens only sometimes, I am confused about what should and shouldn't be done regarding this.Saurabh
If BLE device is in range, it doesn't mean it will always connect. There can be many cases due to which connection was not made or it was made and it got dropped off instantly. So you have to come up with your own solution how you want to handle it. The way we tried in our application was we use to run a service and keeps trying to reconnect & once its done we just stop our service.Gautam
Yes, I've read that on multiple forums and I am using a thread much the same way to connect. But the documentation says that "gatt.connect()" is used to reconnect back to a BLE device. and the other one is used to initialise the connection. The question here is that of clarification of the documentation.Saurabh
can you provide link where its mentioned that connect() should be used for ble devices ? i cant find it anywhereGautam

2 Answers

1
votes

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.

0
votes

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.