Check device online using Android SDK


#1

How could I check whether device is “still” connected after Cloud account login ?
Below is the case I ran into problem.

myDevice is ON : login into Particle account and get Device
myDevice is ON : able to perform get Variable and Functions call to Particle device
myDevice is OFF : using the Particle Device “isConnected()” will still continue to show “true”

When I continue make call to Particle Device, Android Apps will crash. I suppose due to too many Async calls (any limit how many I can generated ?).
The status of “isConnected()” will only be correct again if I perform another login.
I tried “refresh” but did not able to get updated status.

Is there other Device call that I can update the device online status ?


#2

Another question, while myDevice is OFF. Since the “isConnected” still true, I placed a device “callFunction” and after some timeout, I got the callback “onSuccess” but I could not tell it is timeout.

What is the best way to determine a request to device through Cloud failed ?
So that I can find way to referesh the ParticleDevice information.

Thanks,


#3

Anyone has great suggestion or had ran into this problem that your device is off and try to access with Android SDK ?
I tried various method, still could not determine Device is offline until I re-login account.

Once my program call the Spark Cloud functions more than once (when device off), Android apps will stop !!

Thanks…


#4

Lots of questions here. Here are some answers and (hopefully) helpful commentary:

  • refresh() should have worked. If not, that’s a bug! If you can repro this, please file an issue on GitHub and I’ll take a look.
  • what kind of crash are you getting? Can you provide a stack trace?
  • the Async class is using AsyncTask under the hood. By default (at least on the version of Android source code I looked at) that has a limit of 128 outstanding tasks at a time. Seems unlikely that you’d hit that, but it’s certainly possible.
  • Timeouts: are you certain you got the success callback after a network timeout (i.e.: a failure)? Are you sure it didn’t just succeed after a very long time (slow network with many queued up requests, etc)?

#5

With more testing and I also changed my implementation. Now I am using a Service instead of calling directly from Activity. So to make sure the Callback from Async task is always alive and available.

  • (correction) I re-tested the refresh(), it is working now ONLY if device go online. If device go offline, refresh() will not update device isConnected() status correctly.

  • I put a log print on each onSuccess return from ParticleCloud calls. I can see the printout to indicate. However, if I don’t initialize the object, I can see it is “null”.
    For now, I use this as indicator of error (this cause “application no response” problem)

  • But I would prefer it could return as onFailed() or something.

I tested the same scenario with “Particle Apps” and result is the same.

  • Power up device (so that it is online)
  • Start Particle Apps and login (it would shoe online) – OK
  • power down device (at this time, Apps does not know it is offline)
  • Start Tinker by pressing the device
  • Enable a digitalpin (D0 for write)
  • try pressing it a few times.
  • Enable another digitalpin (D1 for write)
  • try pressing it a few times.

Now, you should receive a error message indicate Particle Apps is “stopped”

p.s.
Will the listen to Device Event soon be available ? This will resolve a lot of my current limitation.
A test code would be nice :smile:


#6

Re: the refresh problem
There is some delay between when the device drops offline and when the cloud can reflect this, usually between 30-60 seconds. That’s nothing specific to Particle or the SDK, that’s just the nature of networking – your device could drop its connection at any time, so you have to be ready for errors with your network calls.

onSuccess() issues?
I’m still not clear on what’s happening here. Could you post a gist or a sample project to github which demonstrates this problem?

Crashes
Interesting re: the “stopped” error message. I’ll have to try that out for myself.

Also, if you get an “application not responding”, then you’re performing long operations on the main thread. I am 99% sure that it’s not the Particle SDK doing that, so it’s something you’ll want to check into. (OTOH, if you find that it is the SDK, please let us know!)

Events
It’s already available! :grinning: Check it out: ANNOUNCEMENT: Android SDK finally here! No sample usage available at the moment, but the API itself in ParticleDevice and ParticleCloud is well-documented via javadoc.


#7

Re: the refresh problem
I confirm the status will be updated after some time as @jensck indicated.

onSuccess() issues?
below is the code I am using for getting variable. When I turn off device, the log will print from “onSuccess”

 public boolean getVariable(final Service service, final String name, final int type) {
    if (mDevice == null)                            // Device info available
        return false;
    if (mDevice.isConnected()==false)               // Device online
        return false;
    Async.executeAsync(ParticleCloud.get(service), new Async.ApiWork<ParticleCloud, Object>() {
        Object result;
        @Override
        public Object callApi(ParticleCloud sparkCloud) throws ParticleCloudException, IOException {
            try {
                if (type == SprinklerData.TYPE_STRING)          // String variable
                    result = mDevice.getStringVariable(name);
                else if (type == SprinklerData.TYPE_INT)        // Integer variable
                    result = mDevice.getIntVariable(name);
            } catch (IOException | ParticleCloudException | ParticleDevice.VariableDoesNotExistException error) {}
            return name;                                        // Use in call back on READ
        }
        @Override
        public void onSuccess(Object name) {
            sparkVarStatus = false;
            if (result==null){
                Log.d("CloudService", "Variable-error");
            }
            CloudService.variableCallback(name.toString(), result);     // Call Cloud Service
        }
        @Override
        public void onFailure(ParticleCloudException e) {
            sparkVarStatus = false;
            Log.d("CloudService", e.getBestMessage());
        }
    });
    return true;
}

Crashes
Now, my Apps is working fine after I put in checking for every ParticleCloud access return, whether “null” or not. So that I can indicate device busy and not to proceed the remaining ParticleCloud access calls.

Events
Updating tools and code. Wish I get this working today :slight_smile: Thanks for all the support to Android newbie like me :wink:


#8

Hi Dilbert

Can I get the code to solve your problem, I cannot understand :frowning:


#9

Hi @hitmanbaby2007.

It looks like Dilbert’s last post on this topic was about 3 years ago. It’s not guaranteed he is still active within the community or has access to this source code that you’re referencing.

Also, if you want to get someone’s attention, it’s always a good idea to “at” them, as I did to you above.


#10

To directly tag some member you should use this way of writing it: @Dilbert (I see Matthew as pointed that out already :blush:)

His last post in the forum was August 2018. That also seems the last time he visited this forum.


#11

@ScruffR Thank you for your reply ^^

Can you kindly check this topic Check photon lost wifi connection in android app

No one respone for my topic :frowning:


#12

@mstanley Thank you for your reply ^^

Can you kindly check this topic Check photon lost wifi connection in android app

No one respone for my topic :frowning:


#13

Sorry @hitmanbaby2007, I only start the Android code for testing back then. I did not continue development after a month.