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 |
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 connection is successful. The login is successful only after the LoginService.TOPIC_LOGIN_CONNECTED broadcast is received. Before connecting a device, call the API Setting Service Parameters to transfer the required connection 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 | The connection or reconnection is successful. |
| TOPIC_LOGIN_DISCONNECT | IotaMessage object (Obtained by using intent.getSerializableExtra(LoginService. LOGIN_BROADCAST_MSG_IE_IOTAMSG)) | LOGIN_IE_REASON | Cause of the connection or reconnection failure. |
LOGIN_IE_REASON:
| Enumerated Item | Value | Type | Description |
|---|---|---|---|
| LOGIN_REASON_NULL | 0 | N/A | The reason is not specified. |
| LGN_REASON_CONNCET_ERR | 1 | N/A | A connection error occurs. |
| LOGIN_REASON_SERVER_BUSY | 2 | N/A | The server is busy. |
| LOGIN_REASON_AUTH_FAILED | 3 | N/A | The authentication fails. You need to reconnect the device to the IoT platform. |
| LOGIN_REASON_NET_UNAVAILABLE | 4 | N/A | The network is unavailable. |
| LOGIN_REASON_DEVICE_NOEXIST | 5 | N/A | The device does not exist. You need to reconnect the device to the IoT platform. |
| LOGIN_REASON_DEVICE_RMVED | 6 | N/A | The device is deleted. You need to reconnect the device to the IoT platform. |
| LOGIN_REASON_UNKNOWN | 7 | N/A | Unknown reason. |
Example
1 2 3 4 5 6 7 8 9 10 11 | //Set connection 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 waits for a connection status broadcast from the AgentLite.
Suggestions:
- If the connection is successful, the gateway reports the status of indirectly connected devices and cached data.
- If the connection fails, the gateway records 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 | //Call this API to register the function for subsequent processing after successful connection.
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);
//Call this API to register the function for subsequent processing after the connection fails.
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);
|
Once the connection is successful, the device is connected to the IoT platform.
If the device is disconnected from the IoT platform due to network or server faults, the AgentLite will automatically attempt to reconnect the device to the IoT platform and report the real-time status to the third-party application.
Last Article: (Optional) Configuring the Login Information Encryption Algorithm
Next Article: Disconnecting a Device
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.