Android BLE example

Viewed 4557

I tried to look for complete examples that could help me with the development of the app but on the net I found only parts of code not linked to each other. Is it possible to see a complete example of connection including the communication part between the 2 devices? Also I happen to have a problem with the nRF Connect app: the bluetooth automatically disconnects from the device after about 30 seconds, why?

2 Answers

BLE complete example link to github - contains Android, iOS, ESP32 - 2 apps for each platform, Central and Peripheral. Each Central is compatible with each Peripheral.

This is a great guide: The Ultimate Guide to Android Bluetooth Low Energy by PunchThrough.

About your second question (that automatic disconnection occurs), I remember I've noticed similar disconnections on iOS, but didn't find out the reason. I also noticed that after subscribing to a characteristic notifications/indications it's not disconnecting anymore, that's why I guess it could be operating system optimization for the inactive connections.

Just an idea about the reason of unexpected disconnections: some Peripherals can be strict to connection parameters, and disconnect when the phone asks for changing them too many times.

In my case it was iPhone and Nordic nRF51822 (sample project from SDK 12.3.0, with modified settings to have low latency). Nordic (Peripheral) was disconnecting after about 30 seconds (4th time iPhone asked to change connection params), and it was a feature of that project, implemented this way:

static void conn_params_init(void)
{
    ble_conn_params_init_t cp_init;
    ...
    // allow to update 3 times max, disconnect on the 4th
    cp_init.max_conn_params_update_count = 3;
    ...
}

// after about 30 seconds it was receiving this call
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{
    ...
    if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
    {
        // and disconneting right here
        sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
    }
}

So that time I fixed it on Periperal side by ignoring event BLE_CONN_PARAMS_EVT_FAILED and not calling sd_ble_gap_disconnect, but proper setup of those connection parameters would be a better fix.

Related