# 配网接口调用 - [是否持WifiAware](#section1488020125510) - [获取Wifi列表](#section743413288512) - [通过NAN发现设备](#section1504174410511) - [通过SoftAP发现设备](#section55681185218) - [连接设备](#section4392324115210) - [对指定设备进行配网](#section772433526) - [释放配网通道](#section194641109530) - [秒控接口](#section144171232175313) - [注册消息回调](#section7232650195315) - [设备断链通知回调消息](#section176579445411) 配网相关接口都在login/fa-netconfig.js下。 参考app.js中的import netConfig from 'fa-netconfig'进行引用。 ## 是否持WifiAware

函数原型

NetConfig.isSupportWifiAware()

说明

WifiAware是否支持。

接口类型

同步接口

返回值

0(支持)/ -1(不支持)

- 参数 无 - 示例: ``` getApp(this).NetConfig.isSupportWifiAware().then(function (result) { let ret = JSON.parse(result); console.info("isSupportWifiAware消息结果: code=" + ret.code + ", data=" + ret.data); if (ret.code == 0) { //支持WifiAware } else { //不支持WifiAware } }); ``` ## 获取Wifi列表

函数原型

NetConfig.getWifiList(callbackFunc)

说明

获取周边wifi列表。

接口类型

异步接口

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。

返回:List<WifiApInfo>

WifiApInfo {

int channel;

boolean hasDefaultPassword;

boolean is5G; // 是否是5G wifi

String securityType;

int signalLevel;

String ssid;

String wifiApId; // wifiAp ID

}

2.4G和5G都返回,原子化服务只需通过is5G是否支持5G进行筛选过滤;

周边范围内的已配置过,并且能够成功获取密码的ssid,hasDefaultPassword才为true;

- 示例 ``` 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(); } }); ``` ## 通过NAN发现设备

函数原型

NetConfig.discoveryByNAN(scanNanInfo, callbackFunc)

说明

广播NAN服务,等待客户端连接,建立起一条连接,停止广播。

接口类型

异步接口

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

scanNanInfo

Object

scanNanInfo {

int duration; 在指定的duration时间内未建立起连接,停止广播,单位为秒,范围[0,100],0表示不限制时长。

int lockTime; 设备收到NAN互信消息后在NAN模式的锁定时长,单位为秒,范围[0,100],0表示不限制时长。

String sessionId; 值为service拉起原子化服务时的Intent携带的sessionId值。

}

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。返回:

DeviceInfo{

String productId,

String sessionId

String sn,

}

- 示例: ``` 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(); } }); ``` ## 通过SoftAP发现设备

函数原型

NetConfig.discoveryBySoftAP(callbackFunc)

说明

扫描AP。

接口类型

异步接口

输入参数

由原子化服务自行确定调用次数

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。

返回:List<SoftAPInfo> SoftAPInfo{

String ssid,

boolean usePassword // 是否需要使用密码

}

- 示例: ``` 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回调:device element=" + JSON.stringify(element)); // softAP扫描到的列表中,与既定的设备的ssid格式+品类+mac地址进行比对 if (element.ssid.indexOf(self.softAPSsidDefault) != -1) { this.discoverDeviceBySoftAPResult = JSON.stringify(element); this.softAPSsidDefault = element.ssid; break; } } } }); ``` ## 连接设备

函数原型

NetConfig.connectDevice(connectInfo,callbackFunc)

说明

连接设备。

接口类型

异步接口

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

connectInfo

Object

connectInfo {

string targetDeviceId; //必填,待连接设备的标识。

  • number type; //必填,0(NAN)/ 1(SoftAp)。
  • string pin; //必填,设备pin。
  • string password; //必填,若设备需要密码,则设置为所需密码;否则设置为""。
  • string sessionId; //必填,若type为NAN,则设置为discoverDevByNAN 返回的sessionId;否则设置为""。

    }

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。

返回:String vendorData // 后续扩展使用。

Softap模式下返回{

String productId,

String sn,

String vendorData

} 的JSON格式字符串

- NAN类型示例 ``` 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(); } }); ``` - SoftAP类型示例 ``` 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(); } }); ``` ## 对指定设备进行配网

