RxBle: Use Android Bluetooth API in Rx way

Overview

RxBle: Use Android Bluetooth API in Rx way

A lightweight encapsulation of Android Bluetooth API.

  • Use Android Bluetooth API in Rx way.
  • Support multiple Bluetooth device connection.
  • Async error handling.

Preview

Preview_1 Preview_2

Init

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        RxBleManager.init(this)
    }
}

Scan

RxBleManager.instance.scan(
    null,
    ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build()
)   // Original scan parameters are supported
.filter { it.device.name != null }  // Use Rx filter function
.takeUntil(Observable.timer(10, TimeUnit.SECONDS)) // Unsubscribe after 10 seconds
.observeOn(AndroidSchedulers.mainThread())
.subscribe { }

Create

val ble: RxBle = RxBleManager.instance.obtain(address)

Connect and discover services

ble.connect()   // JUST connect or reconnect

ble.connectWithState()
.timeout(8, TimeUnit.SECONDS) // Set 8 seconds timeout
.retry(2).filter { it == BluetoothProfile.STATE_CONNECTED }  // Retry twice until STATE_CONNECTED
.firstOrError().flatMap { ble.discoverServices() }  // Combining with discoverServices
.subscribe { }

ble.connectWithServices().subscribe { }

Observe connection state on change

ble.connectionState().subscribe { }

Read and write Characteristic or Descriptor

ble.read(characteristic).subscribe { }
ble.write(characteristic).subscribe { }
ble.read(descriptor).subscribe { }
ble.write(descriptor).subscribe { }

Split ByteArray before write when length exceeds mtu.

fun write(
    values: Iterable<ByteArray>, 
    characteristic: BluetoothGattCharacteristic
) { }
fun reliableWrite(
    values: Iterable<ByteArray>,
    characteristic: BluetoothGattCharacteristic
) { }

Enable or disable notification

ble.setNotification(descriptor).subscribe(object : SingleObserver<BluetoothGattDescriptor> {
    override fun onSubscribe(d: Disposable) {
        descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE // or disable value
    }
    override fun onSuccess(t: BluetoothGattDescriptor) {}
    override fun onError(e: Throwable) {}
})

Observe Characteristic on change

ble.characteristic(uuid).subscribe { }

LICENSE

Copyright (C) 2022 TianFeng

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.
You might also like...
A non-trivial Bluetooth LE app using Kable and app architecture best practices

kable_mvvm_demo The intention of this project is to demonstrate a non-trivial Bluetooth LE app using Kable and app architecture best practices. ⚠️ The

BluePass extracts two factor authentication codes (2FA) from SMS and sends them to a paired device via Bluetooth RFCOMM.
BluePass extracts two factor authentication codes (2FA) from SMS and sends them to a paired device via Bluetooth RFCOMM.

BluePass extracts two factor authentication codes (2FA) from SMS and sends them to a paired device via Bluetooth RFCOMM.

Open-source weight and body metrics tracker, with support for Bluetooth scales
Open-source weight and body metrics tracker, with support for Bluetooth scales

Open-source weight and body metrics tracker, with support for Bluetooth scales

Simple bluetooth flutter project

bluetooth_simple Simple bluetooth implementation. Getting Started This project is a starting point for a Flutter application. A few resources to get y

The purpose is to share the Internet capability of one device to the entire Bluetooth LAN.
The purpose is to share the Internet capability of one device to the entire Bluetooth LAN.

bluenet The purpose is to share the Internet capability of one device to the entire Bluetooth LAN. To make a prototype of a soft bus, or actually, I w

User-friendly Lightweight TPM Remote Attestation over Bluetooth

Ultrablue Ultrablue (User-friendly Lightweight TPM Remote Attestation over Bluetooth) is a solution to allow individual users to perform boot state at

Allows Android apps to interact with BLE beacons

Android Beacon Library An Android library providing APIs to interact with beacons. Please visit the project website for how to use this library. IMPOR

A simple, lightweight library intended to take away some of the cruft and tediousness of using the Android BLE.

Blueteeth What Is Blueteeth? Blueteeth is a simple, lightweight library intended to take away some of the cruft and tediousness of using the Android B

🍔 Meals is a small demo app based on modern Android technologies and MVVM architecture
🍔 Meals is a small demo app based on modern Android technologies and MVVM architecture

Meals 🍔 Meals is a small demo app based on modern Android technologies and MVVM architecture. built-in Kotlin, Coroutine, Flow, Retrofit, and Jetpack

Owner
null
[UNMAINTAINED][Android] Bluetooth Serial Port Profile which comfortable to developer application to communication with microcontroller via bluetooth

⚠ WARNING: This project is no longer being maintained Android-BluetoothSPPLibrary Bluetooth Serial Port Profile which comfortable to developer applica

Akexorcist 1.7k Dec 31, 2022
Android Bluetooth Helper Library, Bluetooth Device Finder

Bluetooth Helper Allows you to access the Bluetooth of your mobile device, manage turn-on - turn off, and discover bluetooth devices around you. Getti

Tolga Bolatcan 44 Jul 15, 2022
An Android Library for handling Bluetooth Low Energy on Android Easy

An Android Library for handling Bluetooth Low Energy on Android Easy

Leandro SQ 42 Jan 3, 2023
An Android library that solves a lot of Android's Bluetooth Low Energy problems

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.

Nordic Semiconductor 1.4k Jan 7, 2023
Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino.

Android Smooth Bluetooth Smooth communication via bluetooth with other android devices or microcontrollers such as Arduino. Getting Started Add Gradle

Mantas Palaima 191 Nov 28, 2022
A reactive, interface-driven central role Bluetooth LE library for Android

RxCentralBle RxCentralBle provides a simple reactive paradigm for connecting to and communicating with Bluetooth LE peripherals from the central role.

Uber Open Source 198 Nov 29, 2022
A Bluetooth kotlin multiplatform "Cross-Platform" library for iOS and Android

Blue-Falcon A Bluetooth "Cross Platform" Kotlin Multiplatform library for iOS, Android, MacOS, Raspberry Pi and Javascript. Bluetooth in general has t

Andrew Reed 220 Dec 28, 2022
BLESSED Coroutines, a Bluetooth Low Energy (BLE) library for Android using Kotlin Coroutines

BLESSED for Android with Coroutines - BLE made easy BLESSED is a very compact Bluetooth Low Energy (BLE) library for Android 8 and higher, that makes

Martijn van Welie 82 Jan 1, 2023
This library allows for easy access to a Bluetooth LE device's AdRecord and RSSI value. It offers additional functionality for iBeacons.

Bluetooth LE Library for Android This library allows for easy access to a Bluetooth LE device's Advertisement Records. It also offers: A simple runnin

Alexandros Schillings 843 Dec 13, 2022
Kotlin Asynchronous Bluetooth Low-Energy

Kable Kotlin Asynchronous Bluetooth Low Energy provides a simple Coroutines-powered API for interacting with Bluetooth Low Energy devices. Usage is de

JUUL Labs 493 Dec 25, 2022