IoTIoT

Compute
Elastic Cloud Server
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
Domain Name Service
VPC Endpoint
Cloud Connect
Enterprise Switch
Security & Compliance
Anti-DDoS
Web Application Firewall
Host Security Service
Data Encryption Workshop
Database Security Service
Advanced Anti-DDoS
Data Security Center
Container Guard Service
Situation Awareness
Managed Threat Detection
Compass
Cloud Certificate Manager
Anti-DDoS Service
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GaussDB NoSQL
GaussDB(for MySQL)
Distributed Database Middleware
GaussDB(for openGauss)
Developer Services
ServiceStage
Distributed Cache Service
Simple Message Notification
Application Performance Management
Application Operations Management
Blockchain
API Gateway
Cloud Performance Test Service
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
DevCloud
ProjectMan
CodeHub
CloudRelease
CloudPipeline
CloudBuild
CloudDeploy
Cloud Communications
Message & SMS
Cloud Ecosystem
Marketplace
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP License Service
Support Plans
Customer Operation Capabilities
Partner Support Plans
Professional Services
enterprise-collaboration
Meeting
IoT
IoT
Intelligent EdgeFabric
DeveloperTools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Updated at: Feb 24, 2022 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.

Did you find this page helpful?

Failed to submit the feedback. Please try again later.

Which of the following issues have you encountered?







Please complete at least one feedback item.

Content most length 200 character

Content is empty.

OK Cancel