I am trying to read a variable every 5 seconds. So far I can read the variable on a button press but I want to continuously read the variable every 5 seconds without a button press.
Here is what I have so far:
protected void readRange(){
Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Object>() {
@Override
public Object callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException {
ParticleDevice device = ParticleCloud.getDevice(getIntent().getStringExtra(ARG_DEVICEID)); Object variable;
try { variable = device.getDoubleVariable("range");
} catch (ParticleDevice.VariableDoesNotExistException e) { Toaster.l(ReadSensorActivity.this, e.getMessage()); variable = -1; }
return variable; }
@Override public void onSuccess(@NonNull Object i) { // this goes on the main thread range.setText(i.toString()+" in"); }
@Override public void onFailure(@NonNull ParticleCloudException e) { e.printStackTrace(); } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read_sensor); range = (TextView) findViewById(R.id.range); button = (Button) findViewById(R.id.readButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { readRange(); } }); }