Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the App doesn't reconnect to the BLE device when set autoConnect to true in Android?

I am develop in Android and BLE. I want the App automatic reconnect to the BLE device after the BLE device disconnect but come back in the range and advertising.

I use the following code to connect to the BLE device:

public void connect(final String address) {
        // TODO Auto-generated method stub
        Log.w(TAG, "BluetoothLeService Connect function.");
        if(mBluetoothAdapter == null || address == null){
            Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
            //return false;
        }

        final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
        mBluetoothGatt = device.connectGatt(this, true, mGattCallback);

    }

I have set the AutoConnect to the true , but it didn't reconnect when the BLE device has disconnect and come back in the range.

Why the App doesn't reconnect to the BLE device when set autoConnect to true in Android?

Did I missing something ?

Thanks in advance.

like image 423
Wun Avatar asked Nov 19 '14 12:11

Wun


1 Answers

The auto connect parameter determines whether to actively connect to the remote device (or) rather passively scan and finalize the connection when the remote device is in range.

But this does not mean that a peripheral that's been disconnected for days then reappears will be reconnected.

Generally, the first ever connection to a device should be direct (autoConnect set to false) and subsequent connections to known devices should be invoked with the autoConnect parameter set to true.

Also please note, the auto connect will only work when the device is still broadcasting. If not, then it will not work.

I would prefer that you re-connect manually when the device is disconnected. If in case you do end up following this, you would need a marker to determine whether the device was actually disconnected without the user consent.

If true then unbind/unregister your service/broadcast receiver and connect again using the device address which you must have saved previously.

like image 143
Vikram Ezhil Avatar answered Sep 26 '22 20:09

Vikram Ezhil