Search code examples
androidkotlinbluetooth-lowenergyandroid-blerxandroidble

How can I write to a BLE GATT characteristic using RxAndroidBle in Kotlin?


I am unable to use the sample code to write to a GATT characteristic over BLE. I'm using the following code from Polidea's examples available here: https://github.com/Polidea/RxAndroidBle

Code:

rxBleDevice.establishConnection(false) .flatMapSingle(rxBleConnection - > rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite)) .subscribe( characteristicValue - > { // Characteristic value confirmed. }, throwable - > { // Handle an error here. } );

IDE Errors:

enter image description here

enter image description here

App Level Gradle:


apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

apply plugin: 'realm-android'

apply plugin: 'com.google.gms.google-services'

ext.versionMajor = 1
ext.versionMinor = 4
ext.versionPatch = 2
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 19

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "ca.hooplight.hooplight"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode generateVersionCode()
        versionName generateVersionName()
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

private Integer generateVersionCode() {
    return ext.minimumSdkVersion * 10000000 + ext.versionMajor * 10000 + ext.versionMinor * 100 + ext.versionPatch
}

private String generateVersionName() {
    String versionName = "${ext.versionMajor}.${ext.versionMinor}.${ext.versionPatch}"
    if (ext.versionClassifier == null && ext.isSnapshot) {
        ext.versionClassifier = "SNAPSHOT"
    }

    if (ext.versionClassifier != null) {
        versionName += "-" + ext.versionClassifier
    }
    return versionName;
}

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {

    /*implementation "com.github.parse-community.Parse-SDK-Android:parse:1.20.0"
    // for FCM Push support (optional)
    implementation "com.github.parse-community.Parse-SDK-Android:fcm:1.20.0"
    // for Kotlin extensions support (optional)
    implementation "com.github.parse-community.Parse-SDK-Android:ktx:1.20.0"*/

    /* Multidex */
    implementation 'com.android.support:multidex:1.0.3'

    /* Firebase */
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.google.firebase:firebase-core:17.2.1'
    implementation 'com.google.firebase:firebase-auth:19.1.0'
    implementation 'com.google.firebase:firebase-database:19.2.0'

    /* Polidea */
    implementation "com.polidea.rxandroidble2:rxandroidble:1.10.3"
    implementation "io.reactivex.rxjava2:rxjava:2.2.8"

    implementation 'com.jakewharton:butterknife:10.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
    compile 'io.realm:android-adapters:2.0.0'
    compile "org.jetbrains.anko:anko-commons:0.10.0"
    implementation 'com.ramijemli.percentagechartview:percentagechartview:0.3.0'
    // implementation "com.polidea.rxandroidble2:rxandroidble:1.8.1"
    // implementation "io.reactivex.rxjava2:rxjava:2.2.7"
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    compile 'com.github.woxthebox:draglistview:1.6.2'
    compile 'com.joanzapata.iconify:android-iconify-fontawesome:2.2.2' // (v4.5)
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    compile "org.jetbrains.kotlin:kotlin-reflect:1.3.50"
}

Solution

  • .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
    

    is Java syntax. Use Kotlin's instead:

    .flatMapSingle { rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite) }