preface

Friends visit, customers dine in… When they want to connect their phones to Wi-Fi, they invariably ask for the wi-fi name and password. The process of asking and giving passwords is very troublesome, and there are often misheard or mistyped passwords. More importantly, passwords will be leaked unintentionally. How to implement a one-click login to Wi-Fi without manually entering a password?

Huawei’s Nearby Service offers you a new feature called “Wi-Fi sharing”. With this feature, users only need to select the device to be connected in an application and determine to assist the device to be connected to a Wi-Fi network in one click.

Wi-FiShareDemo

Wi-fi HareDemo is a sample application that integrates with Nearby Service to implement Wi-Fi sharing. This section describes how to run the sample application and the key code of the sample application.

Tools to prepare

  • 1 Huawei mobile phone (sharing party must be Huawei mobile phone)

  • 1 Android device (the recipient should be an Android device with Android 5.0 or above and HMSCore installed, which can be mobile phone, TV box and other Android devices)

  • Wi-fi is connected to the sharing party, but not to the receiving party

  • Android Studio(3.x or later)

Building the sample application

  • Import the sample source code in Android Studio.

  • Register as a Huawei developer.

  • Refer to Nearby Service Development to prepare to create your app in huawei App Marketplace. Note: you need to download the “agconnect-services.json” file and refer to the instructions to generate the signing certificate and place it in the app/ directory.

  • Run the application in Android Studio to install it on the test machine.

steps

If you have completed the compilation and installation as described in the preceding steps, you can perform the following steps to experience the installation. Note: Test machine A must be A Huawei phone and connected to Wi-Fi, while test machine B can be another Android phone and not connected to target Wi-Fi.

  1. Open Wi-Fi HareDemo on test machine B and click “Connect Wi-Fi”

  1. Test machine A is connected to Wi-Fi. Open Wi-Fi FiShareDemo and click Share Wi-Fi.

  1. On test machine A, select the mobile phone to be shared, ensure that the verification codes on the two mobile phones are the same, and select ALLOW.

  1. Click ALLOW in the dialog box displayed on test machine B to ALLOW the WLAN connection.

Key Code Description

The source code for the sample application is hosted on GitHub, where you can view the source code directly. In this example application, the wifishareHelper. Java class is encapsulated based on the Nearby Service interface, providing only two classes mainActivity. Java and wifishareHelper. Java in the source code. Wifisharehelper. Java only uses the two interfaces for connecting to Wi-Fi and sharing Wi-Fi.

  1. Connected wi-fi (requestWiFiConfig)
// The device request to connect WiFi
public void requestWiFiConfig(a) {
    Log.d(TAG, "requestWiFiConfig");
    mWifiShareEngine.startWifiShare(mWifiShareCallback, WifiSharePolicy.POLICY_SET)
            .addOnFailureListener(e -> Log.e(TAG, Objects.requireNonNull(e.getMessage())));
}
Copy the code
  1. Share the wi-fi (shareWiFiConfig)
// The device to share wifi
public void shareWiFiConfig(a) {
    Log.d(TAG, "Start to share WiFi");
    mWifiShareEngine.startWifiShare(mWifiShareCallback, WifiSharePolicy.POLICY_SHARE)
            .addOnFailureListener(e -> Log.e(TAG, Objects.requireNonNull(e.getMessage())));
    showListView();
    setListViewListenerMode();
}
Copy the code

For more details

Huawei developer alliance website: developer.huawei.com/consumer/en…

To obtain development guidance document: developer.huawei.com/consumer/en…

Participate in developer discussion please to Reddit community: www.reddit.com/r/HuaweiDev…

Download the demo and sample code at Github: github.com/HMS-Core

Solve the integration problem please to Stack Overflow: stackoverflow.com/questions/t…


The original link: developer.huawei.com/consumer/cn…

Original author: Pepper