Skip to content

FerrandTian/RxBle

Repository files navigation

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

Gradle settings

If you don't have this already, add it to your root build.gradle file:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Then you can add the dependency to your app build.gradle file:

dependencies {
    ...
    implementation 'com.github.FerrandTian:RxBle:1.1.0'
}

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.result.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.create(address)

Connect and discover services

ble.connect()   // JUST connect or reconnect

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

ble.connectWithServices().subscribe { }

Observe connection state on change

ble.connectionState().subscribe { }

Read and write Characteristic or Descriptor

This method is thread-safe. Use this method to execute write operation sequentially.

fun writeWithQueue(
    characteristic: BluetoothGattCharacteristic,
    value: ByteArray,
    writeType: Int = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT,
)
fun writeWithQueue(
    descriptor: BluetoothGattDescriptor,
    value: ByteArray,
)

NOT thread-safe.

ble.read(characteristic).subscribe { }
ble.write(
    characteristic, bytes, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
).subscribe { }
ble.reliableWrite(iterator).subscribe { }

ble.read(descriptor).subscribe { }
ble.write(descriptor, bytes).subscribe { }

Enable or disable notification

ble.setNotification(
    descriptor, BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE   // or disable value
).subscribe { }

Observe Characteristic on change

ble.characteristic(uuid).subscribe { }

Disconnect or close connection

ble.disconnectWithState().subscribe { }     // Call connect() to reconnect
ble.disconnect()     // Call connect() to reconnect

ble.close()     // Dispose all resources and close this Bluetooth GATT client

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.

About

RxBle: Use Android Bluetooth API in Rx way

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages