0

I am trying to connect my android device to BLE device. I am using the following code to do it.

 mBluetoothGatt = mDevice.connectGatt(BaseApplication.getContext(), true, getInstance());

You can see the second parameter as true.In docs it is called as autoconnect flag. I can keep it true as well as false. What is the use of that flag ? Only thing that I have observed is that when I use autoconnect as true it connects for sure but with delay, if I use false it does not connect each time,sometime it does and sometime it does not. Also I would like to know is this the correct way to connect a saved device each time ? or is it only after scanning for first time and there is some other way to do it ? Thanks :)

Pritish
  • 1,284
  • 1
  • 19
  • 42

2 Answers2

0

You can see the second parameter as true.In docs it is called as autoconnect flag. I can keep it true as well as false. What is the use of that flag ?

whenever your remote device is become visible its automatically connected if is it true.

Also I would like to know is this the correct way to connect a saved device each time ?

The best way to connect save device You need to store the Bluetooth Device Address (AB:CD:EF:01:02:03) of the device in for example shared preferences or in a sqlite db. Then use https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String) when you later restart the app to retrieve a BluetoothDevice object. Once you have the BluetoothDevice object you can use the connectGatt method as usual.

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

Most of the question has already been answered in Which correct flag of autoConnect in connectGatt of BLE?.

For your second question, see Android save BLE device to reconnect after app close.

Emil
  • 16,784
  • 2
  • 41
  • 52