Search code examples
androidbluetooth-lowenergyrxandroidble

Are there issues with caching / reusing a RxBleDevice instance?


RxBleClient#scanBleDevices emits a sequence of ScanResult objects. In turn, ScanResult#getBleDevice provides the RxBleDevice instance from one of these emitted item.

Are there any issues with caching a RxBleDevice instance to avoid having to perform future scans?

A couple of potential use cases:

  • A scan is performed well ahead of the need to actually connect to the device.
  • A general scan is performed for an unrelated operation, so it would be efficient to utilize any other devices emitted during this scan.

Solution

  • There are at least two options:

    • You can cache RxBleDevice instance yourself, as long as you keep the client instance. It internally keeps some references to objects scoped with the lifecycle of the client.
    • You can use RxBleClient#getBleDevice passing the MAC address. RxBleDevice instances are cached across the client and you shouldn't expect performance issues with creating the device.