RxAndroidBle Documentation

repository·master·Indexed 25 days ago

https://github.com/dariuszseweryn/rxandroidble

A reactive library for Android that simplifies Bluetooth Low Energy (BLE) operations using RxJava. It manages asynchronous operations such as reading, writing, and notifications, while handling threading and error handling. The library supports RxJava 2 and RxJava 3, provides a MockRxAndroidBle client for unit testing, and includes guidance on Android Manifest permissions for BLE scanning and location across various API levels.

Tokens
6.5K
Snippets
14
Records
22
Agent score
36%

What's inside RxAndroidBle

  1. Request Bluetooth enablement

    master

    The library does not manage the BluetoothAdapter state. To request the user to enable Bluetooth via a native Android activity, use an Intent with BluetoothAdapter.ACTION_REQUEST_ENABLE.

    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    int REQUEST_ENABLE_BT = 1;
    context.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  2. Install RxAndroidBle via Maven

    master

    To include RxAndroidBle in your project using Maven, add the dependency for either the RxJava 3 or RxJava 2 artifact. Note that the type is aar.

    <!-- For RxJava 3 -->
    <dependency>
      <groupId>com.polidea.rxandroidble3</groupId>
      <artifactId>rxandroidble</artifactId>
      <version>1.19.1</version>
      <type>aar</type>
    </dependency>
    
    <!-- For RxJava 2 -->
    <dependency>
      <groupId>com.polidea.rxandroidble2</groupId>
      <artifactId>rxandroidble</artifactId>
      <version>1.19.1</version>
      <type>aar</type>
    </dependency>
  3. Install MockRxAndroidBle

    master

    You can add MockRxAndroidBle to your project using Maven or Gradle. This library allows you to mock a Bluetooth LE device for testing purposes with the RxAndroidBle library.

    ### Gradle
    ```groovy
    implementation "com.polidea.rxandroidble2:mockclient:1.19.1"

    Maven

    <dependency>
      <groupId>com.polidea.rxandroidble2</groupId>
      <artifactId>mockclient</artifactId>
      <version>1.19.1</version>
      <type>aar</type>
    </dependency>
  4. Test RxAndroidBle in Unit tests

    master

    You can test your application using RxAndroidBle in two ways:

    1. Mocking Interfaces: Most library objects implement interfaces, allowing you to use standard mocking frameworks (like Mockito) to mock them.
    2. MockRxAndroidBle: You can use MockRxAndroidBle for testing. Note that using MockRxAndroidBle in unit tests requires Robolectric.
  5. Configure Android Manifest permissions for BLE scanning and location

    master

    RxAndroidBle provides a default set of permissions, but you may need to customize your AndroidManifest.xml depending on whether you want to derive the user's location or scan in the background.

    To derive user location across all API versions

    Use these tags to ensure location permissions are handled correctly during manifest merging and across different Android versions:

    <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" tools:node="remove" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" tools:node="remove" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    To scan without deriving user location

    If you only want to scan for peripherals without accessing location otherwise, restrict the permissions:

    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" tools:node="replace" />
    <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" tools:node="replace" />

    To scan in the background (APIs 29 & 30)

    Add the background location permission:

    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" android:maxSdkVersion="30" />

    Note: For APIs > 30, remove the android:maxSdkVersion attribute to access location in the background.

  6. Configure Android Manifest permissions for API 31+ (Android 12)

    master

    Android 12 (API 31) introduced new Bluetooth permissions. RxAndroidBle uses neverForLocation by default to avoid requiring location permissions for scanning.

    To derive user location via BLE scanning on API 31+

    If you specifically need to locate the user using BLE scanning, you must replace the default scanning permission and keep ACCESS_FINE_LOCATION:

    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" tools:node="replace" />

    To remove Bluetooth Connect permission

    If your app only needs to scan and does not need to connect to peripherals, you can remove the connection permission:

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" tools:node="remove" />
  7. View usage examples in Sample projects

    master

    The repository contains sample implementations in both Java and Kotlin to demonstrate how to use the library. Note that these samples are intended for library usage demonstration and may not represent ideal application architecture.

    • Java Samples: Located in /sample/src/main/java/com/polidea/rxandroidble2/sample
    • Kotlin Samples: Located in /sample-kotlin/src/main/kotlin/com/polidea/rxandroidble2/samplekotlin