|
|
|
|
|
|
|
IoM Solution
V100R001C10
Huawei IoT App SDK Integration Development Guide (Android)
|
|
|
Issue
|
01
|
Date
|
2016-09-22
|
HUAWEI TECHNOLOGIES CO., LTD.
|
Copyright © Huawei Technologies Co., Ltd. 2016. All rights reserved.
No part of this document may be reproduced or transmitted in any form or by any means without prior written consent of Huawei Technologies Co., Ltd.
Trademarks and Permissions
and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.
All other trademarks and trade names mentioned in this document are the property of their respective holders.
Notice
The purchased products, services and features are stipulated by the contract made between Huawei and the customer. All or part of the products, services and features described in this document may not be within the purchase scope or the usage scope. Unless otherwise specified in the contract, all statements, information, and recommendations in this document are provided "AS IS" without warranties, guarantees or representations of any kind, either express or implied.
The information in this document is subject to change without notice. Every effort has been made in the preparation of this document to ensure accuracy of the contents, but all statements, information, and recommendations in this document do not constitute a warranty of any kind, express or implied.
|
Huawei Technologies Co., Ltd.
|
Address:
|
Huawei Industrial Base
Bantian, Longgang
Shenzhen 518129
People's Republic of China
|
Website:
|
http://www.huawei.com
|
Email:
|
support@huawei.com
|
|
|
Contents
1 About This Document 1
2 Overview 3
3 Development Environment 6
4 Getting Started 8
5 App Development Guide 9
5.1 Importing the SDK Demo 9
5.1 Importing the SDK Demo 9
5.2 Compiling the Demo Project 10
5.2 Compiling the Demo Project 10
5.3 Creating an Android Project 12
5.3 Creating an Android Project 12
5.4 Basic Services 18
5.4 Basic Services 18
6 References 21
1 About This Document
Purpose
This document describes how to develop the app (Android) for a device to be managed on Huawei IoT Connection Management Platform (OceanConnect) when the OceanConnect and associated gateway are available.
This document provides guidance for app developers to quickly develop an app to implement interactions with the OceanConnect and the gateway in various scenarios, such as login, registration, gateway binding, device adding, command delivery, device information presentation, and device deletion.
Intended Audience
This document is intended for developers to develop an app under the OceanConnect and associated gateway, who must:
-
Master Android development technologies
-
Have read and understood the profile development documents associated with the OceanConnect and gateway
This document is intended for:
-
Android development engineers
Symbol Conventions
The symbols that may be found in this document are defined as follows.
-
Symbol
|
Description
|
|
Indicates an imminently hazardous situation which, if not avoided, will result in death or serious injury.
|
|
Indicates a potentially hazardous situation which, if not avoided, could result in death or serious injury.
|
|
Indicates a potentially hazardous situation which, if not avoided, may result in minor or moderate injury.
|
|
Indicates a potentially hazardous situation which, if not avoided, could result in equipment damage, data loss, performance deterioration, or unanticipated results.
NOTICE is used to address practices not related to personal injury.
|
|
Calls attention to important information, best practices and tips.
NOTE is used to address information not related to personal injury, equipment damage, and environment deterioration.
|
Change History
Changes between document issues are cumulative. The latest document issue contains all the changes made in earlier issues.
-
Issue
|
Release Date
|
Description
|
Impact
|
1
|
2016-06-17
|
This is the first official release.
|
|
About This Document
2 Overview
1.Capability Overview
The IoT app SDK performs encapsulation for app service processing and interactions between the app and the platform. It encapsulates various interfaces for the implementation of various interfaces. Developers can directly call the interfaces provided by the SDK to easily implement various services.
The interfaces provided by the SDK cover the following modules:
-
System initialization module
-
Login module
-
Device module
-
User module
-
Event module
-
Log module
-
Offline push module
The following figure shows the overall networking structure.
2.Technical Advantages
-
Focusing on UI Design Instead of Services and Interactions
After the SDK is integrated, the developer does not need to care about the implementation of the network layer or service layer but needs only to care about codes of the UI layer to be customized.
-
Fast Integration Development with Low Technical Requirements
The developer can quickly develop an app by using fundamental UI controls. The technical requirements for the developer are low.
3.Basic Concepts
-
Principles of communications (far-field communications):
The app interacts with the OceanConnect, to which the gateway in the same network layer is connected. The app delivers commands through the OceanConnect to the gateway. The gateway transfers the commands to devices bound to itself. Similarly, device information updates are reported in MQTT messages through the OceanConnect.
A service is a complete function of a device. Each service corresponds to a capability of the device. For details, see Huawei IoT Device Capability Profile Development Guide.
One or multiple properties form a service. A property is a specific segmentation of the service. For example, the temperature service has two properties: temperature (which is used to represent the current temperature) and target temperature (which is used for temperature adjustment).
-
Basic Concepts
-
Principles of communications (far-field communications):
The app interacts with the OceanConnect, to which the gateway in the same network layer is connected. The app delivers commands through the OceanConnect to the gateway. The gateway transfers the commands to devices bound to itself. Similarly, device information updates are reported in MQTT messages through the OceanConnect.
A service is a complete function of a device. Each service corresponds to a capability of the device. For details, see Huawei IoT Device Capability Profile Development Guide.
One or multiple properties form a service. A property is a specific segmentation of the service. For example, the temperature service has two properties: temperature (which is used to represent the current temperature) and target temperature (which is used for temperature adjustment).
3 Development Environment
Development Environment Requirements
-
Category
|
Recommended Configuration
|
PC operating system
|
Windows 7, Windows 8, Windows 10
|
Android Studio
|
Android Studio1.4 or later versions
|
Java
|
JDK1.7 or later versions
|
Android phone
|
Android 4.0 or later versions, with at least 1 GB memory
|
The Android development tools for the JDK version shall meet the following basic requirements:
-
Android SDK platform tools are related to the packing of Android JAR packages. After the JDK version is upgraded, Android SDK platform tools need to be upgraded accordingly. Android development tools are downward compatible. It is recommended that later versions of Android development tools be used as much as possible if conditions permit.
For details on the setup of the Android studio environment, visit the web page http://www.mamicode.com/info-detail-887724.html.
Project Directory Structure and Files
-
SDK App Demo Directory Structure
|
Directory
|
Function
|
APP
│
├─src
│ └─main
│ └─assets
│ └─config
├─java
Smarthomelibrary
├─libs
├─src
│ └─main
│ └─jniLibs
│ └─armeabi
|
java
|
Stores SDK app Demo codes.
|
config
|
Stores the ca.pem certificate.
Stores the IOM.cer platform certificate.
Stores the UDS.cer cloud storage certificate.
For details on the functions of the certificates, see section "Glossary."
|
Libs
|
Stores jar packages provided by the Agent Lite and third-party jar packages.
|
armeabi
|
Stores the so library.
|
|
| 4 Getting Started
Obtaining Software and Documents
Checking Whether All Necessary Resources Have Been Obtained
Download the Android SDK software package, which includes the following contents:
-
SDK static library software package
-
IOM.cer
-
ca.pem
-
UDS.cer
-
Demo project for SDK integration
Other Associated Documents
-
IoT App SDK API document
-
IoT Agent Lite Integration Development Guide
-
IoT App SDK Integration Development Senior Guide
Development Steps
The developer can directly import the Demo into the Android studio and check the Demo to learn how to call relevant interfaces to implement basic services. The developer can also perform development based on the Demo to optimize the implementation of various services. The Demo simply demonstrates how to call interfaces. It is based on rough UI architecture and simple UI arrangement. It is recommended that a new project be created to perform the development. For details on how to create a project, see section "Creating an Android Project".
This document describes SDK initialization and how to use the login interface and the gateway binding interface based on the Demo. For details on how to use the other interfaces, see Huawei IoT App SDK Integration Development Senior Guide and the IoT App SDK API document.
The development steps are as follows:
-
Import the Demo into the Android studio to perform development based on the Demo, or create an Android project to perform development.
-
Initialize the SDK.
-
Example of using the login interface
-
Example of using the gateway registration interface
The developer shall contact Huawei customer support personnel to learn the examples of using the other interfaces.
5.1 Importing the SDK Demo
5.2 Compiling the Demo Project
5.3 Creating an Android Project
5.4 Basic Services
5.1 Importing the SDK Demo
The developer can import the Demo into the Android studio to learn examples of calling specific interfaces, and perform the development based on the Demo. However, it is not recommended that the development be performed based on the Demo, because the Demo simply demonstrates how to call interfaces and is based on rough UI architecture and simple UI arrangement. The developer is advised to create an Android project to perform the development.
-
Copy and decompress the SDK Demo to a local directory, and perform the following steps to import the Android project.
-
Open the studio, and choose Open from the File menu. The Open File or Project dialog box is displayed.
-
Select the SDK Demo on the dialog box, and then click OK.
5.2 Compiling the Demo Project -
Connect the Android phone to a PC. If the connection is successful, the brand and model of the connected phone will be displayed in the lower left corner of the Android studio, as shown in the following figure.
-
Select a specific item on the left of a green triangle icon on the menu bar, and then click the green triangle icon.
-
A dialog box is displayed on the successfully running studio. Select the model of the connected phone, and then click OK. A smart home application appears on the phone.
5.3 Creating an Android Project
The developer can create an Android project using the Android studio to develop an app to implement the UI layout and style as he or she expects. The following descriptions show how to create a project and how to add jar packages, the so library, and certificates. The developer can begin with UI development after finishing such work.
-
Create an Android project.
Create a project using the studio. Application name indicates the name of the app to be developed, and Company Name indicates the domain name of the company, which will have an impact on Package name. Package name indicates the package name of the app. Each app has a unique package name. If two apps have the same package name, the Android system will consider that the two APPs are the same app. Therefore, ensure that the package names of different apps are different. After setting the parameters, click Next all the way until Finish to finish creating the project.
-
Directory structure of the successfully created project:
-
As shown in the preceding figure, app is a module used to compile codes for UI implementation. You can create another module to store third-party jar packages and the so library. Choose File > New > New Module, and then select Android Library. After the module is successfully created, a module named smarthomelibrary appears in the directory structure, as shown in the following figure.
-
Add the dependency library. Use the module smarthomelibrary as the dependency of the module app. After the dependency is added, the module app can reference the jar packages and so library in the module smarthomelibrary. Choose File > Project Structure. A dialog box is displayed. Select app, click a green plus mark on the right, and select Module dependency to add the module smarthomelibrary as the dependency.
-
The Libs folder in the module smarthomelibrary is used to store jar packages. Put all jar packages of the SDK software package into this directory, and then the functions of these jar packages can be used in the codes.
-
Choose smarthomelibrary > src > main > jniLibs > armeabi to add the so library of the SDK software package. If the jniLibs folder does not exist, you can create it in the main directory and then create a folder named armeabi in the jniLibs folder.
-
Add certificates ca.pem, IOM.cer, and UDS.cer to the module app. The following figure shows the directory structure after the certificates are added.
5.4 Basic Services
1.SDK Initialization
Scenario description:
Before using the interfaces of the SDK, the developer must initialize the SDK and set proper parameters. It is recommended that SDK initialization be performed in the application function, or SDK initialization be performed before login to set parameters such as HA_IP. The parameters may be configurable and may support manual entry.
Before all APIs are called, they must be initialized. For example, to call the login interface, you must call the LoginApi.init(context) interface to initialize the login interface first.
Example of codes:
SysApi.init(context);
LoginApi.init(context);
UserApi.init(context);
EventApi.init(context);
DeviceApi.init(context);
SysApi.setConfig(SysApi.SYS_CFG_NA_SERVER_IP, "na ip");
SysApi.setConfig(SysApi.SYS_CFG_NA_SERVER_PORT, "na port");
SysApi.setConfig(SysApi.SYS_CFG_MQTT_SERVER_IP, "mqtt ip");
SysApi.setConfig(SysApi.SYS_CFG_MQTT_SERVER_PORT, "mqtt port");
SysApi.setConfig(SysApi.SYS_CFG_OPERATION_TIMEOUT_TIME, "60");
2.Login
Scenario description:
After the app is started and the SDK is initialized to set the parameters, the login process continues. Login falls into login without the verification code and login with the verification code. The initial login is performed without the verification code.
For details on the meanings of error codes that may be returned during login, see the definitions of constants of the LoginApi module in the SDK API document.
The LoginApi provides a broadcast on login status changes. The broadcast name is EVENT_LOGIN_STATUS_CHANGED, where userinfo carries the parameter PARAM_LOGIN_STATUS to indicate the current login status of the user and the parameter PARAM_LOGIN_REASON to indicate the reason why the user is in this status.
Example of codes:
LoginApi.login(account, password, new ResultsListener>() {
@Override
public void onSuccess(Class voidClass) {
mSharePreference.commitString(BaseConstants.SP_USER_ACCOUNT,
account);
}
@Override
public void onError(int i) {
LogApi.d(TAG, "Login failed, The errorCode is " + i);
hideProgress();
String failedDesc = "";
switch (i) {
case LoginApi.ERROR_CODE_LOGIN_AUTH_FAILED:
failedDesc =
getString(R.string.User_login_error_account_or_password_incorrect);
break;
case LoginApi.ERROR_CODE_LOGIN_ACCOUNT_INACTIVE:
failedDesc = getString(R.string.base_user_inactive);
break;
case LoginApi.ERROR_CODE_LOGIN_ACCOUNT_LOCKED:
failedDesc = getString(R.string.User_login_error_account_is_locked);
mVerifyCodeRL.setVisibility(View.VISIBLE);
getVerifyCode();
break;
case LoginApi.ERROR_CODE_LOGIN_FORCE_LOGOUT:
failedDesc = getString(R.string.base_user_force_logout);
break;
case LoginApi.ERROR_CODE_LOGIN_ACCOUNT_NOT_EXIST:
failedDesc =
getString(R.string.User_login_error_account_or_password_incorrect);
break;
case LoginApi.ERROR_CODE_LOGIN_INVALID_VERIFY_CODE:
failedDesc = getString(R.string.User_login_error_verify_invalid);
mVerifyCodeRL.setVisibility(View.VISIBLE);
getVerifyCode();
break;
default:
failedDesc = getString(R.string.base_user_login_failed);
break;
}
Toast.makeText(mContext, failedDesc, Toast.LENGTH_SHORT).show();
mLogin.setEnabled(true);
}
@Override
public void onTimeout() {
LogApi.d(TAG, " Login time out");
hideProgress();
Toast.makeText(mContext, getString(R.string.base_time_out),
Toast.LENGTH_SHORT).show();
mLogin.setEnabled(true);
}
});
}
3.Gateway Binding
Scenario description:
The gateway is a transfer station for communications between the current device and other devices. Before adding the other devices, you must specify a gateway to connect to the OceanConnect and ensure that the gateway is not bound to other users. For details on gateway initialization and registration, see relevant documents of the Agent Lite and the OceanConnect.
The registerDeviceToCloud function of the interface DeviceApi is called to bind the gateway. In general, the MAC address of the gateway is carried in the command. After the gateway is successfully bound, the callback function corresponding to the binding success will apply and carry information about the successfully bound gateway.
Example of codes:
DeviceApi.registerDeviceToCloud(editText, new ResultsListener() {
@Override
public void onSuccess(Device device) {
hideProgress();
mOk.setEnabled(true);
Toast.makeText(mContext, getString(R.string.device_add_new_device_success), Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onError(int i) {
hideProgress();
mOk.setEnabled(true);
Toast.makeText(mContext, getString(R.string.device_add_new_device_failed), Toast.LENGTH_SHORT).show();
}
@Override
public void onTimeout() {
hideProgress();
mOk.setEnabled(true);
Toast.makeText(mContext, getString(R.string.base_time_out), Toast.LENGTH_SHORT).show();
}
});
6 References
FAQs
Question 1: No response is returned during login. That is, the console log does not contain the login HTTP request. Check the following possible causes:
-
Check whether the Wi-Fi and the OceanConnect are located in the same network segment.
-
Check whether the entered HA IP address is inconsistent with the IP address provided by the OceanConnect.
-
Check whether the ca.pem certificate has been imported and does not expire.
Question 2: The login is successful but other HTTP requests cannot be sent.
-
Check whether the certificates IOM.cer and UDS.cer have been imported.
Glossary
The OceanConnect is a server for an app. The app exchanges all its data and services with the OceanConnect.
As the platform connection certificate, it is used by the server to check the legality of the client and used by the client to check the legality of the server.
It is the certificate of the cloud storage UDS. Videos recorded by a camera are mostly stored in the cloud storage space. This certificate is required for a check during video download.
This certificate is used for authentication between an app and the OceanConnect.
Share with your friends: |