Android demo app template

更新时间:
复制 MD 格式

The demo app describes the application development flow and business logic. Before you develop your own application, you must complete the operations for the demo app that are described in this document.

Prerequisites

  • You have created a product on the IoT Platform and completed related operations, such as defining product functions and configuring the application.
  • You have created a proprietary application on the IoT Platform and downloaded the source code for the demo app. For more information, see Create a proprietary app.

Demo app source code structure

Demo源码

The app module handles the business logic of the application. This includes demonstrations of features, such as on-premises device discovery, device provisioning, device attachment, and device status retrieval and control. The following list describes the subdirectories of java/com/aliyun/iot.

  • ilop/startpage: Demonstrates how to customize the country selection page.
  • ilop/demo/SDKInitHelper.java: Demonstrates how to use a unified interface to initialize various software development kits (SDKs).
  • aep/oa: Demonstrates how to customize the mobile area code selection page.
  • aep/sdk/receiver: Demonstrates how to use a broadcast receiver to handle push messages and notifications.
  • ilop/demo/page/login3rd
    • AuthCodeFragment.java: Demonstrates how to use a third-party account for authorization logon.
    • OALoginActivity.java: Demonstrates how to customize the logon page.

Run the demo app

This section describes how to import the demo app using Android Studio and build an APK.

  1. Open Android Studio, select File > Open..., and import the demo app source code.
  2. Integrate the security image. For more information, see Integrate a security image.
    Note Do not rename the security image. Renaming the security image will cause the SDK initialization to fail. After you download the image, place it directly in the `code-based project` folder of the application.
  3. (Optional) Add a camera device to the demo app.
    1. Open the demo app project. In the build.gradle file, find dependencies and add a reference to the Link Visual panel.
      implementation project(':ipcview')
    2. Uncomment the Link Visual panel initialization code.

      In the src\main\java\com\aliyun\iot\ilop\demo\DemoApplication.java file, remove the comment symbols from the following code.

      IPCViewHelper.getInstance().init(this,"2.0.0");
    3. Uncomment the code that redirects to Link Visual.

      In the src\main\java\com\aliyun\iot\ilop\demo\page\ilopmain\HomeTabFragment.java file, remove the comment symbols from the following code.

      Intent intent = new Intent(getActivity(), IPCameraActivity.class);
      intent.putExtra("iotId", iotId);
      intent.putExtra("appKey", EnvConfigure.getEnvArg(EnvConfigure.KEY_APPKEY));
      startActivity(intent);
    4. Replace the product's ProductKey.

      In the src\main\java\com\aliyun\iot\ilop\demo\page\ilopmain\HomeTabFragment.java file, replace the sample ProductKey with your product's ProductKey.

  4. In the demo app folder, run the following command to build the APK.
    ./gradlew clean build
    Note
    • Set the JDK version to 1.8.
    • The packaging plugin version 3.0.0 requires Android Studio 3.0 or later.
    • The packaging plugin version 3.0.0 needs to access Google's repository. Make sure that your network environment allows this access.

Demo app business logic

By default, the demo app is configured for the official project and several supported device types.

  1. Select a country or region.
    Before you log on, you must select a country or region.
    选择国家/地区
    Note If you switch between regions in and outside China, you must restart the app for the change to take effect.
  2. Register or log on to an account.
    When the app starts, the following logon page appears.
    1. Click Register for free to view the registration methods.
    2. Enter your mobile number or email address to register, and then click to send a text message verification code.
    3. Enter the verification code that you received and click Next.
    4. Set a password to log on.
  3. Switch the application language.
    To switch the application language, go to Me > Switch Language. The interface is shown in the following figure.
    切换语言
    Note After you switch the application language, you are redirected to the logon interface. You must log on again for the change to take effect.
  4. Set up the debug interface.

    The DebugTabFragment debug interface calls the com.aliyun.iot.ilop.demo.view.DebugLayout layout and provides the following features.

    • API Channel: API gateway debugging
    • Long-lived Connection Channel: Long-lived connection channel debugging
    • Account and User: Logon and logoff
    • Mobile Push SDK: View information such as AppKey, ClientID, and DeviceID

    The BoneMobile plugin debugging includes the following features:

    • Switching between development and production environments
    • On-premises debugging
    调试版App
  5. Control a virtual device.
    1. Go to the main interface, as shown in the following figure.
      Demo主界面

      If you scan the QR code to download and try the demo app, five virtual devices are created by default after you log on.

      If you download the code to compile and run the app, the five virtual devices are not created by default. You must modify the code in HomeTabFragment. Enter the ProductKeys of the products that are created in your project into the `pks` array. Then, you can compile and run the app to create and test the virtual devices.

      /**
       * Enter the pks from your project to experience virtual devices.
       */
      //String[] pks = new String[]{"a1AzoSi5TMc", "a1B6cFQldpm", "a1XoFUJWkPr", "a1nZ7Kq7AG1", "a1OX20YJSLk"};
      String[] pks = new String[]{};
    2. Click a device to go to its device panel interface.
      面板界面
  6. Add a device.
    1. Click the plus sign in the upper-right corner of the homepage.
    2. Select a device to add. You are then directed to the add device interface.
      添加设备界面

      You can add devices in the following two ways.

      • Select a device from a product list, such as the supported devices shown in the figure.
      • Select a device that is discovered on the on-premises network or a device that supports zero-configuration networking. These devices are automatically displayed if they are found, such as the on-premises device shown in the figure.
      Note To add a device from the product list, you must use a product that is connected to the official project. No physical devices are available for this step. To test this flow, you must develop your own application.

Create a virtual device for a product

If you do not have a physical device, you can use a virtual device for development and testing. This section describes how to create a virtual device for your product.

  1. Open the HomeTabFragment.java file in the com/aliyun/iot/ilop/demo/page/ilopmain/ folder of the demo project.
  2. Search for the keyword pks to find the code that you need to modify.
    // Register a virtual device
    Set<String> set = new HashSet<String>();
    ArrayList<String> deviceStrList = new ArrayList<>();
    for (DeviceInfoBean deviceInfoBean : deviceInfoBeanList) {
        set.add(deviceInfoBean.getProductKey());
        deviceStrList.add(deviceInfoBean.getProductKey() + deviceInfoBean.getDeviceName());
    }
    mBundle.putStringArrayList("deviceList", deviceStrList);
    //noinspection MismatchedReadAndWriteOfArray
    String[] pks = new String[]{/*"a1xxxxc", "a1xxxxxm", "a1xxxxr", "a1xxxx1", "a1xxxxk"*/};
    mRegisterCount = 0;
    mVirtualCount = 0;
    //noinspection RedundantOperationOnEmptyContainer
    for (String pk : pks) {
        if (set.add(pk)) {
            mRegisterCount++;
            //registerVirtualDevice(pk);
            addVirtualDevice(pk);
        }
    }
  3. In the console, obtain the productKey of the product that you want to test.
    设备PK
  4. Replace the productKey.
    Replace the default productKey in String[] pks = new String[]{/*"a1xxxxc", "a1xxxxxm", "a1xxxxr", "a1xxxx1", "a1xxxxk"*/}; with your product's productKey. The following code provides an example.
    String[] pks = new String[]{/*"xxxx", "xxxxx"*/};
    Note You can request one virtual device for each product. If you have multiple ProductKeys, separate them with commas.
  5. Log on to the application again. You can now use the virtual device to test the controls for your product.