我在Thermostat
对象中使用SmokeCOAlarm
对象尝试使用相同的初始和更新方法但不起作用。
有谁知道如何初始化和更新SmokeCOAlarm
对象?
或者Nest没有打开访问SmokeCOAlarm
对象?!
以下是我的代码:
public class COSmokeAlarm extends Activity implements
NestAPI.AuthenticationListener, Listener.SmokeCOAlarmListener, Listener.ThermostatListener {
private Listener mUpdateListener;
private NestAPI mNestApi;
private SmokeCOAlarm mSmokeCOAlarm;
private Thermostat mThermostat;
private AccessToken mToken;
TextView txvBattery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_co_smoke_alarm);
txvBattery = (TextView)findViewById(R.id.txvBattery);
mNestApi = NestAPI.getInstance();//Initial NestAPI, connect Firebase
mToken = Settings.loadAuthToken(this);//Loade AccessToken
authenticate(mToken);
}
private void updateBatteryTextView() {
if (mSmokeCOAlarm != null) {
txvBattery.setText(mSmokeCOAlarm.getBatteryHealth());
}
}
private void authenticate(AccessToken token) {
Log.v("COSmokeAlarm", "Authenticating...");
NestAPI.getInstance().authenticate(token, this);
}
@Override
public void onAuthenticationSuccess() {
Log.v("COSmokeAlarm", "Authentication succeeded.");
fetchData();
}
@Override
public void onAuthenticationFailure(int errorCode) {
Log.v("COSmokeAlarm", "Authentication failed with error: " + errorCode);
}
private void fetchData() {
Log.v("COSmokeAlarm", "Fetching data...");
mUpdateListener = new Listener.Builder()
.setSmokeCOAlarmListener(this)
.setThermostatListener(this)
.build();
mNestApi.addUpdateListener(mUpdateListener);
Toast.makeText(COSmokeAlarm.this, "Success fetching data.", Toast.LENGTH_SHORT).show();
}
@Override
public void onSmokeCOAlarmUpdated(@NonNull SmokeCOAlarm smokeCOAlarm) {
Log.v("COSmokeAlarm", String.format("COSmoke Alarm (%s) updated.", smokeCOAlarm.getDeviceID()));
this.mSmokeCOAlarm = smokeCOAlarm;
updateBatteryTextView();
}
@Override
public void onThermostatUpdated(@NonNull Thermostat thermostat) {
Log.v("COSmokeAlarm", String.format("Thermostat (%s) updated.", thermostat.getDeviceID()));
mThermostat = thermostat;
}
}