函数原型

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

说明

开始对指定的设备进行配网,发送SSID和密码,以及设备和账号信息。

接口类型

异步接口

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

deviceInfo

string

设备云为设备分配的信息,如deviceID等

accountInfo

string

账号信息

netConfigInfo

Object

netConfigInfo {

string ssid;

//必填,如果是手机已经配置过的ssid,可免输密码。

string ssidPassword;//非必填,不需要下发密码时,填写“”。

boolean isDefaultPassword; //必填。

int channel; //必填。

string sessionId; //必填,type为NAN时填实际值;type为softAp时填“”。

int type; //必填,0(NAN)/ 1(softAp)。

String wifiApId; //填写wifi列表返回信息中的对应字段。

String vendorData; //产品数据,当前为random Base64编码;

int timeout; // 配网超时时间,在NAN模式下设置,范围[1,90]s。

}

说明:如果用户输入密码,则isDefaultPassword=false, ssidPassword=“输入的密码”;否则按使用默认密码处理,isDefaultPassword=true, ssidPassword=””

callbackFunc

function

回调函数,NAN配网模式中,回调成功表示模组上报配网状态结果,其结果会转换成整型数值放到data.code,字符串类型会放到data.msg; SoftAp配网模式中,返回0/ -1表示配网成功/失败;

- NAN类型示例 ``` 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(); } }); ``` - SoftAP类型示例 ``` 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(); } }); ``` ## 释放配网通道

函数原型

NetConfig.disconnectDevice(commonInfo,callbackFunc)

说明

释放配网通道,在原子化服务不再使用NAN通道、原子化服务退出等情况需要原子化服务主动调用该接口。

接口类型

异步接口

输入参数

commonInfo

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

commonInfo

Object

commonInfo{

String sessionId; //若type为NAN,则设置为discoverDevByNAN 返回的sessionId;SoftAp设置为””。

}

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。

返回:0(成功)/ -1(失败)

- 示例: ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.disconnectDevice(commonInfo, (result) => { if (result.code == 0) { return; } }); ``` ## 秒控接口

函数原型

NetConfig.sendMessage(commonInfo,message,callbackFunc)

说明

秒控接口。

接口类型

异步接口

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

commonInfo

Object

commonInfo{

String sessionId; //若type为NAN,则设置为discoverDevByNAN 返回的sessionId;否则设置为””。

}

message

String

消息内容

callbackFunc

function

回调函数,返回命令发送结果。

返回:0(成功)/ -1(失败)

- 示例: ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.sendMessage(commonInfo, "111111", (result) => { // sendMessage 回调 }) ``` ## 注册消息回调

函数原型

NetConfig.registerMsgReceive(commonInfo, callbackFunc)

说明

获取设备侧消息,建议在NAN配网模式下连接设备接口前调用。

接口类型

异步接口

输入参数

详见下方参数表格

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

commonInfo

Object

commonInfo{

String sessionId; //若type为NAN,则设置为discoverDevByNAN 返回的sessionId;否则设置为””。

}

callbackFunc

function

回调函数,回调成功的情况下,data为返回数据结果,在该回调函数中解析使用数据结果。

返回:String message

- 示例: ``` let commonInfo = { sessionId: getApp(this).ConfigParams.deviceInfo.sessionId } getApp(this).NetConfig.registerMsgReceive(commonInfo, () => { // registerMsgReceive 回调 }); ``` ## 设备断链通知回调消息

函数原型

NetConfig.registerDisconnectCallback(commonInfo, callbackFunc)

说明

注册连接断开事件回调,建议在NAN配网模式下连接设备接口前调用。

当因某些原因造成设备主动与手机断开连接时,会通过该回调通知。

接口类型

异步接口

输入参数

详见下方参数表格

返回值

0(成功)/ -1(失败)

- 参数

参数名

类型

必填

说明

commonInfo

Object

commonInfo{

String sessionId; //若type为NAN,则设置为discoverDevByNAN 返回的sessionId ;否则设置为””。

}

callbackFunc

function

回调函数,当收到该回调时表示设备与手机连接断开。

返回状态码:0。

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