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

Problem with killing process in Android

Hello !

I've created an app based on the nrf blinky master : it works perfectly on my smartphones. But when I kill the app, and when I relaunch it, it doesn't connect (or it connects, but in 2 minutes at least)

Do you know why, please ? I've been searching for five days, but I couldn't fix the problem.

I wonder that it it due to the GATT, which has to be reinitialized ?

  • Hi,

    I'm not sure if it's the case, but it may be related to a way how you call device.connectGatt(..., autoConnect, ...). The first call should be with autoConnect = false, as it's much faster. If you want to keep device connected you also connect with false, but then, when device disconnects, call gatt.connect() which will use autoConnect = true. This parameter set to true adds the device to a list in the low level controller, so the CPU doesn't have to be waken up every time an adv packet is received to check if we should, or should not connect. Controller is power optimized, so it may connect after some time. When the app is killed (the gatt.close() was called) the autoConnect should again be set to false so that next connection is quick.

    nRF Blinky wasn't updated for some time now, please check nRF Toolbox app, a specially the BleManager class (link). It makes sure that autoConnect is false on the first call.

    Hope it helped...

    Aleksander

  • Hello, Thanks for your answer. I change "mBluetoothGatt = device.connectGatt(mContext, autoConnect, getGattCallback());" to mBluetoothGatt = device.connectGatt(mContext, false, getGattCallback()); It connects quicker. BUt again, when I kill the app, it doesn't reconnect, until I restart manually the Bluetooth. Do you know what can be the problem ?

    EDIT : in facts, it reconnects, but after 2 minutes !

  • Hi, did you implement reconnecting on your own? As I wrote, when the app is killed the gatt.close() is called and it will no longer reconnect automatically (assuming autoConnect was true). When the app is launched again you have to call connectGatt again (with autoConnect = "is the app in fg"). Keep in mind, that when you device is not bonded nor has a public address and your phone was restarted, you have to scan for your device and an adv packet from it needs to be received at least once so that the system know what address type it has (public or private). By default it assumes a public address so calling

    BluetoothDevice device = new BluetoothDevice(savedAddress);
    ... = device.connectGatt(...);
    

    will fail, as it will try to connect to a public address. Check this: devzone.nordicsemi.com/.../

  • Yes : on the launching of the app, I do a scan, and I filter the devices by name, and I connect if the name is one in particular. Thanks, I'm reading it.

  • EDIT : When I re-launch the app, the board detect the connection quickly (1-2 seconds), but the app detects the connection after 1 or 2 minutes

Related