background

Small program wifi series interface for the system’s native capabilities. As early as 2015, wechat launched “wechat Wi-fi”, which provides a complete and convenient solution for merchants’ offline locations to connect tO Wi-Fi through wechat. It is now a complete and convenient solution. Customers can connect to wifi by scanning codes, and wechat can also send messages to users. The scheme has been described in detail in the official documents, so this paper will not repeat it. This article focuses on the problems and experiences of using wifi capability in small programs.

[wechat with wifi] Portal

Interpretation of wifi capability

Introduction to wifi Capability

In applets, using the wifi module requires first calling wx.startwifi () to initialize the wifi module. The following capabilities need to be used in the SUCCESS callback of Wx. startWifi.

Connect the wifi

wx.connectWifi({
    SSID: 'the mx iphone', // Wi-Fi device SSID' '// Wi-fi device BSSID password:'xxxxxxxx'.success() {},fail() {},complete(){}
})
Copy the code

ConnectWifi provides us with the ability to connect directly to wifi, which is only supported by Android and iOS 11 and above, requiring base library 1.6.0 or above. This is usually the core API for using wifi functionality. In ios, a system pop-up box will appear, asking the user whether to connect to wifi. Only when the user clicks “OK”, connectWifi will continue. Otherwise, fail callback will be performed. On android (xiaomi note3, android 9), wechat toasts appear in a row.

Get the wifi that is currently connected

wx.getConnectedWifi({
    success(WifiInfo){
        // WifiInfo
    }
})
Copy the code

According to the information obtained from the community, signalStrength refers to signalStrength; iOS is returned by the system, and the value is 0-1; android is converted, and the value is 0-100. The value is directly proportional to the signal intensity.

Get wifi List

wx.getWifiList({
    success(e) {
        wx.onGetWifiList((res) {
            // res.wifiList:wifiInfo[]
        })
    }
})
Copy the code

To obtain the list of surrounding wifi, use getWifiList first and then onGetWifiList to listen. In ios, getWifiList will jump to the ios interface, which is inevitable due to the limitations of ios. (In fact, there is nothing wrong with doing this, wifi information is sensitive, but it has a certain impact on user experience.) On Android, getWifiList needs to get a user’s location because it can infer a user’s location using a small program that sniffs out nearby Wi-Fi hotspots. In order to ensure that the privacy of users is not infringed, it is necessary to obtain the user’s geographical location (scope.userLocation) since 7.0.4 of wechat Android client. For details, see wx.getWifiList. The interface needs to obtain user location information before using it

Set AP information

wx.onGetWifiList(function(res) {
    wx.setWifiList({
      wifiList: [{
        SSID: res.wifiList[0].SSID,
        BSSID: res.wifiList[0].BSSID,
        password: '123456'}]})})Copy the code

other

  • wx.stopWifiTurn off the wifi module
  • wx.onWifiConnected(function callback)Listen for wi-fi connection events
  • wx.offWifiConnectedCancel listening for wi-fi connection events
  • wx.offGetWifiList(function callback)Cancel Listening The event that Wi-Fi list data is obtained.

Connect Android to wifi

Connect to specified Wi-Fi interface call sequence:

Android: startWifi -- > Connect -- > onWifiConnectedCopy the code

Call sequence of the surrounding Wi-Fi interface:

Android: startWifi -- > getWifiList -- > onGetWifiList -- > connectWifi -- > onWifiConnectedCopy the code

IOS connect wifi

Connect to the specified wi-fi interface call sequence :(same as android)

IOS (supported only on iOS 11 and later) : startWifi -- > connectWifi -- > onWifiConnectedCopy the code

Call sequence of the surrounding Wi-Fi interface:

IOS (iOS 11.0 and 11.1 are not supported due to system problems) : startWifi - > getWifiList - > onGetWifiList - >setWifiList - > onWifiConnectedCopy the code

practice

In my project, I was exposed to scenarios using the hardware (wifi) capabilities provided by applets. Using the ability of wifi and socket, we can connect wifi for iot devices. The process is as follows:

  • Applets and device data transfer (WebSocket);
  • Data transfer between applets and devices (UDpSocket);

Wx. Request/wx. ConnectSocket/wx. UploadFile/wx downloadFile url parameter allows for{PORT}/${PATH} format, the request/connection will be successful only if the IP address and the mobile IP address are in the same network segment and not the same as the local IP address (generally speaking, the same LAN, such as the connection under the same wifi). In this case, security domain verification is not performed and HTTPS/WSS is not required, but HTTP/WS can be used instead.

In LAN communication, security domain verification is not performed. Therefore, you do not need to add a security domain name in the MP background.

conclusion

With the hardware capabilities provided by applets (wifi, Bluetooth, NFC), applets have a broader landscape. For example, the mini program of [Mobike], some cars require users to turn on bluetooth, and the bluetooth function of the native system provided by the mini program enables us. Through the small program, we can also realize communication with the Internet of Things devices, by connecting to the device hot spot, using HTTP/WSS/UDP transmission data. Of course, there are still problems with wifi capability, such as compatibility problems, and some Android phones may report inexplicable errors when connecting. In this case, you need to view the specific cause in the errcode provided by the official.

The pit of tread

  1. For some android models, wifi has been connected to useconnectWifiReconnect, even if you pass the wrong password, you can still connect.

    A: There is no solution to this problem at present.
  2. Ios ingetWifiListWill jump to the system Settings page, inconnectWifiModal appears to confirm whether a connection is made.

    A: It can’t be avoided. Restrictions on ios
  3. Unable to debug wifi capability in developer tools while debugging wifi feature. A: The developer tools are currently unavailable for emulation. In addition, an error will be reported after the device hotspot is connected during debugging of the real machine. (After all, the real machine debugging needs to be able to access the network, generally the device hotspot does not have the ability to access the network). It can be solved by preview + vconsole at present, but it is still quite troublesome.
  4. Can I disconnect wifi voluntarily? A: According to the documentation, this capability is not provided. But try connecting to a non-existent wifi.
  5. Notes from the documentation:

Reference

  • Wechat open documents – wifi
  • Wechat Open Documents – LAN communication
  • Small program WIFI interface and WebSocket to create LAN data communication