am a new to particle cloud api and am unable to read the variable i set in my .ino file i upload to the electron to get sensor values. I need to read the value in android studio and use it in my app.
i used the particle android sdk example from github as reference to implement it in my project. Everything works fine through the android app, i can log in particle cloud, see device info and description, but am unable to read variable values from the electron.
here’s my code:
package com.example.footy;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import java.io.IOException;
import java.util.Map;
import io.particle.android.sdk.cloud.ParticleCloud;
import io.particle.android.sdk.cloud.exceptions.ParticleCloudException;
import io.particle.android.sdk.cloud.ParticleCloudSDK;
import io.particle.android.sdk.cloud.ParticleDevice;
import io.particle.android.sdk.utils.Async;
import io.particle.android.sdk.utils.Toaster;
public class ValueActivity extends AppCompatActivity {
private static final String ARG_VALUE = "ARG_VALUE";
private static final String ARG_DEVICEID = "ARG_DEVICEID";
private TextView tv, tv2;
private ParticleDevice device;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_value);
tv = findViewById(R.id.value);
tv.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));
tv2 = findViewById(R.id.value2);
//tv2.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));
findViewById(R.id.refresh_button).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v){
// Do network work on background thread
Async.executeAsync(ParticleCloudSDK.getCloud(), new Async.ApiWork<ParticleCloud, Integer>() {
@Override
public Integer callApi(@NonNull ParticleCloud ParticleCloud) throws ParticleCloudException, IOException {
device = ParticleCloud.getDevice(getIntent().getStringExtra(ARG_DEVICEID));
int variable;
try {
variable = device.getIntVariable("distance");
Toaster.l(ValueActivity.this, variable + " cm");
} catch (ParticleDevice.VariableDoesNotExistException e) {
Toaster.l(ValueActivity.this, e.getMessage());
variable = -1;
}
return variable;
}
@Override
public void onSuccess(@NonNull Integer i) { // this goes on the main thread
tv.setText(String.valueOf(i));
}
@Override
public void onFailure(@NonNull ParticleCloudException e) {
e.printStackTrace();
}
});
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_value, menu);
return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_info) {
Intent intent = DeviceInfoActivity.buildIntent(ValueActivity.this, getIntent().getStringExtra(ARG_DEVICEID));
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
public static Intent buildIntent(Context ctx, Integer value, String deviceId) {
Intent intent = new Intent(ctx, ValueActivity.class);
intent.putExtra(ARG_VALUE, value);
intent.putExtra(ARG_DEVICEID, deviceId);
return intent;
}
}
what am i missing? the variable name is the same as in the code, but i get the error “variable distance does not exists on the device”
please help out