Android-NestDK无法初始化恒温器对象

Android-NestDK cannot initial thermostat object

我想制作自己的恒温器APP。


我的方法

  1. 参考MainActivity,我新建activity实现NestAPI.AuthenticationListener,Listener.ThermostatListener
  2. 从 MainActivity 复制 implements 方法和 authenticate 方法的内容

在 运行 之后,由于 logcat 中的消息 "Authentication succeeded.",身份验证成功。但是,Thermostat 对象 mThermostat 仍然为 null,无法获取其中的信息。

p.s。如果复制 MainActivity 的 mthermostat 设备 ID,我可以修改目标温度。


有谁知道如何初始化恒温器对象?


我在尝试写一个简单的例子上传到这里时解决了这个问题:I

我发现我必须添加 fetchData() 来设置恒温器的侦听器,然后 mthermostat 可以初始化并在 onThermostatUpdated(@NonNull Thermostat thermostat) 中更新。


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.nestapi.codelab.demo.R;
import com.nestapi.codelab.demo.Settings;
import com.nestapi.lib.API.*;

public class Activity4Test extends Activity implements
    NestAPI.AuthenticationListener, Listener.ThermostatListener {

    private Listener mUpdateListener;
    private NestAPI mNestApi;
    private Thermostat mThermostat;
    private AccessToken mToken;

    TextView txvGet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_4test);
        mNestApi = NestAPI.getInstance();//Initial NestAPI, connect Firebase
        mToken = Settings.loadAuthToken(this);//Loade AccessToken
        authenticate(mToken);

        txvGet = (TextView) findViewById(R.id.txvGet);
    }

    private void authenticate(AccessToken token) {
        Log.v("Activity4Test", "Authenticating...");
        NestAPI.getInstance().authenticate(token, this);
    }

    @Override
    public void onAuthenticationSuccess() {
        Log.v("Activity4Test", "Authentication succeeded.");
        fetchData();
    }

    @Override
    public void onAuthenticationFailure(int errorCode) {
        Log.v("Activity4Test", "Authentication failed with error: " + errorCode);
    }

    private void fetchData() {
        Log.v("Activity4Test", "Fetching data...");

        mUpdateListener = new Listener.Builder()
                .setThermostatListener(this)
                .build();
        mNestApi.addUpdateListener(mUpdateListener);
    }

    @Override
    public void onThermostatUpdated(@NonNull Thermostat thermostat) {
        Log.v("Activity4Test", String.format("Thermostat (%s) updated.", thermostat.getDeviceID()));
        mThermostat = thermostat;
    }