媒介
小程序中直接毗连wifi-----微信小程序
代码
启动
- //启动wifi
- startWifi() {
- return new Promise((resolve, reject) => {
- uni.startWifi({
- success: (res) => {
- console.log('启动wifi 成功', res)
- resolve(true)
- },
- fail: (err) => {
- console.error('启动wifi 失败', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- reject(new Error(err))
- },
- })
- })
- },
复制代码 获取wifi列表
- /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
- async getWifiList() {
- const hasStart = await this.startWifi()
- if (hasStart !== true) return
- uni.getWifiList({
- success: (res1) => {
- console.log('获取wifi列表命令发送 成功', res1)
- this.getLianjie() //调用方法,进行链接
- },
- fail: (err) => {
- console.error('获取wifi列表 失败', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- },
- })
- },
复制代码 毗连wifi
- getLianjie() {
- uni.connectWifi({
- SSID: this.wirelessName,//这个就是无线网的名称
- password: this.wirelessPassword,//这个是无线网的密码
- forceNewApi:true, //这个是安卓端,必须要添加的东西,否则会连接失败
- success: (res) => {
- console.log('wifi 成功:', res)
- this.$uni.gettitle(res.errMsg)//封装的全局方法
- },
- fail: (err) => {
- console.error('wifi连接 失败:', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- },
- })
- },
复制代码 完整代码
- //启动wifi
- startWifi() {
- return new Promise((resolve, reject) => {
- uni.startWifi({
- success: (res) => {
- console.log('启动wifi 成功', res)
- resolve(true)
- },
- fail: (err) => {
- console.error('启动wifi 失败', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- reject(new Error(err))
- },
- })
- })
- },
- /** 获取wifi列表, ios和android 各不相同,具体看顶部资料 */
- async getWifiList() {
- const hasStart = await this.startWifi()
- if (hasStart !== true) return
- uni.getWifiList({
- success: (res1) => {
- console.log('获取wifi列表命令发送 成功', res1)
- this.getLianjie()
- },
- fail: (err) => {
- console.error('获取wifi列表 失败', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- },
- })
- },
- //链接
- getLianjie() {
- uni.connectWifi({
- SSID: this.wirelessName,
- password: this.wirelessPassword,
- forceNewApi:true,
- success: (res) => {
- console.log('wifi 成功:', res)
- this.$uni.gettitle(res.errMsg)
- },
- fail: (err) => {
- console.error('wifi连接 失败:', err)
- uni.showModal({
- content: err.errMsg,
- showCancel: false
- })
- },
- })
- },
复制代码 注意事项
毗连wifi的时候:forceNewApi:true, //这个是安卓端,必须要添加的东西,否则会毗连失败,
ios必要跳转到设置打开WiFi的页面,否则也会毗连失败
在使用这个方法的时候,一定要打开手机的wifi,否则会失败
安卓端,必要在manifest.json的源码视图中加上一句代码
安卓端
否则似乎不能使用定位 功能!
竣事语
欢迎指教,点个赞啦!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |