All Projects → JuulLabs → Able

JuulLabs / Able

Licence: apache-2.0
Able: Android Bluetooth Low Energy library

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Able

Conduit
High Performance Streams Based on Coroutine TS ⚡
Stars: ✭ 135 (-14.01%)
Mutual labels:  coroutines
Co
Art of C++. Flag, logging, unit-test, json, go-style coroutine and more.
Stars: ✭ 2,264 (+1342.04%)
Mutual labels:  coroutines
Boson
A C++14 framework for asynchronous I/O, cooperative multitasking and green threads scheduling
Stars: ✭ 154 (-1.91%)
Mutual labels:  coroutines
Cppcoro
A library of C++ coroutine abstractions for the coroutines TS
Stars: ✭ 2,118 (+1249.04%)
Mutual labels:  coroutines
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (-8.92%)
Mutual labels:  coroutines
Ktarmor Mvvm
👻 Android快速开发框架, KtArmor 寓意着 为Android 赋予战斗装甲, 方便开发者快速进行Android 开发。
Stars: ✭ 148 (-5.73%)
Mutual labels:  coroutines
Ct Smart Home
A ready-to-use Node-RED setup for home automation
Stars: ✭ 132 (-15.92%)
Mutual labels:  bluetooth-low-energy
Easyble
Android BLE framework
Stars: ✭ 155 (-1.27%)
Mutual labels:  bluetooth-low-energy
Coredux
Opinionated Redux store implementation using Kotlin coroutines
Stars: ✭ 144 (-8.28%)
Mutual labels:  coroutines
Libwire
User space threading (aka coroutines) library for C resembling GoLang and goroutines
Stars: ✭ 149 (-5.1%)
Mutual labels:  coroutines
Ble.net
Cross-platform Bluetooth Low Energy (BLE) library for Android, iOS, and UWP
Stars: ✭ 137 (-12.74%)
Mutual labels:  bluetooth-low-energy
Fiber Ext
stackful-coroutines for PHP
Stars: ✭ 142 (-9.55%)
Mutual labels:  coroutines
Covid19radar
Open Source / i18n / iOS Android Cross Platform Contact Tracing App by exposure notification framework Xamarin App and Server Side Code
Stars: ✭ 35 (-77.71%)
Mutual labels:  bluetooth-low-energy
Retrofit Kotlin Coroutines Example
An example project to demonstrate how to use Retrofit with Kotlin Coroutines in Android
Stars: ✭ 135 (-14.01%)
Mutual labels:  coroutines
Flutter reactive ble
Flutter library that handles BLE operations for multiple devices.
Stars: ✭ 155 (-1.27%)
Mutual labels:  bluetooth-low-energy
Kotlinmultiplatform mvvm
Android & iOS App using MVVM pattern and LiveData on the presentation layer + Clean Arch on the common shared code.
Stars: ✭ 135 (-14.01%)
Mutual labels:  coroutines
Python Fibers
Lightweight cooperative microthreads for Python
Stars: ✭ 146 (-7.01%)
Mutual labels:  coroutines
Coolweather
Weather App that uses Android best practices. Android Jetpack, clean architecture. Written in Kotlin
Stars: ✭ 154 (-1.91%)
Mutual labels:  coroutines
Zewo
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.
Stars: ✭ 1,856 (+1082.17%)
Mutual labels:  coroutines
Bluetoothlinux
Pure Swift Linux Bluetooth Stack
Stars: ✭ 149 (-5.1%)
Mutual labels:  bluetooth-low-energy

DEPRECATED

Project has been deprecated, and superseded by Kable. If any needed features are available in Able but absent from Kable, please create an issue requesting the desired feature. Thank you.

Able

codecov

Provides a Kotlin Coroutines powered interaction with Android's Bluetooth Low Energy (BLE) framework.

See Recipes page for usage examples.

API

When feasible, the API closely matches the Android Bluetooth Low Energy API, replacing methods that traditionally rely on BluetoothGattCallback calls with suspension functions.

Android BluetoothDevice Able Device
fun connectGatt(
    context: Context,
    autoConnect: Boolean,
    callback: BluetoothGattCallback
): BluetoothGatt
suspend fun connectGatt(
    context: Context
): ConnectGattResult1

1 Suspends until STATE_CONNECTED or non-GATT_SUCCESS is received, or connectGatt returns null, then returns ConnectGattResult:

sealed class ConnectGattResult {

