2017-07-18 45 views
1

我完全是Android Studio的新手。我的簡單目標是從我的BLE112中讀取一個值並將其顯示在應用程序中。我使用的是Android Studio 2.4 Preview 7.我已經拿到了RxAndroidBLE的官方示例代碼,並將其裁剪下來,但仍然無法編譯。任何幫助/建議將不勝感激。RxAndroidBLE - 簡單的讀取和顯示1值

提到我的最終目標是將這些值以1000採樣/秒的速度送入實時圖。我已經有了與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() { 
    } 

} 
+0

我投票結束這個問題作爲題外話,因爲它屬於[代碼審查](https://codereview.stackexchange.com/) – G5W

回答

0

從錯誤來看,你在你的項目中沒有RxLifecycle庫。你一定要添加到您的build.gradle

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

與樣本項目的build.gradle文件進行比較和其他使用dependencies

+0

謝謝,這是非常有幫助的。我會嘗試。 – user3286543