Hey guys, so i ran the example app and it worked fine. I added an extra textview to the ValueActivity to display a message if an event is sent. This is my source code below. Currently i am having trouble with subscribing to the event as the textview does not change once the event occurs. I checked on the cloud, the event is occuring and it is public. Any help would be good.
Thanks,
Chuck
public class ValueActivity extends AppCompatActivity {
private static final String ARG_VALUE = "ARG_VALUE";
private static final String ARG_DEVICEID = "ARG_DEVICEID";
private static String TAG;
private TextView tv;
private TextView testing;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_value);
tv = findViewById(R.id.value);
testing = (TextView)findViewById(R.id.test);
tv.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));
testing.setText(String.valueOf(getIntent().getIntExtra(ARG_VALUE, 0)));
findViewById(R.id.refresh_button).setOnClickListener(v -> {
//...
// Do network work on background thread
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;
long subscriptionId; // save this for later, for unsubscribing
try {
variable = device.getDoubleVariable("charles");
} catch (ParticleDevice.VariableDoesNotExistException e) {
Toaster.l(ValueActivity.this, e.getMessage());
variable = -1;
}
return variable;
}
@Override
public void onSuccess(@NonNull Object i) { // this goes on the main thread
tv.setText(i.toString());
}
@Override
public void onFailure(@NonNull ParticleCloudException e) {
e.printStackTrace();
}
public String doInBackground(Void... params) {
try {
// Subscribe to an event
long subscriptionId = ParticleCloudSDK.getCloud().subscribeToDeviceEvents("beamStatus","------------",
new ParticleEventHandler() {
@Override
public void onEvent(String eventName, ParticleEvent particleEvent) {
//testing is the textview id
testing.setText("23443");
}
@Override
public void onEventError(Exception e) {
testing.setText("234455555");
Log.e(TAG, "Event error: ", e);
}
});
return "Subscribed!";
}
catch(IOException e) {
// We end up here if there was an error subscribing
Log.e(TAG, e.toString());
return "Error subscribing!";
}
}
});
});
}