    data class Success(val gatt: Gatt) : ConnectGattResult()

    sealed class Failure : ConnectGattResult() {

        /** Connection could not be established (e.g. device is out of range). */
        data class Connection(val cause: Exception) : Failure()

        /** Android's `BluetoothDevice.connectGatt` returned `null` (e.g. BLE off or unsupported). */
        data class Rejected(val cause: Exception) : Failure()
    }
}
Android BluetoothGatt Able Gatt
fun disconnect(): Boolean
suspend fun disconnect(): Unit2
fun discoverServices(): Boolean
suspend fun discoverServices(): GattStatus3
fun getServices(): List
val services: List
fun getService(uuid: UUID): BluetoothGattService
fun getService(uuid: UUID): BluetoothGattService
fun readCharacteristic(
    characteristic: BluetoothGattCharacteristic
): Boolean
suspend fun readCharacteristic(
    characteristic: BluetoothGattCharacteristic
): OnCharacteristicRead3
fun writeCharacteristic(
    characteristic: BluetoothGattCharacteristic
): Boolean
suspend fun writeCharacteristic(
    characteristic: BluetoothGattCharacteristic,
    value: ByteArray,
    writeType: WriteType
): OnCharacteristicWrite3
fun writeDescriptor(
    descriptor: BluetoothGattDescriptor
): Boolean
suspend fun writeDescriptor(
    descriptor: BluetoothGattDescriptor,
    value: ByteArray
): OnDescriptorWrite3
fun requestMtu(mtu: Int): Boolean
suspend fun requestMtu(mtu: Int): OnMtuChanged3
fun readRemoteRssi(): Boolean
suspend fun readRemoteRssi(): OnReadRemoteRssi3
fun setCharacteristicNotification(
    characteristic: BluetoothGattCharacteristic,
    enable: Boolean
): Boolean
fun setCharacteristicNotification(
    characteristic: BluetoothGattCharacteristic,
    enable: Boolean
): Boolean

2 Suspends until STATE_DISCONNECTED or non-GATT_SUCCESS is received, then calls close() on underlying BluetoothGatt.
3 Throws RemoteException if underlying BluetoothGatt call returns false.
3 Throws GattResponseFailure if an error occurs while waiting for response (e.g. connection is lost).

Details

The primary entry point is the BluetoothDevice.connectGatt(context: Context): ConnectGattResult extension function. This extension function acts as a replacement for Android's BluetoothDevice.connectGatt(context: Context, autoConnect: Boolean, callback: BluetoothCallback): BluetoothGatt? method (which relies on a BluetoothGattCallback). The autoConnect parameter is not configurable (and is always false).

Prerequisites

Able expects that Android Bluetooth Low Energy is supported (BluetoothAdapter.getDefaultAdapter() returns non-null) and usage prerequisites (e.g. bluetooth permissions) are satisfied prior to use; failing to do so will result in RemoteException for most Able methods.

Structured Concurrency

During the connectGatt and disconnect process, Able will ensure that connections are cleaned up (i.e. close will always be called on the underlying BluetoothGatt) in the event of failure or Coroutine cancellation:

fun connect(context: Context, device: BluetoothDevice) {
    val job = launch {
        device.connectGatt(context)
    }

    launch {
        delay(1_000L) // Assume, for this example, that BLE connection takes more than 1 second.

        // Cancels the `launch` Coroutine and automatically closes the underlying `BluetoothGatt`.
        job.cancel()
    }
}

Note that in the above example, if the BLE connection takes less than 1 second, then the established connection will not be cancelled and result will be ConnectGattResult.Success.

Once a connection is established (connectGatt returns ConnectGattResult.Success) then it will remain connected until disconnect() is called. It is the responsibility of the caller to clean up the connection when no longer needed (via disconnect).

Setup

Gradle

Maven Central

To use Able in your Android project, setup your build.gradle as follows:

repositories {
    jcenter() // or mavenCentral()
}

dependencies {
    implementation "com.juul.able:core:$version"
}

Packages

Able provides a number of packages to help extend it's functionality:

Package Functionality
keep-alive Provides a keep-alive GATT (which automatically reconnects when connection is lost).
processor A Processor adds the ability to process (and optionally modify) GATT data
pre-write or post-read.
throw Adds extension functions that throw exceptions on failures for various BLE
operations.
timber-logger Routes Able logging through Timber.

License

Copyright 2020 JUUL Labs, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].