# Calling of Network Configuration APIs - [Whether WifiAware Is Supported](#section1488020125510) - [Obtaining the Wi-Fi List](#section743413288512) - [Discovering Devices Through NAN](#section1504174410511) - [Discovering a Device Through SoftAP](#section55681185218) - [Connecting a Device](#section4392324115210) - [Configuring the Network for a Device](#section772433526) - [Releasing the Network Configuration Channel](#section194641109530) - [Controlling the Device in Seconds](#section144171232175313) - [Registering Message Callbacks](#section7232650195315) - [Callback for Device Disconnection](#section176579445411) All APIs related to the network configuration are in **login/fa-netconfig.js**. For details about how to reference these APIs, see **import netConfig from'fa-netconfig'** in **app.js**. ## Whether WifiAware Is Supported

Function Prototype

NetConfig.isSupportWifiAware()

Description

Whether WifiAware is supported.

API Type

Synchronous API

Return Value

0 (supported) and -1 (not supported)

- Parameters None - Example ``` getApp(this).NetConfig.isSupportWifiAware().then(function (result) { let ret = JSON.parse(result); console.info"isSupportWifiAware message result: code=" + ret.code +", data=" + ret.data) if (ret.code == 0) { // WifiAware supported. } else { // WifiAware not supported. } }); ``` ## Obtaining the Wi-Fi List

Function Prototype

NetConfig.getWifiList(callbackFunc)

Description

Obtains the list of nearby Wi-Fi networks.

API Type

Asynchronous API

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback.

Data returned: List<WifiApInfo>

WifiApInfo {

int channel;

boolean hasDefaultPassword;

boolean is5G; // Specifies whether 5 GHz Wi-Fi is used.

String securityType;

int signalLevel;

String ssid;

String wifiApId; // wifiAp ID

}

Both 2.4 GHz Wi-Fi and 5 GHz Wi-Fi are returned. The atomic service can filter data based on the value of is5G.

The value of hasDefaultPassword is true only when the SSID has been connected before and the password can be successfully obtained.

- Example ``` getApp(this).NetConfig.getWifiList((result) => { if (result.code == 0 && result.data && result.data.length > 0) { this.progress = this.progressStatus[3]; this.desc = this.descStatus[3]; this.configDevice(result.data[0]); } else { this.progress = this.progressStatus[4]; this.desc = this.descStatus[3]; this.disconnectDevice(); } }); ``` ## Discovering Devices Through NAN

Function Prototype

NetConfig.discoveryByNAN(scanNanInfo, callbackFunc)

Description

Initiates a NAN broadcast, waits for a client connection, establishes the connection, and stops the broadcast.

API Type

Asynchronous API

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

scanNanInfo

Object

Yes

scanNanInfo {

int duration // If a connection is not set up within the specified duration, the broadcast stops. The value range is [0,100], and the unit is seconds. The value 0 indicates an unlimited duration.

int lockTime // Locking duration of a device in NAN mode after the device receives an NAN mutual trust message. The value range is [0,100], and the unit is seconds. The value 0 indicates an unlimited duration.

String sessionId // The value is the sessionId value carried by the intent when the service starts the atomic service.

}

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback. Data returned:

DeviceInfo{

String productId,

String sessionId

String sn,

}

- Example ``` let scanInfo = { duration: 30, lockTime: 60, sessionId: '' }; getApp(this).NetConfig.discoveryByNAN(scanInfo, (result) => { if (result.code == 0) { this.progress = this.progressStatus[1]; this.desc = this.descStatus[1]; getApp(this).ConfigParams.deviceInfo = result.data; this.registerDisconnectCallback(result.data.sessionId); this.connectDevice(); } else { this.progress = this.progressStatus[4]; this.desc = this.failDescStatus[1]; this.disconnectDevice(); } }); ``` ## Discovering a Device Through SoftAP

Function Prototype

NetConfig.discoveryBySoftAP(callbackFunc)

Description

Scans for access points (APs).

API Type

Asynchronous API

Input Parameter

The number of calling times is determined by the atomic service.

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback.

Data returned: List<SoftAPInfo> SoftAPInfo{

String ssid,

boolean usePassword // Specifies whether a password is required.

}

