RxAndroidBLE - 简单读取和显示1值

时间:2017-07-18 23:43:20

标签: android bluetooth-lowenergy rxandroidble

我对Android Studio完全不熟悉。我的简单目标是从我的BLE112中读取一个值并将其显示在应用程序中。我正在使用Android Studio 2.4 Preview 7.我已经采用了RxAndroidBLE的官方示例代码并将其缩减,但仍无法进行编译。任何帮助/建议将不胜感激。

提及我的最终目标可能是相关的,即以1000个样本/秒的速度将这些值提供给RealTime图。我已经使用了MPAndroidCharts的RealTime图。只需要值......

这些是当前的错误,但我相信还有更多......

错误:(21,52)错误:包com.trello.rxlifecycle.android.ActivityEvent不存在 错误:(48,41)错误:找不到符号变量PAUSE

package com.example.monik.rx5;

import android.support.v7.app.AppCompatActivity;

import android.bluetooth.BluetoothGattCharacteristic;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.polidea.rxandroidble.RxBleConnection;
import com.polidea.rxandroidble.RxBleDevice;
import com.polidea.rxandroidble.RxBleClient;
import com.polidea.rxandroidble.utils.ConnectionSharingAdapter;

import java.util.UUID;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subjects.PublishSubject;

import com.trello.rxlifecycle.android.ActivityEvent.PAUSE;

public class MainActivity extends AppCompatActivity {

    private static String macAddress = "00:07:80:F2:92:D8";
    private UUID characteristicUuid;
    private PublishSubject<Void> disconnectTriggerSubject = PublishSubject.create();
    private Observable<RxBleConnection> connectionObservable;
    private RxBleDevice bleDevice;
    private RxBleClient bleClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        characteristicUuid = UUID.fromString("F90EA017-F673-45B8-B00B-16A088A2ED61");
        bleClient = RxBleClient.create(this);
        bleDevice = bleClient.getBleDevice(macAddress);
        connectionObservable = prepareConnectionObservable();
        onConnectToggleClick();
        onReadClick();
    }

    private Observable<RxBleConnection> prepareConnectionObservable() {
        return bleDevice
                .establishConnection(false)
                .takeUntil(disconnectTriggerSubject)
                .compose(bindUntilEvent(PAUSE))
                .compose(new ConnectionSharingAdapter());
    }

    public void onConnectToggleClick() {

        if (isConnected()) {
            triggerDisconnect();
        } else {
            connectionObservable
                    .flatMap(RxBleConnection::discoverServices)
                    .flatMap(rxBleDeviceServices -> rxBleDeviceServices.getCharacteristic(characteristicUuid))
                    .observeOn(AndroidSchedulers.mainThread())
                    //.doOnSubscribe(() -> connectButton.setText(R.string.connecting))
                    .subscribe(
                            characteristic -> {
                                Log.i(getClass().getSimpleName(), "Hey, connection has been established!");
                            },
                            this::onConnectionFailure,
                            this::onConnectionFinished
                    );
        }
    }

    public void onReadClick() {

        if (isConnected()) {
            connectionObservable
                    .flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(characteristicUuid))
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(bytes -> {
                        TextView tv = (TextView) findViewById(R.id.read_output);
                        tv.setText(new String(bytes));
                    }, this::onReadFailure);
        }
    }

    private boolean isConnected() {
        return bleDevice.getConnectionState() == RxBleConnection.RxBleConnectionState.CONNECTED;
    }

    private void onConnectionFailure(Throwable throwable) {
    }

    private void onConnectionFinished() {
    }

    private void onReadFailure(Throwable throwable) {
    }

    private void triggerDisconnect() {
    }

}

1 个答案:

答案 0 :(得分:0)

从错误判断,您的项目中没有RxLifecycle库。您绝对应该添加到build.gradle

compile 'com.trello:rxlifecycle:1.0'
compile 'com.trello:rxlifecycle-components:1.0'

与示例项目的build.gradle文件和其他使用过的dependencies进行比较。