uniapp直接毗连wifi(含有ios和安卓的注意事项)

打印 上一主题 下一主题

主题 1665|帖子 1665|积分 4995

媒介

   小程序中直接毗连wifi-----微信小程序
  代码

启动

  1.         //启动wifi
  2.                         startWifi() {
  3.                                 return new Promise((resolve, reject) => {
  4.                                         uni.startWifi({
  5.                                                 success: (res) => {
  6.                                                         console.log('启动wifi 成功', res)
  7.                                                         resolve(true)
  8.                                                 },
  9.                                                 fail: (err) => {
  10.                                                         console.error('启动wifi 失败', err)
  11.                                                         uni.showModal({
  12.                                                                 content: err.errMsg,
  13.                                                                 showCancel: false
  14.                                                         })
  15.                                                         reject(new Error(err))
  16.                                                 },
  17.                                         })
  18.                                 })
  19.                         },
复制代码
获取wifi列表

  1.     /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
  2.                         async getWifiList() {
  3.                                 const hasStart = await this.startWifi()
  4.                                 if (hasStart !== true) return
  5.                                 uni.getWifiList({
  6.                                         success: (res1) => {
  7.                                                 console.log('获取wifi列表命令发送 成功', res1)
  8.                                                 this.getLianjie() //调用方法,进行链接
  9.                                         },
  10.                                         fail: (err) => {
  11.                                                 console.error('获取wifi列表 失败', err)
  12.                                                 uni.showModal({
  13.                                                         content: err.errMsg,
  14.                                                         showCancel: false
  15.                                                 })
  16.                                         },
  17.                                 })
  18.                         },
复制代码
毗连wifi

  1.         getLianjie() {
  2.                                 uni.connectWifi({
  3.                                         SSID: this.wirelessName,//这个就是无线网的名称
  4.                                         password: this.wirelessPassword,//这个是无线网的密码
  5.                                         forceNewApi:true, //这个是安卓端,必须要添加的东西,否则会连接失败
  6.                                         success: (res) => {
  7.                                                 console.log('wifi 成功:', res)
  8.                                                 this.$uni.gettitle(res.errMsg)//封装的全局方法
  9.                                         },
  10.                                         fail: (err) => {
  11.                                                 console.error('wifi连接 失败:', err)
  12.                                                 uni.showModal({
  13.                                                         content: err.errMsg,
  14.                                                         showCancel: false
  15.                                                 })
  16.                                         },
  17.                                 })
  18.                         },
复制代码
完整代码

  1.         //启动wifi
  2.                         startWifi() {
  3.                                 return new Promise((resolve, reject) => {
  4.                                         uni.startWifi({
  5.                                                 success: (res) => {
  6.                                                         console.log('启动wifi 成功', res)
  7.                                                         resolve(true)
  8.                                                 },
  9.                                                 fail: (err) => {
  10.                                                         console.error('启动wifi 失败', err)
  11.                                                         uni.showModal({
  12.                                                                 content: err.errMsg,
  13.                                                                 showCancel: false
  14.                                                         })
  15.                                                         reject(new Error(err))
  16.                                                 },
  17.                                         })
  18.                                 })
  19.                         },
  20.                         /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
  21.                         async getWifiList() {
  22.                                 const hasStart = await this.startWifi()
  23.                                 if (hasStart !== true) return
  24.                                 uni.getWifiList({
  25.                                         success: (res1) => {
  26.                                                 console.log('获取wifi列表命令发送 成功', res1)
  27.                                                 this.getLianjie()
  28.                                         },
  29.                                         fail: (err) => {
  30.                                                 console.error('获取wifi列表 失败', err)
  31.                                                 uni.showModal({
  32.                                                         content: err.errMsg,
  33.                                                         showCancel: false
  34.                                                 })
  35.                                         },
  36.                                 })
  37.                         },
  38.                         //链接
  39.                         getLianjie() {
  40.                                 uni.connectWifi({
  41.                                         SSID: this.wirelessName,
  42.                                         password: this.wirelessPassword,
  43.                                         forceNewApi:true,
  44.                                         success: (res) => {
  45.                                                 console.log('wifi 成功:', res)
  46.                                                 this.$uni.gettitle(res.errMsg)
  47.                                         },
  48.                                         fail: (err) => {
  49.                                                 console.error('wifi连接 失败:', err)
  50.                                                 uni.showModal({
  51.                                                         content: err.errMsg,
  52.                                                         showCancel: false
  53.                                                 })
  54.                                         },
  55.                                 })
  56.                         },
复制代码
注意事项

   毗连wifi的时候:forceNewApi:true, //这个是安卓端,必须要添加的东西,否则会毗连失败,
  ios必要跳转到设置打开WiFi的页面,否则也会毗连失败
  在使用这个方法的时候,一定要打开手机的wifi,否则会失败
  安卓端,必要在manifest.json的源码视图中加上一句代码
  安卓端


否则似乎不能使用定位 功能!
竣事语

   欢迎指教,点个赞啦!
  


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表