Is device active

Have successfully built my first IOS app using swift. But I do have a question concerning “get device” function. Is there a way to tell whether or not a device is active (turned on) programmatically? I noticed when clicking on “devices” using “Particle Build” there is a color coded dot that signifies when a particular device is active or not. I would like to replicate this if possible.

Thank you

I think it’s using this API endpoint, so that information should be included in the response.

Thanks Moors, I see that in the “get” response there is a bool that indicates whether or not the device is connected to the cloud.

To save me some time, and it’s not too much of a problem, would you by chance have the snippet of swift sample code that would accomplish this check? I would like to incorporate it into the “get device” function? If not, I’ll try to figure it out.

Much appreciated

I’ve never programmer iOS before, so not too much of help here.
Maybe something similar to this?

https://docs.particle.io/reference/ios/#list-devices

var myPhoton : SparkDevice?
SparkCloud.sharedInstance().getDevices { (sparkDevices:[AnyObject]!, error:NSError!) -> Void in
    if let e = error {
        println("Check your internet connectivity")
    }
    else {
        if let devices = sparkDevices as? [SparkDevice] {
            for device in devices {
                if device.connected == true {
                    //do stuff here for connected devices
                }
                else {
                    // do stuff for non-connected devices?
                }
            }
        }
    }
}

This is a total, random guess, with a high chance of it not working… Just sayin’ :wink:

1 Like

Lol you hit a home run, it works just a fine. For some reason I was thinking I had to write some special API call but actually it was nothing more than setting a property in the cloud class, as you so graciously pointed out. Thank you for taking time to assist😄

1 Like

Guess I should start programming some iOS then… Let’s just call it beginners luck :see_no_evil: Glad it works for you though :smile: