Android subscription event is getting called only once

Messages from photon are being published correctly and also the android output is displaying that it is receiving the messages, but the problem is that onEvent() in the handler is crapping out after being only called once or twice.
Any thoughts on this? I searched around the forums, docs, and github and haven’t been able to find out why.

Code:

private ParticleEventHandler statusHandler = new ParticleEventHandler() {
    @Override
    public void onEvent(String eventName, ParticleEvent event) {
        System.out.println("dataPayload=" + event.dataPayload);
    }

    @Override
    public void onEventError(Exception e) {
        e.printStackTrace();
    }
};


Async.executeAsync(myDevice, new Async.ApiProcedure<ParticleDevice>() {
        @Override
        public Void callApi(ParticleDevice particleDevice) throws ParticleCloudException, IOException {
          
            particleDevice.subscribeToEvents("status", statusHandler);
            
            return null;
        }

        @Override
        public void onFailure(ParticleCloudException exception) {
            exception.printStackTrace();
        }
    });

Output:

D/EventsDelegate: Created event subscription with ID 1 for URI https://api.particle.io/v1/devices/xx/events/status
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.streamOpened
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-02-27T20:19:06.924Z","coreid":"xx"}
I/System.out: dataPayload=0
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-02-27T20:19:12.032Z","coreid":"xx"}
I/System.out: dataPayload=1
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-02-27T20:19:15.033Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-02-27T20:19:17.031Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-02-27T20:19:19.031Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-02-27T20:19:20.031Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-02-27T20:19:22.040Z","coreid":"xx"}

bump. Any devs have any input on this?

I am having trouble reproducing this issue.

Could you provide more information:

  1. Smartphone model, did you try running code on multiple devices
  2. Android version
  3. How often events are sent/received
  4. Anything else you could add, perhaps there is more logs?

I have posted the code of my class here: https://gist.github.com/Hammy223/462fb7842465ce215f3878b07082a287

1 & 2. Tried emulators with API 21, 24, and 25, same problem. Also tried on physical devices nexus 5x and 6p running API 25
3. Time log shows event happening only every couple of seconds, well outside the timeframe of the limit of 1 event /sec or a burst of 4 events. (I’m manually opening and closing a reed switch on my desk)
4. Check out the code i posted in the gist.
Logcat isn’t spilling out anything more than what I posted.
Also the photon code is irrelevant, console.particle.io log’s and also logcat are showing everything correctly -with the exception of the event handler not being called (which is my problem)

Let me know if you want anymore info

Which version of particle cloud sdk you are using?

0.4.1

I’m having the same issue with SDK 0.3.4.

I was unable to reproduce it so I made cloudsdk version with a bit more logging. If you are willing please try a build I shared and post your log/output.

Adding library to a project:

  1. Add cloudsdk.aar to libs folder.

  2. Add dependencies to build.gradle

    repositories {
    flatDir {
    dirs ‘libs’
    }
    }
    dependencies{
    compile(name: ‘cloudsdk’, ext: ‘aar’)
    compile 'com.google.code.findbugs:jsr305:3.0.1’
    compile 'com.google.code.gson:gson:2.7’
    compile 'com.squareup.okhttp:okhttp:2.7.5’
    compile 'com.squareup.okio:okio:1.9.0’
    compile ‘com.squareup.retrofit:retrofit:1.9.0’
    }

Thanks for your help, I really appreciate it, here is the log:

edit: Is this problem that I am having related to this bug? https://github.com/spark/spark-sdk-android/issues/10

D/EventsDelegate: Created event subscription with ID 1 for URI https://api.particle.io/v1/devices/xx/events
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.streamOpened
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-03-09T02:19:15.779Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN garage/status org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived
D/SseEventReaderImpl: org.kaazing.net.sse.impl.SseEventReaderImpl.next: payload statusDATA
D/SseEventReaderImpl: org.kaazing.net.sse.impl.SseEventReaderImpl.next: DATA
I/System.out: dataPayload=1
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-03-09T02:19:37.760Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN message org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-03-09T02:19:44.776Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN garage/status org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-03-09T02:19:49.759Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN garage/status org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"1","ttl":"60","published_at":"2017-03-09T02:19:52.759Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN message org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived
D/AuthenticatedSseEventSourceImpl: entering org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageReceived: {"data":"0","ttl":"60","published_at":"2017-03-09T02:19:56.771Z","coreid":"xx"}
D/AuthenticatedSseEventSourceImpl: OPEN garage/status org.kaazing.net.sse.impl.AuthenticatedSseEventSourceImpl.messageArrived

My current guess it is our blockingqueue which is…blocking. Made one more build I would appreciate logs from it.

gotcha, here is that log output: pastebin

Ok we managed to narrow it down to next not being called. It looks like next should be called from one place only, I added couple log outputs there in this build, probably quits loop which constantly reads event data however I don’t see why it would do that yet.

http://pastebin.com/pKvterr3

okay, I’m also working on my end to create steps to reproduce

It looks like when an interface’s method is called from the onEvent it causes the bug.

Here’s a github repo with example project
in GarageClient.java uncomment line #37 to reproduce the bug

Until there is a better solution, I solved the problem by executing the code on a new thread.

Good job @Hammy, from what I see it stops event listening loop on onEvent crash. I made this build to test fix (it should pass any crash from onEvent to onEventError). You can also use try catch in onEvent with current release.

Thank you, great fix. I really like that behavior of passing the crash to onEventError a lot more. Thanks for your help, I really do appreciate it.