After you download and integrate the SDKs for your app, you must initialize them.
Overview
For SDKs that use API Level 8 or later, you do not need to initialize each SDK separately. Instead, you can use the unified initialization interface to initialize all required SDKs at once. If you use an SDK that uses API Level 7 or earlier, you must upgrade it to the latest version, API Level 10. For more information, see SDK upgrade.
The unified initialization interface initializes the following SDKs based on the SDK configuration items that you select when you download the SDK.
API channel SDK (Required for initialization)
Account and user SDK (Required for initialization)
Identity authentication SDK (Required for initialization)
Persistent connection channel SDK
SDK for the Thing Specification Language model
Mobile application push SDK
BoneMobile container SDK
Link Visual video media SDK
Currently, unified initialization only supports initialization with parameters.
Parameter-based initialization supports customization. This includes selecting the connection type (the Chinese mainland or global), enabling logs, and customizing parameters for third-party offline push channels.
SDK initialization
Download and integrate the SDK. For more information, see Download and integrate the SDK.
Initialize the SDK.
Initialize with parameters
// Configure initialization parameters. IoTSmart.InitConfig initConfig = new IoTSmart.InitConfig() // REGION_ALL: Connect to multiple global endpoints. REGION_CHINA_ONLY: Connect directly to endpoints in the Chinese mainland. .setRegionType(IoTSmart.REGION_ALL) // setProductEnv is for API Level 8 only. For API Level 9 and later, use IoTSmart.setProductScope to specify whether the app can operate on unreleased products. The distinction between beta and official versions is removed, and all are treated as official. .setProductEnv(IoTSmart.PRODUCT_ENV_PROD) // Specifies whether to enable logs. .setDebug(true); // Method 1: The user passes the appKey and appSecret. The user must securely store the appKey and appSecret. IoTSmart.AppKeyConfig keyConfig = new IoTSmart.AppKeyConfig(); keyConfig.appKey = "appKey"; // Replace with the appKey obtained from the platform. keyConfig.appSecret = "appSecret"; // Replace with the appSecret obtained from the platform. initConfig.setAppKeyConfig(keyConfig); // Method 2: The user passes a class that implements the ISecuritySource interface. For more information, see the "User self-signing code reference" section below. SecurityImpl customSecurityImp=new SecurityImpl(); initConfig.setCustomSecurity(customSecurityImp); // Customize third-party offline push channels. Huawei, Xiaomi, FCM, OPPO, and VIVO are supported. IoTSmart.PushConfig pushConfig = new IoTSmart.PushConfig(); pushConfig.fcmApplicationId = "fcmid"; // Replace with the ID obtained from the FCM platform. pushConfig.fcmSendId = "fcmsendid"; // Replace with the send ID obtained from the FCM platform. pushConfig.xiaomiAppId = "XiaoMiAppId"; // Replace with the AppID obtained from the Xiaomi platform. pushConfig.xiaomiAppkey = "XiaoMiAppKey"; // Replace with the AppKey obtained from the Xiaomi platform. pushConfig.oppoAppKey = "oppoAppKey"; // Replace with the AppKey obtained from the OPPO platform. pushConfig.oppoAppSecret = "oppoAppSecret"; // Replace with the AppSecret obtained from the OPPO platform. // The push channels for Huawei and VIVO are added in the AndroidManifest.xml file and do not need to be configured here. initConfig.setPushConfig(pushConfig); /** * Set the product scope for the app's device provisioning list. PRODUCT_SCOPE_ALL includes all published and unpublished products in the current project. * PRODUCT_SCOPE_PUBLISHED includes only published products. Select PRODUCT_SCOPE_PUBLISHED for officially released apps. */ IoTSmart.setProductScope(IoTSmart.PRODUCT_SCOPE_PUBLISHED); // Initialize. The app must inherit from AApplication, or an error will occur. IoTSmart.init(app, initConfig);For more information about third-party offline push channels, see Mobile application push development guide.
Set the country.
IoT Platform cloud services are deployed across multiple regions. Whether you need to set the country during initialization depends on your SDK version and the value of the config.regionType parameter.
Parameter value
API Level 8 and earlier
API Level 9 and later
REGION_CHINA_ONLY
Not required
Not required
REGION_ALL
Required
NoteIf you do not set the country, the SDK initialization process is paused, and you cannot use any SDK APIs. To set the country, see Archived Documents.
Not required (The country must be set when you register the app account).
(Optional) Customize the logon and registration page.
To use the built-in account: Refer to SDKInitHelper.postInit in the demo app to customize your logon page. For more information, see Account and user SDK.
To use your own account: Develop your own logon UI. After a user successfully logs on, you must grant authorization for authentication. For more information, see Account and user SDK.
User self-signing code reference
If you use a custom signing method, you must implement the ISecuritySource interface and the getAppKey and sign methods. The following code provides an example.
// Implement the ISecuritySource interface.
public class CustomSecurityImp implements ISecuritySource {
@Override
public String getAppKey() {
// appKey
return "appKey";
}
@Override
public String sign(String input, String signMethod) {
// You must implement MD5, HmacSha1, and HmacSHA256 encryption methods. For encryption methods, see the com.aliyun.alink.linksdk.securesigner.util.Utils class.
String signStr;
if ("MD5".equals(signMethod)) {
signStr = Utils.getMD5String(input);
return signStr.toLowerCase();
} else if ("HmacSHA256".equals(signMethod)) {
// For security, write the encryption method at the C layer and add multiple conversion layers to prevent runtime data from being obtained.
signStr = Utils.hmacSha256("AppSecretKey".getBytes(), input.getBytes());
return signStr.toLowerCase();
} else {
signStr = Utils.hmacSha1("AppSecretKey", input);
return signStr.toLowerCase();
}
}
}SDK API reference
For the API reference for the SDKs provided by IoT Platform, see the SDK API Reference.