This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

(Android) Connecting to device with high advertisement interval

Hi,

I'm currently working on a BLE device which works his whole lifetime on one battery. This means we have to save as much energy as possible. This is why we set our advertisement interval to 3 seconds.

Using the Nordic Connect app this causes no problem, even though scanning takes a while, connecting always completes in about five seconds.

Yet, when developing our own app we experience the same scanning duration but connecting actually takes incredibly long or doesn't happen at all.

Our implementation looks like this:

public boolean connect(final String address) {
    long lStartTime = System.currentTimeMillis();
    Log.d(TAG, "connect: " + address);
    if (mBluetoothAdapter == null || address == null) {
        return false;
    }

    //	Previously connected device.Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mBluetoothGatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
        }
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    // get bluetooth device object based on MAC address //
    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        return false;
    }

    mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
    if (mBluetoothGatt == null) return false;

    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;

    long lEndTime = System.currentTimeMillis();
    long output = lEndTime - lStartTime;
    Log.d(TAG + " (connect())", "Elapsed time in milliseconds: " + output);

    return true;
}

I've noticed that you provide a library, is this also used in the Connect app? What is exactly the difference in using the default Android connect() and the one used in Connect?

Thanks!

Related