- Example ``` getApp(this).NetConfig.discoveryBySoftAP((result) => { if (result.code == 0) { for (let index = 0; index < result.data.length; index++) { let element = result.data[index]; console.info("discoveryBySoftAP callback: device element=" + JSON.stringify(element)); // Compare the Wi-Fi list scanned through SoftAP with the target device's SSID format, category, and MAC address. if (element.ssid.indexOf(self.softAPSsidDefault) != -1) { this.discoverDeviceBySoftAPResult = JSON.stringify(element); this.softAPSsidDefault = element.ssid; break; } } } }); ``` ## Connecting a Device

Function Prototype

NetConfig.connectDevice(connectInfo,callbackFunc)

Description

Connects a device.

API Type

Asynchronous API

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

connectInfo

Object

Yes

connectInfo {

string targetDeviceId; // Mandatory. ID of the device to connect.

  • number type; // Mandatory. The value can be 0 (NAN) and 1 (SoftAp).
  • string pin; // Mandatory. Device PIN.
  • string password; //Mandatory. If the device requires a password, set this parameter to the required password. Otherwise, set this parameter to "".
  • string sessionId; // Mandatory. If type is set to NAN, set this parameter to the value of sessionId returned by discoverDevByNAN. Otherwise, set this parameter to "".

    }

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback.

Data returned: String vendorData // Reserved.

In SoftAP mode, {

String productId,

String sn,

String vendorData

} JSON string

- Example of the NAN type ``` let connectInfo = { targetDeviceId: getApp(this).ConfigParams.deviceInfo.productId, type: 0, pin: '0123456789012345', password: getApp(this).ConfigParams.deviceInfo.sn, sessionId: getApp(this).ConfigParams.deviceInfo.sessionId }; getApp(this).NetConfig.connectDevice(connectInfo, (result) => { if (result.code === 0) { this.progress = this.progressStatus[2]; this.desc = this.descStatus[2]; this.getWifiInfo(); } else { this.progress = this.progressStatus[4]; this.desc = this.failDescStatus[2]; this.disconnectDevice(); } }); ``` - Example of the SoftAP type ``` let connectInfo = { targetDeviceId: getApp(this).ConfigParams.deviceInfo.productId, type: 1, pin: '0123456789012345', password: getApp(this).ConfigParams.deviceInfo.sn, sessionId: getApp(this).ConfigParams.deviceInfo.sessionId }; getApp(this).NetConfig.connectDevice(connectInfo, (result) => { if (result.code === 0) { this.progress = this.progressStatus[2]; this.desc = this.descStatus[2]; this.getWifiInfo(); } else { this.progress = this.progressStatus[4]; this.desc = this.failDescStatus[2]; this.disconnectDevice(); } }); ``` ## Configuring the Network for a Device

Function Prototype

NetConfig.configDeviceNet(deviceInfo,accountInfo,netConfigInfo,callbackFunc)

Description

Starts to configure the network for the specified device by sending the SSID, password, device information, and account information.

API Type

Asynchronous API

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

deviceInfo

string

Yes

Information allocated by the device cloud to the device, such as the device ID.

accountInfo

string

Yes

Account information.

netConfigInfo

Object

Yes

netConfigInfo {

string ssid;

// Mandatory. If the SSID has been configured for the phone, you do not need to enter a password.

string ssidPassword; // Optional. If the password does not need to be delivered, set this parameter to "".

boolean isDefaultPassword; // Mandatory.

int channel; // Mandatory.

string sessionId; // Mandatory. When type is set to NAN, enter the actual value. Otherwise, set this parameter to "".

int type; // Mandatory. The value can be 0 (NAN) and 1 (SoftAp).

String wifiApId; // Enter the corresponding fields in the returned Wi-Fi network list information.

String vendorData; // Product data, which is encoded using random Base64.

int timeout; // Network configuration timeout interval, which is set in NAN mode. The value range is [1,90], and the unit is seconds.

}

Note: To require the user to enter the password, set isDefaultPassword to false and ssidPassword to the entered password; otherwise, set isDefaultPassword to true and ssidPassword to "".

callbackFunc

function

Yes

Callback function. In NAN mode, if the callback is successful, the module reports the network configuration status. The result is converted into two parts: the integer part (stored in data.code) and the string type (stored in data.msg). In SoftAP mode, either 0 or -1 is returned, indicating that the network configuration is successful or fails.

