Teach you to integrate Nearby Service to achieve one-click Wi-Fi sharing

Preface

Friends come as guests at home, customers come to eat in the store... When they want to connect their mobile phone to Wi-Fi, they will inevitably ask for the Wi-Fi name and password. This process of asking for a password and giving it is very troublesome, and there are often cases where the password is misheard or entered, and more importantly, the password will be leaked unintentionally. How to realize the one-click login link Wi-Fi without manually entering the password?

Huawei Nearby Service provides you with a new feature of "Wi-Fi Sharing". With this feature, the user only needs to select the device to be connected in the application and confirm to assist it in connecting to the network, and then one-click access to the Wi-Fi network can be achieved.

Wi-FiShareDemo

Wi-FiShareDemo is an example application that has integrated Nearby Service to realize Wi-Fi sharing. This section introduces how to run this example application and the key code of the example application.

Tool preparation

  • 1 Huawei mobile phone (the sharing party needs to be a Huawei mobile phone)

  • 1 Android device (the recipient needs to be a device with Android 5.0 or higher and HMSCore installed, which can be a mobile phone, a TV box and other Android devices)

  • The sharing party is connected to Wi-Fi, and the recipient is not connected

  • Development Tools Android Studio (3.X or later)

Build a sample application

  • Import the sample source code in Android Studio.

  • Register as a Huawei developer .

  • Refer to the development of Nearby Service and prepare to create your application in the Huawei application market. Note: You need to download the "agconnect-services.json" file and refer to the instructions to generate a signed certificate and place it in the app/ directory.

Insert picture description here

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

Insert picture description here

Steps

If you have completed the compilation and construction according to the above steps and installed it on your phone, you can experience it as follows. Note: Test machine A must be a Huawei mobile phone and connected to Wi-Fi, test machine B can be another Android mobile phone and not connected to the target Wi-Fi.

  1. Open Wi-FiShareDemo on test machine B and click "Connect Wi-Fi"

Insert picture description here

  1. Test machine A is connected to Wi-Fi, open Wi-FiShareDemo, click Share Wi-Fi"

Insert picture description here

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

Insert picture description here

  1. Click the prompt "ALLOW" in the pop-up box on test machine B to allow connection to WLAN.

Insert picture description here

Key code description

The source code of the sample application is hosted on GitHub, you can directly view the source code. In this sample application, the class WifiShareHelper.java is encapsulated based on the Nearby Service interface. There
are only two classes MainActivity.java and WifiShareHelper.java in the source code. The above Wi-Fi sharing process only uses the connection in WifiShareHelper.java Two interfaces for Wi-Fi and sharing Wi-Fi.

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

More details

Official website of Huawei Developer Alliance:
https://developer.huawei.com/consumer/en/hms/huawei-nearbyservice?ha_source=hms1

Obtain development guidance documents:
https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides-V5/introduction-0000001050040566-V5?ha_source=hms1

To participate in developer discussions, please go to the Reddit community:https://www.reddit.com/r/HuaweiDevelopers/

To download the demo and sample code, please go to Github:https://github.com/HMS-Core

To solve integration problems, please go to Stack Overflow:
https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest


Original link:https://developer.huawei.com/consumer/cn/forum/topic/0204415936476600459?fid=18

Author: Pepper

Guess you like

Origin blog.51cto.com/14772288/2568372