我想创建自己的恒温器APP。
运行后,身份验证成功,因为消息"身份验证成功。"在logcat中。但是,恒温器对象mThermostat仍为空,无法获取信息。
P.S。如果复制MainActivity的mthermostat设备ID,我可以修改目标温度。
有谁知道如何初始恒温器对象?
答案 0 :(得分:0)
当我尝试编写一个简单的示例来上传时,我解决了这个问题:我
我发现我必须添加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;
}