Skip to content
Joe Soultanis edited this page Nov 8, 2019 · 1 revision

Motivation

Since iOS 5 and Android 4.3 first introduced Bluetooth LE compatibility, the same challenges persist when integrating with peripherals - dealing with the implicit serial nature of BLE GATT, implementing retry logic to handle connection and GATT errors, using outdated development patterns (delegate pattern in 2019?)...the list goes on.

Additionally, with the advent and expansion of reactive programming, a whole host of new challenges arise to create a reactive bridge between iOS and Android's imperative Bluetooth APIs and a consumer app based on RxSwift and RxJava.

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

Similarly to the Rx languages, RxCBCentral + RxCentralBLE allow mobile engineers who work on different platforms to use similar protocols and speak the same language, enabling increased developer efficiency and easy of achieving platform parity implementations.

Reactive Contract

RxCBCentral provides a fully reactive interface built using RxSwift 4.5 (at the moment). Underneath this interface is a series of subjects that interact directly with iOS's CoreBluetooth and take imperative actions as observers subscribe to them and dispose of those subscriptions.

Every action in RxCBCentral is triggered when you observe that action. To cancel an ongoing or queued action, dispose of your subscription. This is a core principal of RxCBCentral. This allows you to construct complex Observable streams of actions, relying on the internal subscription mechanisms of RxSwift as you flatMapLatest, 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
Clone this wiki locally