Updated on 2022-02-24 GMT+08:00

Adding a Device

API Function

This API is used to add a device that accesses the IoT platform through the gateway. After the device is connected to the IoT platform, the IoT platform will assign a unique logical ID to the device.

API Description

1
public static boolean addDevice(int cookie, IotaDeviceInfo deviceInfo);

Class

HubService

Parameter Description

Parameter

Mandatory or Optional

Type

Description

cookie

Optional

int

The value ranges from 1 to 65535.

deviceInfo

Mandatory

IotaDeviceInfo class

Specifies information about the device.

Return Value

Return Value

Description

true

Success

false

Failure

NOTE:

Return values only show API calling results. For example, the return value true indicates that the API is called successfully but does not indicate that the device is added successfully. The device is added successfully only after the HubService broadcast is received.

Example

Call this API to add a device.

1
HubService.addDevice(29011, new IotaDeviceInfo("nodeId", "manufacturerId", "deviceType", "model", "protocolType"));

Implement the observer API provided by the AgentLite before calling this API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class AgentliteHub implements MyObserver {
    public AgentliteHub (Observable hubService) {
        hubService. registerObserver (this);
    }
//Receive a device adding response.
    @Override
    public void update(IotaMessage arg0) {
        // TODO Auto-generated method stub
        System.out.println("Receive a notification from HubService:" + arg0);
        int mMsgType = arg0.getMsgType();
        switch(mMsgType) {
//Receive a device adding response.
            case IodevService.IODEV_MSG_ADD_DEVICE_RSP:
            getAddDeviceAnswer(arg0);
            break;
//Receive a device deleting response.
            case IodevService.IODEV_MSG_RMV_DEVICE_RSP:
            getRmvDeviceAnswer(arg0);
            break;
//Receive a device status update response.
            case IodevService.IODEV_MSG_UPDATE_DEVSTATUS_RSP:
            getUpdateStatusAnswer(arg0);
            break;
            case IodevService.IODEV_MSG_RECEIVE_CMD:
            getUnbindAnswer(arg0);
            break;
            default:
            break;
        }
    }