Skip to content
Dariusz Seweryn edited this page Feb 18, 2020 · 2 revisions

Does this library support bonding?

TL;DR — Yes

Bonding is performed and managed by the Android Operating System. The OS presents the UI to the user if/when needed.

How bonding works?

There are three typical scenarios.

1. Protected characteristic access

  1. Connect to peripheral
  2. Perform service discovery
  3. Perform action on a characteristic that requires authorization

At this point peripheral returns Gatt status == 5 which means GATT_INSUF_AUTHENTICATION. This response triggers bonding process on the client see below OS Bonding UI. The response may or may not be returned by the OS to the library depending on system version / vendor. Successful bonding may or may not trigger automatic action retry — again depending on system version / vendor.

2. Peripheral's request

  1. Connect to peripheral
  2. Peripheral requests bonding

Simple as that. User is presented with OS Bonding UI (see below). To get info about bond state changes one needs to implement a BroadcastReceiver with BluetoothDevice.ACTION_BOND_STATE_CHANGED. As of library version 1.11.0 there is no convenience Observable for such occasion.

3. Central's request

  1. Central requests bonding

Since API 19 is possible to call BluetoothDevice.createBond(). It is discouraged though as bonding parameters like transport layer (BLE/Classic) will be decided by the OS which may pick a wrong one.

OS Bonding UI

Depending on OS version / vendor and supported bonding procedure user will be presented with native UI that is generally not controllable* by the application. Usually this UI involves a notification which user needs to click in order to bond.

Under some circumstances it was possible that Bonding UI will include a foreground popup. As of Android 10 this function cannot be found and BluetoothPairingService seems to only work with notifications.

* It is possible to hijack bonding in a way and alter default OS behaviour (version dependent) but these are out of scope of this library and can be implemented by you. YMMV.