AbleDevice and storage

AbleDevice - the AbleLib currency

One of the goals of AbleLib SDK is to make your codebases across all platforms as similar as possible. One of the ways this is done is by making sure classes you work with are equally named and as similar in structure as the platform differences permit.

AbleDevice represents an external Bluetooth-enabled device your app interacts with. On Android, it wraps a BluetoothDevice, while on iOS a CBPeripheral. All AbleLib work with AbleDevices only. E.g, the scan method returns a Set of AbleDevices on all platforms. Similarly, AbleComm takes in an AbleDevice and drives the communication with it.

AbleDeviceStorage

AbleDeviceStorage allows for permanent storage for devices you've interacted with. The storage is local and ensures that all stored devices are distinct. The storage is updated whenever an operation on it is performed, or when a new device is paired (Android) or connected to (iOS). On top of this, iOS temporarily stores all discovered devices in order to make use of the OS-level caching that speeds up future connections and communication.

Get all the devices with devices:

val devices: Set<AbleDevice> = AbleDeviceStorage.default.devices

Find a stored AbleDevice by providing any of the usual AbleDevice properties, such as name or address (Android) / identifier (iOS):

val device = AbleDeviceStorage[myDevice]
val otherDevice = AbleDeviceStorage.default.findByAddress("address")

You can update the storage yourself: add new devices with add and remove them with remove:

AbleDeviceStorage.default.add(AbleDevice("My device", "address"))
AbleDeviceStorage.default += AbleDevice("Another device")

val success = AbleDeviceStorage.default.remove("address")
AbleDeviceStorage.default.remove(myAbleDevice)
AbleDeviceStorage.default -= AbleDevice("Another device")

Pro tip: Use AbleManager scanForStoredDevices method to filter your scans to stored devices only.