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

Connecting a Device

API Function

This API is used to connect a device to the IoT platform after the device is bound to the IoT platform for the first time or the device restarts.

API Description

1
public static boolean login();

Class

LoginService

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 login is successful. The login is successful only after the LoginService.TOPIC_LOGIN_CONNECTED broadcast is received. Before login, the API for Setting Service Parameters is used to transfer the required login information.

Output

Broadcast Name

Broadcast Parameter

Member

Description

TOPIC_LOGIN_CONNECTED

IotaMessage object

(Obtained by using intent.getSerializableExtra(LoginService. LOGIN_BROADCAST_MSG_IE_IOTAMSG))

N/A

Specifies that the login or reconnection is successful.

TOPIC_LOGIN_DISCONNECT

IotaMessage object

(Obtained by using intent.getSerializableExtra(LoginService. LOGIN_BROADCAST_MSG_IE_IOTAMSG))

LOGIN_IE_REASON

Specifies the cause of the login or reconnection failure.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
//Set login parameters.
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_DEVICEID, "deviceId");
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_APPID, "appId"); 
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_SECRET, "passWord");
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_IOCM_ADDR, "haAddr"); 
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_IOCM_PORT, "8943");
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_MQTT_ADDR, "haAddr");
LoginConfig.setConfig(LoginConfig.LOGIN_CONFIG_MQTT_PORT, "8883");

//Call this API to connect a device to the IoT platform.
LoginService.login();

The device then waits for a connection status broadcast from the AgentLite.

Suggestions:

  1. If the connection is successful, report the status of the indirectly connected devices and the cached data.
  2. If the connection fails, record the device status. If an indirectly connected device reports data, the data is cached and reported until the connection is successful.
 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
31
32
33
34
//Receive a login success response.
BroadcastReceiver mReceiverConnect;
mReceiverConnect = new BroadcastReceiver() {
    @Override 
       public void onReceive(Context context, Intent intent) {
    //Obtain IotaMessage.
        IotaMessage iotaMsg = (IotaMessage)intent.getSerializableExtra(LoginService. LOGIN_BROADCAST_MSG_IE_IOTAMSG);
//Obtain the system status from the message handle returned by the callback.
        int status = uspMsg.getint(LoginService.LOGIN_IE_STATUS, 0);
        //update device states 
        ... 
        return true;
    }
}
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filterCon= new IntentFilter(LoginService.TOPIC_LOGIN_CONNECTED);
mLocalBroadcastManager.registerReceiver(mReceiverConnect, filterCon);
//Receive a login failure response.
BroadcastReceiver mReceiverDisconnect;
mReceiverDisconnect = new BroadcastReceiver() {
    @Override 
       public void onReceive(Context context, Intent intent) {
    //Obtain IotaMessage.
        IotaMessage iotaMsg = (IotaMessage)intent.getSerializableExtra(LoginService. LOGIN_BROADCAST_MSG_IE_IOTAMSG);
//Obtain the error code of the response.
        int reason = iotaMsg.getint(LoginService.LOGIN_IE_REASON, 0);
        //stop reporting data 
        ... 
        return true;
    }
}
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
IntentFilter filterDiscon= new IntentFilter(LoginService.TOPIC_LOGIN_DISCONNECTED);
mLocalBroadcastManager.registerReceiver(mReceiverDisconnect, filterDiscon);

After the login is successful, the device is connected to the IoT platform.

If the device is disconnected from the IoT platform due to the network or server fault, the AgentLite will automatically attempt to reconnect the device to the IoT platform and report the real-time status to the third-party application by using these two broadcasts.