NestDK无法初始化更新SmokeCOAlarm对象

时间:2016-01-26 04:20:10

标签: java android nest-api

我在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;
}
}

1 个答案:

答案 0 :(得分:1)

转到Nest帐户并添加权限“Smoke + CO alarm read v4” enter image description here

然后我们可以访问SmokeCOAlarm信息。 enter image description here