Particle SDK for Android Not Subscribing to Events

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) {

            }
        });
    }

`

Are you publishing on the Photon as PUBLIC (default) or PRIVATE?
Just try the other :wink:

You say

Are you sure (as in "I'm sure, but have no proof") or has CLI given you clear evidence?

I’m publishing the events as PRIVATE. I’ll try making them PUBLIC, but I need private events to work as well :smile:

Yes the CLI is printing events out, none of which are firing on the Android app.

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?

That error “invalid_request” means you are failing to include the access token in the request. It should look something like access_token=XXXXXX

A bad or unknown token would look like this:

{
  "error": "invalid_token",
  "error_description": "The access token provided is invalid."
}

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.

Thanks for that. I’ll be digging into the source for a little bit to see if I can maybe find the error myself. Will update here.

@MrAnderson

You’re correct, the SDK should definitely be handling those tokens for you. I’ll take a look tonight or tomorrow at the latest.

1 Like

Any updates on this? I happen to work as an Android developer and I thought we might be able to collaborate. Let me know :smiley:

Hi all,

I’ve tried several different approaches… and I can’t repro this bug. :confused:

@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:


    sparkCloud.subscribeToMyDevicesEvents(null, new ParticleEventHandler() {
        @Override
        public void onEvent(String eventName, ParticleEvent particleEvent) {
            Log.i("BANANA", "onEvent: " + eventName + particleEvent);
        }

        @Override
        public void onEventError(Exception e) {
            Log.e("BANANA", "OH NOES, onEventError: ", e);
        }
    });


    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Async.executeAsync(sparkCloud, new ApiProcedure<ParticleCloud>() {
                @Override
                public Void callApi(ParticleCloud particleCloud)
                        throws ParticleCloudException, IOException {

                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Log.i("BANANA", "callApi: PUBLISH");
                    particleCloud.publishEvent("BANANA_EVENT", "banana_time",
                            ParticleEventVisibility.PRIVATE, 300);

                    return null;
                }

                @Override
                public void onFailure(ParticleCloudException exception) {

                }
            });
        }
    });

(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.

1 Like

Fixed in release 0.3.3, thanks to a patch from @MrAnderson

Thanks, everyone!

I’m currently having this problem on my photon. I was wondering if anyone could give me some insights into this issue.