1

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

提到我的最终目标可能是相关的,即将这些值以 1000 个样本/秒的速度输入实时图表。我已经有了与 MPAndroidCharts 一起使用的实时图。只需要价值观...

这些是当前的错误,但我很确定还有更多....

错误:(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() {
    }

}
4

1 回答 1

0

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

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

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

于 2017-07-19T09:08:07.547 回答