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

Binding a Device

API Function

A device must be bound to the IoT platform before accessing the IoT platform for the first time. The upper-layer application calls this API to transfer the device serial number, MAC address, or other device information to bind a device to the IoT platform.

Before binding a device, developers must call the BindConfig.setConfig API to set the IP address and port number of the IoCM server to be bound. The default port number is 8943 for the AgentLite.

API Description

1
public static boolean bind(String verifyCode, IotaDeviceInfo deviceInfo);

Class

BindService

Parameter Description

Parameter

Mandatory or Optional

Type

Description

verifyCode

Mandatory

String

Specifies a device binding verification code.

If the device is registered on the SP portal, set verifyCode to the preSecret used during device registration.

deviceInfo

Mandatory

IotaDeviceInfo

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 binding is successful. The binding is successful only after the BindService broadcast is received.
  • If the binding fails, the AgentLite automatically binds the device after 30 seconds. If the retry fails for five consecutive times (the total number of attempts is six), a message is returned indicating that the binding fails and the binding stops. If developers want to re-initiate the binding, restart the device.

Example

Call this API to bind a device.

1
2
3
String verifyCode =   "123456"  ;
deviceInfo = new IotaDeviceInfo(nodeId,manufactrueId,deviceType,model,protocolType);
BindService.bind(verifyCode,deviceInfo);

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
//Call the bindService.registerObserver(this) to register the observer for receiving the binding result callback and obtaining callback parameters, which are used in login configuration.
public class AgentliteBind implements MyObserver{
    public Subscribe (Observable bindService) {
        bindService. registerObserver (this);
    }
}
//Override the update method in the AgentliteBind.
@Override
public void update(IotaMessage arg0) {
    System.out.println("BindManager receives a binding notification:" + arg0);
    int status = arg0.getUint(BindService.BIND_IE_RESULT, -1);
    System.out.println("status is :" + status);
    switch (status) {
        case 0:
        saveBindParaAndGotoLogin(arg0);
        break;
        default:
        System.out.println("============= binding failure ==============")
        bindAction();
        break;
    }
}

Receive a device binding response.

1
2
3
4
5
6
7
8
9
//After a device is bound, the AgentLite will return the parameters shown in the following output. Developers must store these parameters and configure these parameters on the device before connecting the device to the IoT platform.
//Save the parameters carried in the device binding response.
private void saveBindParaAndGotoLogin(IotaMessage iotaMsg) {
    String appId = iotaMsg.getString(BindService.BIND_IE_APPID);
    String deviceId = iotaMsg.getString(BindService.BIND_IE_DEVICEID);
    String secret = iotaMsg.getString(BindService.BIND_IE_DEVICESECRET);
    String haAddress = null, lvsAddress = null;
    saveGatewayInfo(appId, deviceId, secret, haAddress, lvsAddress);
}