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 TOPIC_ADDDEV_RSP broadcast is received.

Output

Broadcast Name

Broadcast Parameter

Member

Description

TOPIC_ADDDEV_RSP

IotaMessage

(Obtained by using intent.getSerializableExtra(HubService.HUB_BROADCAST_IE_IOTAMSG))

HUB_IE_RESULT

Specifies the device adding result.

HUB_IE_DEVICEID

Specifies the device ID. If the device is added successfully, the device ID is returned.

HUB_IE_COOKIE

The value ranges from 1 to 65535.

Example

1
2
//Call this API to add a device.
HubService.addDevice(29011, new IotaDeviceInfo("nodeId", "manufacturerId", "deviceType", "model", "protocolType"));

The device waits for the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
//java code 
//Register a broadcast receiver to process the device adding.
BroadcastReceiver mAdddeviceRsp;
mAdddeviceRsp = new BroadcastReceiver() {
    @Override 
        public void onReceive(Context context, Intent intent) {
        //Do Something 
        IotaMessage iotaMsg = (IotaMessage)intent.getSerializableExtra(HubService. HUB_BROADCAST_IE_IOTAMSG);
        int result = iotaMsg.getUint(HubService.HUB_IE_RESULT, 0);
        String deviceId = iotaMsg.getString(HubService.HUB_IE_DEVICEID);
        int cookie = iotaMsg.getUint(HubService.HUB_IE_COOKIE, 0);
        return;
    }
};
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filterAddDev = new IntentFilter(HubService.TOPIC_ADDDEV_RSP);
mLocalBroadcastManager.registerReceiver(mAdddeviceRsp, filterAddDev);