I am trying to subscribe to events on my photon. Even though I can successfully get the photon’s ParticleDevice object, calling subscribeToDeviceEvents does nothing and the ParticleHandler is never invoked.
I’ve run particle subscribe mine on the command-line and am sure there are currently firing events from my device, even though they are not picked up by the SDK.
My code:
final ParticleCloud cloud = ParticleCloudSDK.getCloud();
Async.executeAsync(cloud, new Async.ApiWork<ParticleCloud, Object>() {
@Override
public Object callApi(ParticleCloud particleCloud) throws ParticleCloudException, IOException {
return particleCloud.subscribeToDeviceEvents(null, particle_id, new ParticleEventHandler() {
@Override
public void onEvent(String eventName, ParticleEvent particleEvent) {
// This never happens!
Log.d("TAG", "Event! " + eventName);
}
@Override
public void onEventError(Exception e) {
e.printStackTrace();
}
});
}
@Override
public void onSuccess(Object o) {
}
@Override
public void onFailure(ParticleCloudException exception) {
}
});
}
Making the events PUBLIC did not make any difference. I get the following log output indicating a subscription does happen:
D/EventsDelegate: Created event subscription with ID 1 for URI https://api.spark.io/v1/devices/{id}
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.streamOpened
Looks like I’m having the same issue on Android. The link in the log output gives me this:
{
"error": "invalid_request",
"error_description": "The access token was not found"
}
Looks like somehow my instance of ParticleDevice doesn’t have any knowledge of the access token.
I completed the login via ParticleCloud successfully prior to attempting to subscribe.
The cli sees my events.
Anyone able to shed light on this?
These calls are being made from the Android ParticleCloudSDK, which should handle access tokens for us, hence why ahmedghoneim92 and I are asking for help. There is no obvious way to add an access token to a request because, in theory, the ParticleCloud should be doing this for us.
@MrAnderson & @ahmedghoneim92, I have already forwarded your problem to Particle, but being a weekend, you might need to wait for a reply till office hours.
I’ve tried several different approaches… and I can’t repro this bug.
@MrAnderson thanks for the offer, I’d love the assist. Can you (or anyone else interested) clone the Cloud SDK repo (https://github.com/spark/spark-sdk-android) and try out the example app? Try adding the following in the example app’s LoginActivity, in the Async block, after where logIn() and getDevice() get called:
(Please excuse the goofy “BANANA” logging tag. You have to admit though, it is unique.)
I’ve tried the above code, and it works just as expected: the event is published, and then received by the subscriber. If anyone can try this out and report back their results, I’d really appreciate it!
I submitted a pull request that fixes this issue. Turns out that “/events” was getting left off of the URI for single device subscriptions. Therefore subscribing to all devices worked, but subscribing to individual ones didn’t.
It is also worth noting that subscriptions need to happen asynchronously. I was just sort of firing off a sub at first expecting it to work, but it didn’t work until I wrapped the whole thing in an observable and subscribed to it. Obviously that was just the solution I chose. Async.executeAsync should work just as well.