This topic describes how to configure a device SDK to connect a gateway to IoT platform.

Configure the device SDK for a gateway

In this example, the DeviceTopoManager file in the directory java/src/main/java/com/aliyun/iot/api/common/deviceApi contains the code for connecting the gateway to IoT Platform.

  • Configure the gateway information for connection.
    
        private static String regionId = "cn-shanghai";
        private static final String TAG = "TOPO";
    
        //The gateway.
        private static String GWproductKey = "a1BxptK***";
        private static String GWdeviceName = "XMtrv3yvftEHAzrTfX1U";
        private static String GWdeviceSecret = "19xJNybifnmgcK057vYhazYK4b64****";
    
    
        public static void main(String[] args) {
            /**
             * The information about the MQTT connection.
             */
            DeviceTopoManager manager = new DeviceTopoManager();
    
            /**
             * The Java HTTP client of this device supports TSLv1.2.
             */
            System.setProperty("https.protocols", "TLSv2");
    
            manager.init();
        }
  • Establish a connection.
    
    public void init() {
            LinkKitInitParams params = new LinkKitInitParams();
            /**
             * Specifies the parameters for the MQTT initialization.
             */
            IoTMqttClientConfig config = new IoTMqttClientConfig();
            config.productKey = GWproductKey;
            config.deviceName = GWdeviceName;
            config.deviceSecret = GWdeviceSecret;
            config.channelHost = GWproductKey + ".iot-as-mqtt." + regionId + ".aliyuncs.com:1883";
            /**
             * Specifies whether to receive offline messages.
             * The cleanSession field corresponding to the MQTT connection.
             */
            config.receiveOfflineMsg = false;
            params.mqttClientConfig = config;
            ALog.setLevel(LEVEL_DEBUG);
            ALog.i(TAG, "mqtt connetcion info=" + params);
    
            /**
             * Specifies the initialization and passes in the certificate information of the gateway.
             */
            DeviceInfo deviceInfo = new DeviceInfo();
            deviceInfo.productKey = GWproductKey;
            deviceInfo.deviceName = GWdeviceName;
            deviceInfo.deviceSecret = GWdeviceSecret;
            params.deviceInfo = deviceInfo;
    
            /** Establishes a connection. **/
            LinkKit.getInstance().init(params, new ILinkKitConnectListener() {
                public void onError(AError aError) {
                    ALog.e(TAG, "Init Error error=" + aError);
                }
    
                public void onInitDone(InitResult initResult) {
                    ALog.i(TAG, "onInitDone result=" + initResult);
    
    
                    //Queries the topological relationships of this gateway, and checks whether a topological relationship exists between the gateway and the sub-device.
                    //If the topological relationship exists, reports the sub-device information to IoT Platform.
                    getGWDeviceTopo();
    
                    //Dynamically registers the sub-device to obtain the DeviceSecret of the sub-device.If the gateway has obtained the certificate of the sub-device, skips this step.
                    //When you create the sub-device in IoT Platform, you can use  the serial number or MAC address of the physical device as the DeviceName of the sub-device.
                    gatewaySubDevicRegister();
    
                    //The certificate information of the sub-device.
                    gatewayAddSubDevice();
    
                    //Connects the sub-device to IoT Platform through the gateway.
                    gatewaySubDeviceLogin();
    
                }
            });
        }

Test the connection

After you configure the gateway in the device SDK, you can test the connection between the device SDK and IoT platform.

  1. Run DeviceTopoManager.
  2. Go to the IoT Platform console, and in the left-side navigation pane, choose Devices.
  3. In the Device List section, find the gateway and check the state of the device. If status of the gateway device is Online, the gateway is connected to IoT platform.
    Connect a device to IoT Platform

What to do next

Connect the sub-device to IoT Platform