Java Android NestDK无法初始化自动调温器对象
我想创建我自己的恒温器应用程序Java Android NestDK无法初始化自动调温器对象,java,android,nest-api,Java,Android,Nest Api,我想创建我自己的恒温器应用程序 我的方法 参考MainActivity,我创建新的活动实现NestAPI.AuthenticationListener、Listener.ThermostatListener 从MainActivity复制implements方法和authenticate方法的内容 运行后,由于logcat中的消息“Authentication Successed.”,身份验证成功。但是,恒温器对象mThermostat仍然为null,无法获取其中的信息 p、 如果复制Mai
我的方法
运行后,由于logcat中的消息“Authentication Successed.”,身份验证成功。但是,恒温器对象mThermostat仍然为null,无法获取其中的信息 p、 如果复制MainActivity的mthermostat设备ID,我可以修改目标温度
有人知道如何初始化恒温器对象吗
当我试图写一个简单的例子上传到这里时,我解决了这个问题:I 我发现我必须添加
fetchData()
来设置恒温器的侦听器,然后mthermostat
可以是初始值并在onThermostatUpdated(@NonNull恒温器)
中更新
我对android不太了解,对恒温器也不太了解,但我相信你需要展示一些你的代码。谢谢你的推荐!当我试图写一个简单的例子时,我解决了这个问题。哈哈,非常感谢:)很高兴听到这个!这种情况经常发生:)
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;
}