简体   繁体   English

如何使用 Kotlin 中的 RxAndroidBle 写入 BLE GATT 特性?

[英]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.我无法使用示例代码通过 BLE 写入 GATT 特征。 I'm using the following code from Polidea's examples available here: https://github.com/Polidea/RxAndroidBle我正在使用 Polidea 示例中的以下代码: 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: IDE 错误:

在此处输入图像描述

在此处输入图像描述

App Level Gradle:应用级别 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"
}
.flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))

is Java syntax.是 Java 语法。 Use Kotlin's instead:改用 Kotlin 的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM