Device

Device management can be done with the following JavaScript APIs.

Method/Property Description
monaca.cloud.Device.getProperty() Get a property value of device
monaca.cloud.Device.getProperties() Get property values of a device
monaca.cloud.Device.saveProperty() Update a property of a device
monaca.cloud.Device.saveProperties() Update properties of a device

Device.getProperty()

Get a property value of a device.

monaca.cloud.Device.getProperty(name: String) : $.Promise

Parameter

Name Type Description
name String A property name

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Within the done() callback, there is the property value.

Example

Refer to the following code for an example of how to get a property value of a device.

monaca.cloud.Device.getProperty("nickname")
.done
(
    function(result)
    { console.log("Device's nickname: " + result); }
)
.fail
(
    function(err)
    { /* error handling codes */ }
)
.always
(
    function()
    { /* what must be done despite the outcome of the getProperty function */ }
);

Device.getProperties()

Get property values of a device.

monaca.cloud.Device.getProperties(names: Array) : $.Promise

Parameter

Name Type Description
names Array of String Property names

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Within the done() callback, there is a JSON Object containing various properties’ values.

Example

Below is how to get the values of 2 properties of a device.

monaca.cloud.Device.getProperties(["nickname", "color"])
.done
(
    function(result)
    {
        console.log("Properties: " + JSON.stringify(result));
        console.log("Device's nickname: " + result.nickname);
    }
)
.fail
(
    function(err)
    { /* error handling codes */ }
)
.always
(
    function()
    { /* what must be done despite the outcome of the getProperties function */ }
);

Device.saveProperty()

Update a property value of a device.

monaca.cloud.Device.saveProperty(name: String, value: String) : $.Promise

Parameter

Name Type Description Requirement
name String A property name Must consist of [a-zA-Z0-9] characters and must start with [a-zA-Z].
value String The value of the corresponded property name to be added or updated

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Errors Code

Errors are returned as Error object.

Code Description
-32602 Invalid params

Example

The following example illustrates how to add/update the device’s nickname to "Monaca".

monaca.cloud.Device.saveProperty("nickname", "Monaca")
.done
(
    function()
    { console.log("Saved."); }
)
.fail
(
    function(err)
    { /* error handling codes */ }
)
.always
(
    function()
    { /* what must be done despite the outcome of the saveProperty function */ }
);

Device.saveProperties()

Update an array of property values of a device.

monaca.cloud.Device.saveProperties(properties: Object) : $.Promise

Parameter

Name Type Description Requirement
properties JSON Object Additional properties of a device to be added/updated Key names must consist of [a-zA-Z0-9] characters and must start with [a-zA-Z]. Data size must not exceed the size limit (500KB).

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Errors Code

Errors are returned as Error object.

Code Description
-32602 Invalid params

Example

The following example illustrates how to add/update 2 properties (nickname & color) of a device.

monaca.cloud.Device.saveProperties({"nickname": "Monaca", "color": "#9999FF"})
.done
(
    function()
    { console.log("Saved."); }
)
.fail
(
    function(err)
    { /* error handling codes */ }
)
.always
(
    function()
    { /* what must be done despite the outcome of the saveProperties function */ }
);

See Also: