文档管理中心
API参考快应用API系统能力Wifi

Wifi

接口声明

在manifest.json文件的 features属性中增加如下配置。

收起
自动换行
深色代码主题
复制
  1. {"name": "system.wifi"}

导入模块

在调用接口页面的<script>部分增加如下配置。

收起
自动换行
深色代码主题
复制
  1. import wifi from '@system.wifi'

收起
自动换行
深色代码主题
复制
  1. var wifi = require("@system.wifi")

使用限制

展开

限制条件

说明

适用终端

手机、平板、智慧屏

适用区域

全球

接口定义

在快应用中支持搜索周边的Wi-Fi,同时可以针对指定Wi-Fi,传入密码发起连接。

展开

接口

描述

wifi.connect(OBJECT)

连接Wi-Fi。若已知Wi-Fi信息,可以直接调用此接口连接。

wifi.scan(OBJECT)

请求获取Wi-Fi列表,在onscanned注册的回调中返回wifiList数据。

wifi.onscanned = function(data)

监听获取Wi-Fi列表数据时的事件,在回调中将返回 wifiList。

wifi.onstatechanged = function(data)

监听连接断开Wi-Fi的事件。

wifi.getConnectedWifi(OBJECT)

获取已连接中的Wi-Fi信息。

wifi.connect(OBJECT)

描述

连接Wi-Fi。若已知Wi-Fi信息,可以直接调用此接口连接。

当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。

当系统版本为10.0或者以上版本时,调用此接口后,需要等待接到系统通知,并同意接入wifi后自动连接。

收到系统通知有如下限制:

  • 该网络不在禁用清单中(如果用户在设置wifi页面主动断开网络,则会进入禁用清单24小时)。
  • 用户填写的网络信息与首次扫描出的信息匹配成功。

如果未收到系统通知,需要用户进入系统设置手动链接Wi-Fi。

OBJECT参数

展开

参数

类型

是否必填

说明

SSID

string

Wi-Fi设备的ssid。

BSSID

string

Wi-Fi设备的bssid。

password

string

Wi-Fi设备的密码。

success

function

接口调用成功的回调函数。

fail

function

接口调用失败的回调函数。

complete

function

接口调用结束的回调函数(调用成功、失败都会执行)。

示例代码

收起
自动换行
深色代码主题
复制
  1. wifi.connect({
  2. SSID:'',
  3. BSSID:'',
  4. password:'',
  5. success: function() {
  6. console.log('connect wifi success');
  7. },
  8. fail: function(errmsg, errorCode) {
  9. console.log('connect failed ' + errmsg+', error = '+errorCode);
  10. }
  11. })

wifi.scan(OBJECT)

描述

请求获取Wi-Fi列表,在onscanned注册的回调中返回wifiList数据。

当系统版本为6.0或者以上版本时,如果没有打开定位开关,将导致设备无法正常获取周边的Wi-Fi信息。同时,需要请求获取位置信息的权限。

OBJECT参数

展开

参数

类型

是否必填

说明

success

function

接口调用成功的回调函数。

fail

function

接口调用失败的回调函数。

complete

function

接口调用结束的回调函数(调用成功、失败都会执行)。

示例代码

收起
自动换行
深色代码主题
复制
  1. wifi.scan({
  2. success:function() {
  3. console.log('scan wifi success');
  4. },
  5. fail: function(errmsg, errorCode) {
  6. console.log('scan failed ' + errmsg+', error = '+errorCode);
  7. }
  8. })

wifi.onscanned = function(data)

描述

监听获取Wi-Fi列表数据时的事件,在回调中将返回 wifiList。取消监听的方式:wifi.onscanned = null

当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。

OBJECT参数

展开

参数

类型

说明

wifiList

array

Wi-Fi列表数据。

Wi-FI列表项说明:

展开

参数

类型

说明

SSID

string

Wi-Fi的SSID。

BSSID

string

Wi-Fi的BSSID。

secure

boolean

Wi-Fi是否安全。

signalStrength

number

Wi-Fi信号强度。返回值为–100~0的整型数值:

  • 0到–49表示信号最好。
  • –50到–70表示信号偏差。
  • 小于-70表示信号最差,有可能连接不上或者掉线。

示例代码

收起
自动换行
深色代码主题
复制
  1. wifi.onscanned = function(data) {
  2. console.log('scanned result data = '+ JSON.stringify(data));
  3. }

wifi.onstatechanged = function(data)

描述

监听连接断开Wi-Fi的事件。取消监听的方式:wifi.onstatechanged = null

当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。

OBJECT参数

展开

参数

类型

说明

SSID

string

Wi-Fi的SSID。

BSSID

string

Wi-Fi的BSSID。

secure

boolean

Wi-Fi是否安全。

signalStrength

number

Wi-Fi信号强度。返回值为–100~0的整型数值:

  • 0到–49表示信号最好。
  • –50到–70表示信号偏差。
  • 小于-70表示信号最差,有可能连接不上或者掉线。

state

number

  • 0代表断开连接。
  • 1代表连接成功。

示例代码

收起
自动换行
深色代码主题
复制
  1. wifi.onstatechanged = function(data) {
  2. console.log(data.state+' ssid = '+data.SSID+', bssid = '+data.BSSID+', secure = '+data.secure+' signalStrength = '+data.signalStrength);
  3. }

wifi.getConnectedWifi(OBJECT)

描述

获取已连接中的Wi-Fi信息。

当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。

OBJECT参数

展开

参数

类型

是否必填

说明

success

function

接口调用成功的回调函数。

fail

function

接口调用失败的回调函数。

complete

function

接口调用结束的回调函数(调用成功、失败都会执行)。

success返回值:

展开

参数

类型

说明

SSID

string

Wi-Fi的SSID。

BSSID

string

Wi-Fi的BSSID。

secure

boolean

Wi-Fi是否安全。

signalStrength

number

Wi-Fi信号强度。返回值为–100~0的整型数值:

  • 0到–49表示信号最好。
  • –50到–70表示信号偏差。
  • 小于–70表示信号最差,有可能连接不上或者掉线。

示例代码

收起
自动换行
深色代码主题
复制
  1. wifi.getConnectedWifi({
  2. success:function(ret) {
  3. console.log('ssid = '+ret.SSID+', BSSID = '+ ret.BSSID+', secure = '+ret.secure+', signalStrength = '+ret.signalStrength);
  4. },
  5. fail: function(errmsg, errorCode) {
  6. console.log('getConnectedWifi failed ')
  7. }
  8. })

Demo

