Intermediate: WLAN (wireless local area network) in Harmony OS

Lokesh Suryan
4 min readSep 24, 2021

--

Introduction

Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover WLAN with Java in Harmony OS.

A wireless LAN (WLAN) is a wireless local area network that links two or more devices using wireless communication to form a local area network (LAN) within a limited area such as a campus, school, computer laboratory home, or office building etc. A wireless local area network (WLAN) uses the radio, infrared, or other technologies to transmit data between devices that are not physically connected with each other. This gives users the ability to move around within the area and remain connected to the network.

When to Use

  • Check whether WLAN is enabled.
  • Obtain the WLAN connection and IP information.
  • Obtain the country/region code of a device.
  • Start a scan and obtain the scan result.
  • Check whether the device supports a specified feature.

Development Overview

You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.

Hardware Requirements

  • A computer (desktop or laptop) running Windows 10.
  • A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

  • Java JDK installation package.
  • DevEcho studio installed.

Follows the steps.

1. Create HarmonyOS Project.

  • Open DevEcho studio.
  • Click NEW Project, select a Project Templet.
  • Select ability template and click Next as per below image.
  • Enter Project and Package Name and click on Finish.

2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.

3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values. To use the functions of the network management module, you must obtain the below permissions.

4. Create New Ability, as follows.

5. Development Procedure.

Create MainAbilitySlice.java ability and add the below code.

Create ability_main.xml layout and add the below code.

Create button_background.xml in graphic folder and add the below code.

6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.

Result

Provide location permission and Click on UI ‘Scan button. It will show result as per screen.

Click on UI ‘Get Connected Info’ button. It will show connected device and ip address as per below screen.

Click on UI ‘Get Country Code’ button. It will show connected device Country code as per below screen.

Click on UI ‘Get Support Feature button. It will supported feature by connected device as per below screen.

Tips and Tricks

  • Always use the latest version of DevEcho Studio.
  • Use Harmony Device Simulator from HVD section.
  • Network task or any long running task should run in background thread.
  • Make sure network permissions added into config.json file.

Conclusion

In this article, we have learnt how connect two or more devices using wireless communication to form a local area network(LAN) within a limited area such as a campus, school, computer laboratory home, or office building etc. A wireless local area network (WLAN) uses the radio, infrared, or other technologies to transmit data between devices that are not physically connected with each other.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1

WLAN: https://developer.harmonyos.com/en/docs/documentation/doc-guides/connectivity-wlan-overview-0000000000030016?ha_source=hms1

--

--

No responses yet