- Example of the NAN type ``` let netConfigInfo = { ssid: wifiApInfo.ssid, ssidPassword: '', isDefaultPassword: true, channel: wifiApInfo.channel, sessionId: getApp(this).ConfigParams.deviceInfo.sessionId, type: 0, wifiApId: wifiApInfo.wifiApId, vendorData: '', timeout: 30, paramValid: true }; getApp(this).NetConfig.configDeviceNet('deviceInfo', 'accountInfo', netConfigInfo, (result) => { if (result.code == 0) { this.progress = this.progressStatus[4]; this.desc = this.descStatus[4]; this.registerMsgReceive() this.goToControl(); } else { this.progress = this.progressStatus[4]; this.desc = this.failDescStatus[4]; this.disconnectDevice(); } }); ``` - Example of the SoftAP type ``` let netConfigInfo = { ssid: wifiApInfo.ssid, ssidPassword: '', isDefaultPassword: true, channel: wifiApInfo.channel, sessionId: getApp(this).ConfigParams.deviceInfo.sessionId, type: 1, wifiApId: wifiApInfo.wifiApId, vendorData: '', timeout: 30, paramValid: true }; getApp(this).NetConfig.configDeviceNet('deviceInfo', 'accountInfo', netConfigInfo, (result) => { if (result.code == 0) { this.progress = this.progressStatus[4]; this.desc = this.descStatus[4]; this.registerMsgReceive() this.goToControl(); } else { this.progress = this.progressStatus[4]; this.desc = this.failDescStatus[4]; this.disconnectDevice(); } }); ``` ## Releasing the Network Configuration Channel

Function Prototype

NetConfig.disconnectDevice(commonInfo,callbackFunc)

Description

Releases the network configuration channel. When the atomic service does not use the NAN channel or the atomic service exits, the atomic service needs to proactively call this function.

API Type

Asynchronous API

Input Parameter

commonInfo

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

commonInfo

Object

Yes

commonInfo{

String sessionId; // If type is NAN, set this parameter to the value of sessionId returned by discoverDevByNAN. Otherwise, set this parameter to "".

}

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback.

Data returned: 0 (success) or -1 (failure)

- Example ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.disconnectDevice(commonInfo, (result) => { if (result.code == 0) { return; } }); ``` ## Controlling the Device in Seconds

Function Prototype

NetConfig.sendMessage(commonInfo,message,callbackFunc)

Description

Sends a message to the device for control purposes.

API Type

Asynchronous API

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

commonInfo

Object

Yes

commonInfo{

String sessionId; // If type is NAN, set this parameter to the value of sessionId returned by discoverDevByNAN. Otherwise, set this parameter to "".

}

message

String

Yes

Message content.

callbackFunc

function

Yes

Callback function, which returns the command sending result.

Data returned: 0 (success) or -1 (failure)

- Example ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.sendMessage(commonInfo, "111111", (result) => { // sendMessage callback }) ``` ## Registering Message Callbacks

Function Prototype

NetConfig.registerMsgReceive(commonInfo, callbackFunc)

Description

Obtains messages on the device side. You are advised to call this function before calling the device connection API in NAN mode.

API Type

Asynchronous API

Input Parameter

See the table below.

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

commonInfo

Object

Yes

commonInfo{

String sessionId; // If type is NAN, set this parameter to the value of sessionId returned by discoverDevByNAN. Otherwise, set this parameter to "".

}

callbackFunc

function

Yes

Callback function. If the callback is successful, data contains the returned data result. The data result is parsed and used in the callback.

Data returned: String message

- Example ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.registerMsgReceive(commonInfo, () => { // registerMsgReceive callback }); ``` ## Callback for Device Disconnection

Function Prototype

NetConfig.registerDisconnectCallback(commonInfo, callbackFunc)

Description

Registers the callback for device disconnection events. You are advised to call this function before calling the device connection API in NAN mode.

When the device is disconnected from the phone unexpectedly, the callback is triggered.

API Type

Asynchronous API

Input Parameter

See the table below.

Return Value

0 (success) and -1 (failure)

- Parameters

Name

Type

Mandatory

Description

commonInfo

Object

Yes

commonInfo{

String sessionId; // If type is NAN, set this parameter to the value of sessionId returned by discoverDevByNAN. Otherwise, set this parameter to "".

}

callbackFunc

function

Yes

Callback function. This callback is triggered when the device is disconnected from the phone.

Status code returned: 0

- Example ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.registerDisconnectCallback(commonInfo, () => { // registerDisconnectCallback }); ```