收起
自动换行
深色代码主题
复制
  1. <template>
  2. <div class="container">
  3. <div class="item-content">
  4. <text class="txt">connectWifi:{{isSuccess}}</text>
  5. </div>
  6. <input type="button" class="btn" onclick="connectWifi" value="Connect Wifi" />
  7. <div class="item-content">
  8. <text class="txt">wifiResult:{{wifistate}}</text>
  9. </div>
  10. <input type="button" class="btn" onclick="onconnected" value="Wifi onstatechanged" />
  11. <input type="button" class="btn" onclick="cancelConnect" value="wifi cancelConnectlistening" />
  12. <div class="item-content">
  13. <text class="txt">wifi.scan:{{scan}}</text>
  14. </div>
  15. <input type="button" class="btn" onclick="scanWifi" value="Scan Wifi" />
  16. <div class="item-content">
  17. <text class="txt">wifi.scanResult:{{scanedresult}}</text>
  18. </div>
  19. <input type="button" class="btn" onclick="onscanned" value="wifi onscanned" />
  20. <input type="button" class="btn" onclick="cancelonScanned" value="wifi cancelonScanned" />
  21. <div class="item-content">
  22. <text class="txt">wifi.wifiinfo:{{wifiinfo}}</text>
  23. </div>
  24. <input type="button" class="btn" onclick="getConnectedWifiInfo" value="Wifi getConnectedWifiInfo" />
  25. </div>
  26. </template>
  27. <style>
  28. .container{
  29. flex: 1;
  30. flex-direction: column;
  31. }
  32. .item-container {
  33. margin-bottom: 50px;
  34. margin-right: 60px;
  35. margin-left: 60px;
  36. flex-direction: column;
  37. }
  38. .item-content {
  39. flex-direction: column;
  40. background-color: #ffffff;
  41. padding-left: 30px;
  42. padding-right: 30px;
  43. padding-top: 20px;
  44. padding-bottom: 20px;
  45. margin-bottom: 30px;
  46. align-items: flex-start;
  47. }
  48. .txt {
  49. padding-top: 15px;
  50. padding-bottom: 15px;
  51. }
  52. .btn {
  53. height: 80px;
  54. text-align: center;
  55. border-radius: 5px;
  56. margin-right: 60px;
  57. margin-left: 60px;
  58. margin-bottom: 50px;
  59. color: #ffffff;
  60. font-size: 30px;
  61. background-color: #0faeff;
  62. }
  63. </style>
  64. <script>
  65. import wifi from '@system.wifi'
  66. export default {
  67. data: {
  68. componentName: 'Wifi',
  69. isSuccess: '',
  70. wifistate: '',
  71. scan: '',
  72. scanedresult: '',
  73. wifiinfo: ''
  74. },
  75. onInit: function () {
  76. this.$page.setTitleBar({ text: 'Wifi' })
  77. },
  78. connectWifi: function () {
  79. var that = this
  80. wifi.connect({
  81. SSID: 'OPPO Reno7 5G',
  82. BSSID: '0e:71:f6:f1:b9:7a',
  83. password: 'wlj123456',
  84. success: function () {
  85. console.log('connect wifi success')
  86. that.isSuccess = 'Success'
  87. },
  88. fail: function (errmsg, errorCode) {
  89. console.log('connect---------failed ' + errmsg + ', error = ' + errorCode)
  90. that.isSuccess = 'Fail'
  91. }
  92. })
  93. },
  94. onconnected: function () {
  95. var that = this
  96. wifi.onstatechanged = function (ret) {
  97. that.wifistate = ret.state + ' ssid = ' + ret.SSID + ', bssid = ' + ret.BSSID + ', secure = ' + ret.secure + ' signalStrength = ' + ret.signalStrength
  98. }
  99. },
  100. cancelConnect: function () {
  101. wifi.onstatechanged = null
  102. },
  103. scanWifi: function () {
  104. var that = this
  105. wifi.scan({
  106. success: function () {
  107. that.scan = 'success'
  108. console.log('scan wifi success')
  109. },
  110. fail: function (errmsg, errorCode) {
  111. console.log('scan failed ' + errmsg + ', error = ' + errorCode)
  112. that.scan = 'fail'
  113. }
  114. })
  115. },
  116. onscanned: function () {
  117. var that = this
  118. wifi.onscanned = function (ret) {
  119. that.scanedresult = JSON.stringify(ret)
  120. console.log('scaned result success ret = ' + JSON.stringify(ret))
  121. }
  122. },
  123. cancelonScanned: function () {
  124. wifi.onscanned = null
  125. },
  126. getConnectedWifiInfo: function () {
  127. var that = this
  128. wifi.getConnectedWifi({
  129. success: function (ret) {
  130. that.wifiinfo = ret.SSID + ': BSSID' + ret.BSSID + ', secure = ' + ret.secure + ', signalStrength = ' + ret.signalStrength
  131. },
  132. fail: function (data, code) {
  133. console.log('getConnectedWifi failed ' + data)
  134. that.wifiinfo = 'fail'
  135. }
  136. })
  137. }
  138. }
  139. </script>

效果图如下:

错误码说明

展开

错误码

描述

说明

1000

password error

Wi-Fi密码错误。

1001

connection timeout

连接超时。

1002

duplicate request

重复连接 Wi-Fi。

1003

wifi not turned on

未打开Wi-Fi开关。

1004

gps not turned on

未打开GPS定位开关。

1005

invalid SSID

无效SSID。

500

quickapp in background

应用在后台无法进行操作。

版本更新说明

展开

版本

发布日期

描述

1010

2018-04-20

第一次正式发布。

在 API参考 中进行搜索
请输入您想要搜索的关键词