Skip to content
Kevin Babcock edited this page Dec 6, 2019 · 16 revisions

Motivation

Over the last five years, since Android 4.3 introduced Bluetooth LE compatibility, the same challenges persist when integrating with peripherals - dealing with the implicit serial nature of BluetoothGatt, implementing two Bluetooth LE scanners (one each for JellyBean and one for Lollipop), implementing retry logic to handle Android's dreaded GATT errors (133 ring a bell?), the list goes on.

Additionally, with the advent (or one may argue re-introduction) of reactive programming, a whole host of new challenges arise to create a reactive bridge between Android's imperative Bluetooth APIs and a consumer app based on RxJava.

For those tired of writing eerily similar, yet subtly different code for every Bluetooth LE peripheral, RxCentralBle provides a standardized, simple paradigm for connecting to and communicating with peripherals from the central role in a completely reactive manner.

Reactive Contract

RxCentralBle provides a fully reactive interface built using RxJava2. Underneath this interface is a series of subjects that interact directly with Android's Bluetooth API and take imperative actions as observers subscribe to them and dispose of those subscriptions.

Every action in RxCentralBle is triggered when you observe that action. To cancel an ongoing or queued action, dispose of your subscription. This is the core principal of RxCentralBle. This allows you to construct complex Observable streams of actions, relying on the internal subscription mechanisms of RxJava as you switch, retry, and ultimately capture results of operations taken on your peripheral.

Design

RxCentralBle Design

There is a series of common procedures that any central device (i.e. mobile device) must perform to successfully connect to and communicate with a peripheral:

  • Bluetooth State Detection - we must know the state of the Bluetooth Radio, and if the platform supports Bluetooth
  • Scanning - the central must scan for peripherals to connect to
  • Discovery - the central must handle discovery of eligible peripherals
  • Connection Request - the central must request a connection to a discovered peripheral
  • Connection Callbacks - the central must handle changes to connection state to the peripheral
  • GATT Operations - the central must be able to issue operations to the peripheral
  • GATT Callbacks - the central must handle results of GATT operations and handle peripheral-initiated notifications

RxCentralBle boils this down to four logical objects:

  • BluetoothDetector
    • Bluetooth State Detection
  • Scanner
    • Scanning, Discovery
  • ConnectionManager
    • Connection Request, Connection Callbacks
  • PeripheralManager
    • GATT Operations, GATT Callbacks