加勒比路飞
加勒比路飞
  • 发布:2023-11-08 21:17
  • 更新:2023-11-09 10:43
  • 阅读:347

【报Bug】WIFI插件导致vivo手机闪退

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: 11

HBuilderX类型: 正式

HBuilderX版本号: 3.95

手机系统: Android

手机系统版本号: Android 8.0

手机厂商: vivo

手机机型: Y100

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

操作步骤:
<template> <view class="wifi tn-bg-white"> <d-navbar navbarType="0" title="选择WIFI"> </d-navbar> <view style="height: 30rpx;background: #F7F7F7;"></view> <view class="" style="width: 100%;padding: 30rpx;"> <view class="tn-padding-xs"> deviceId:{{obj.deviceId}} </view> <view class="tn-padding-xs"> uuid:{{obj.uuid}} </view> <view class="tn-padding-xs"> characteristicId:{{obj.characteristicId}} </view> <view class="tn-padding-xs"> <view class="tn-padding-xs"> SSID:{{wifiSSID}} </view> <view class="tn-padding-xs"> PSWD:{{wifiPassword}} </view> <view class="tn-padding-xs"> 给设备模块发送的SSID-PSWD {{wifiSSID+'|'+wifiPassword}} </view> <view class="tn-padding-xs"> APP给设备发送的信息:{{JSON.stringify(wifiObj)}} </view> <view class=""> 设备模块给APP接收的数据:{{resHex}} </view> <view class=""> 是否支持notify监听数据:{{JSON.stringify(notifyValueChange)}} </view> code:信息<br /> 10000: '未初始化蓝牙适配器',<br /> 10001: '当前蓝牙适配器不可用,请打开蓝牙后重试',<br /> 10002: '没有找到指定设备',<br /> 10003: '连接失败,请检查蓝牙设备是否打开',<br /> 10004: '没有找到指定服务',<br /> 10005: '没有找到指定特征值',<br /> 10006: '当前连接已断开',<br /> 10007: '当前特征值不支持此操作',<br /> 10008: '其余所有系统上报的异常',<br /> 10009: '系统特有,系统版本低于 4.3 不支持 BLE',<br /> 10010: '已连接',<br /> 10011: '配对设备需要配对码',<br /> 10012: '连接超时',<br /> 10013: '连接 deviceId 为空或者是格式不正确', </view> </view> <view class="tn-padding"> <view class="fzb_discover">请选择您的WIFI</view> <view class="" v-for="(item,index) in wifiList" :key="index"> <view class="tn-flex tn-flex-col-center tn-flex-row-between tn-padding-top tn-padding-bottom " style="height: 120rpx" :class="index!=wifiList.length-1 ?'solid-bottom':''" @click="linkedWifi(item,index)"> <view class="tn-flex tn-flex-col-center"> <view class="tn-text-md">{{item.SSID}}</view> </view> <view class="tn-flex tn-flex-col-center " v-if="item.SSID!=moduleSSID"> <view class="tn-margin-right tn-flex tn-flex-col-center"> <uv-icon name="lock-fill" size="40rpx" class=""></uv-icon> <view class="cuIcon-wifi tn-margin-left-xs" style="font-size: 40rpx;"> </view> </view> <view class="fzb_buetooth_right tn-round tn-flex tn-flex-col-center tn-flex-row-center"> <d-icon type="right" size="20" color="#979797"></d-icon> </view> </view> <view class="fzb_connected tn-flex tn-flex-col-center tn-flex-row-center" v-if="item.SSID==moduleSSID"> 已连接 </view> </view> </view> </view> <d-popup :show="wifiShow" width="590rpx" height="456rpx"> <view class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding-lg tn-flex tn-flex-direction-column tn-flex-row-around"> <view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl "> 输入WIFI密码 </view> <view class="fzb-wifi-input"> <input v-model="wifiSSID" :placeholder="请输入SSID" placeholder-class="placeholder" :adjust-position="false" style="height: 100%;" /> </view> <view class="fzb-wifi-input"> <input v-model="wifiPassword" :placeholder="请输入WIFI密码" placeholder-class="placeholder" adjust-position="false" style="height: 100%;" /> </view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="wifiCancel">取消</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" disabled="wifiPassword.length<6" @click="wifiConfirm">确定</uv-button> </view>
</view>
</d-popup>
<d-popup :show="Application" bgType="default" pos="bottom-center" width="590rpx" height="260rpx">
<view
class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding tn-flex tn-flex-direction-column tn-flex-row-around">
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl ">
提示
</view>
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-md tn-margin-top tn-margin-bottom">
未打开 Wi-Fi 开关
</view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="launchApplication()">设置</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" @click="initWifi()">重试</uv-button>
</view>
</view>
</d-popup>
<d-message ref="toast"></d-message>
</view>
</template>

<script>
import {
judgePermission
} from "@/utlis/permission.js"
import {
asciiToArrayBuffer,
ab2hex,
hexCharCodeToStr
} from "@/utlis/bluetooth.js"
import {
saveUserDeviceMode
} from "@/api/userDveice.js"
import {
mapState
} from "vuex"
export default {
data() {
return {
moduleSSID:"",
resHex: "",
wifiSSID: "",
Application: false,
wifiPassword: "",
wifiShow: false,
wifiIndex: 0,
wifiOptions: {

    },  
    wifiList: [],  
    connectWifi: "",  
    obj: {},  
    msg: {},  
    wifiLIST: {},  
    notifyValueChange: "",  
    wifiObj: {}  
  }  
},  
onLoad(e) {  
  const {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  } = JSON.parse(e.setInit)  
  console.log("setInit");  
  console.log(JSON.parse(e.setInit));  
  this.obj = {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  }  
  this.initWifi()  
},  
computed: {  
  ...mapState(['user'])  
},  
onBackPress(e) {  
  if (e.from == "backbutton") return true; //APP安卓物理返回键逻辑  
},  
watch:{  
  resHex(value){  
    if(value!=3||value!="3"){  
          saveUserDeviceMode({  
            userId: this.user.userId,  
            deviceCode: 255  
          }).then(res => {  
            const {  
              code  
            } = res  
            if (code == 1) {  
              this.$refs.toast.show({  
                model: 'success',  
                black: false,  
                mask: true,  
                label: 'WIFI-连接成功'  
              })  
            }  
            this.moduleSSID = this.wifiOptions.SSID  
          })  
    }else{  
      this.$refs.toast.show({  
        model: 'success',  
        black: false,  
        mask: true,  
        label: '连接失败,请重新连接!'  
      })  
    }  
  }  
},  
methods: {  
  initWifi() {  
    uni.startWifi({  
      success: res => {  
        // console.log(res,'res');  
        this.Application = false  
        uni.getWifiList({  
          success: (res) => {  
            console.log(res, "res");  
          },  
          fail: (err) => {  

          },  
          complete: (err) => {  
            uni.onGetWifiList(this.onGetWifiListFun)  
            this.getWifiListFun()  
          }  
        })  
      },  
      fail: (err) => {  
        if (err.errCode == 12005) {  
          this.Application = true  
        }  
        console.log(err);  
      }  
    })  
  },  
  onGetWifiListFun(info) {  
    this.wifiList = []  
    let wifiList = []  
    const unique = (arr) => {  
      const res = new Map(); //定义常量 res,值为一个Map对象实例  
      return arr.filter((arr) => !res.has(arr.SSID) && res.set(arr.SSID, 1))  
    }  
    wifiList = unique(info.wifiList).filter(item => item.SSID != '')  
    // 按信号强度排序  
    wifiList = wifiList.sort((a, b) => {  
      return b.signalStrength - a.signalStrength  
    })  

    this.wifiList = wifiList  
    // 获取成功,关闭loading  
    // console.log('wifiList', wifiList)  
    console.log('wifiList', this.wifiList)  
    // console.log(info)  
  },  
  getWifiListFun() {  
    // #ifdef APP-PLUS  
    uni.getWifiList({  
      complete: () => {  
        // 移除监听函数  
        uni.offGetWifiList(this.onGetWifiListFun)  
        // 获取当前连接的WiFi信息  
      }  
    })  
    // #endif  
  },  

  wifiConfirm() {  
    // let msg = `${this.wifiOptions.SSID+'|'+this.wifiPassword}`  
    // let msg = `10001`  
    let msg = `${this.wifiSSID+'|'+this.wifiPassword}`  
      const buffer = asciiToArrayBuffer(msg);  
      uni.writeBLECharacteristicValue({  
        // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
        deviceId: this.obj.deviceId,  
        // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取  
        serviceId: this.obj.uuid,  
        // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取  
        characteristicId: this.obj.characteristicId,  
        // 这里的value是ArrayBuffer类型  
        // value: buffer,  
        value: buffer,  
        writeType: 'writeNoResponse',  
        success: (res) => {  
          console.log("发送成功-----", res);  
        },  
        fail: (res) => {},  
        complete: (res) => {  
          this.wifiObj = res  
          this.updateNotify(this.obj.deviceId, this.obj.uuid, this.obj.characteristicId0)  
          this.wifiCancel()  
        }  
      });  
      // let newList = this.wifiList  
      // newList.forEach((item, itemIndex) => {  
      //   if (this.wifiIndex == itemIndex) {  
      //     newList[itemIndex].is = !newList[itemIndex].is;  
      //     return;  
      //   }  
      //   newList[itemIndex].is = false;  
      // });  
  },  
  wifiCancel() {  
    this.wifiShow = false  
  },  

  /**  
   * @tips:启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用   
   */  
  async updateNotify(deviceId, serviceId, characteristicId) {  
      uni.notifyBLECharacteristicValueChange({  
        deviceId,  
        serviceId,  
        characteristicId,  
        state: true,  
        success: (res) => {  
          console.log("notify功能,订阅特征值");  
          console.log(res);  
          this.message()  
        },  
        fail: (err) => {  

        },  
        complete: (res) => {  
          this.notifyValueChange = res  
        }  
      })  

  },  
  //监听发送过来的消息  
  async message() {  
    // setTimeout(() => {  
    let show = true  
    uni.onBLECharacteristicValueChange((res) => {  
      // console.log("蓝牙返回数据为:"+receiveValue)  
      let resHex = ab2hex(res.value) // ArrayBuffer转16进制字符串示例  
      // this.resHex = resHex  
      // let result = hexCharCodeToStr(resHex) // 将16进制的内容转ascii码成我们看得懂的字符串内容  
      this.resHex = resHex  

    })  

    this.$refs.toast.hide()  

    // }, 2000)  
  },  
  // 跳转到手机系统设置页面  
  launchApplication() {  
    if (plus.os.name == "Android") {  
      plus.runtime.launchApplication({  
        pname: "com.android.settings",  
        extra: {  
          action: "android.settings.SETTINGS"  
        }  
      }, function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    } else {  
      plus.runtime.openURL("app-settings:", function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    }  
  },  
  // 链接wifi  
  linkedWifi(item, index) {  
    this.wifiOptions = item  
    console.log(item,"item");  
    this.wifiIndex = index  
    // if (this.wifiList[index].is) return  
    this.wifiShow = true  
    this.wifiPassword = ''  
  }  
}  

}
</script>

<style lang="scss">
page {
background: #FFF;
}

.fzb_discover {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #B5B5B5;
}

.fzb_buetooth_right {
width: 40rpx;
height: 40rpx;
background: #E3E3E3;
}

.fzb_connected {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #353648;
background: #EEEEEE;
border-radius: 30rpx;
width: 130rpx;
height: 60rpx;

}

.fzb-wifi-input {
height: 96rpx;
background: #F7F7F7;
border-radius: 8rpx;
padding: 30rpx;
}

.placeholder {
font-size: 24rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #BDBDBD;
}
</style>

预期结果:
<template> <view class="wifi tn-bg-white"> <d-navbar navbarType="0" title="选择WIFI"> </d-navbar> <view style="height: 30rpx;background: #F7F7F7;"></view> <view class="" style="width: 100%;padding: 30rpx;"> <view class="tn-padding-xs"> deviceId:{{obj.deviceId}} </view> <view class="tn-padding-xs"> uuid:{{obj.uuid}} </view> <view class="tn-padding-xs"> characteristicId:{{obj.characteristicId}} </view> <view class="tn-padding-xs"> <view class="tn-padding-xs"> SSID:{{wifiSSID}} </view> <view class="tn-padding-xs"> PSWD:{{wifiPassword}} </view> <view class="tn-padding-xs"> 给设备模块发送的SSID-PSWD {{wifiSSID+'|'+wifiPassword}} </view> <view class="tn-padding-xs"> APP给设备发送的信息:{{JSON.stringify(wifiObj)}} </view> <view class=""> 设备模块给APP接收的数据:{{resHex}} </view> <view class=""> 是否支持notify监听数据:{{JSON.stringify(notifyValueChange)}} </view> code:信息<br /> 10000: '未初始化蓝牙适配器',<br /> 10001: '当前蓝牙适配器不可用,请打开蓝牙后重试',<br /> 10002: '没有找到指定设备',<br /> 10003: '连接失败,请检查蓝牙设备是否打开',<br /> 10004: '没有找到指定服务',<br /> 10005: '没有找到指定特征值',<br /> 10006: '当前连接已断开',<br /> 10007: '当前特征值不支持此操作',<br /> 10008: '其余所有系统上报的异常',<br /> 10009: '系统特有,系统版本低于 4.3 不支持 BLE',<br /> 10010: '已连接',<br /> 10011: '配对设备需要配对码',<br /> 10012: '连接超时',<br /> 10013: '连接 deviceId 为空或者是格式不正确', </view> </view> <view class="tn-padding"> <view class="fzb_discover">请选择您的WIFI</view> <view class="" v-for="(item,index) in wifiList" :key="index"> <view class="tn-flex tn-flex-col-center tn-flex-row-between tn-padding-top tn-padding-bottom " style="height: 120rpx" :class="index!=wifiList.length-1 ?'solid-bottom':''" @click="linkedWifi(item,index)"> <view class="tn-flex tn-flex-col-center"> <view class="tn-text-md">{{item.SSID}}</view> </view> <view class="tn-flex tn-flex-col-center " v-if="item.SSID!=moduleSSID"> <view class="tn-margin-right tn-flex tn-flex-col-center"> <uv-icon name="lock-fill" size="40rpx" class=""></uv-icon> <view class="cuIcon-wifi tn-margin-left-xs" style="font-size: 40rpx;"> </view> </view> <view class="fzb_buetooth_right tn-round tn-flex tn-flex-col-center tn-flex-row-center"> <d-icon type="right" size="20" color="#979797"></d-icon> </view> </view> <view class="fzb_connected tn-flex tn-flex-col-center tn-flex-row-center" v-if="item.SSID==moduleSSID"> 已连接 </view> </view> </view> </view> <d-popup :show="wifiShow" width="590rpx" height="456rpx"> <view class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding-lg tn-flex tn-flex-direction-column tn-flex-row-around"> <view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl "> 输入WIFI密码 </view> <view class="fzb-wifi-input"> <input v-model="wifiSSID" :placeholder="请输入SSID" placeholder-class="placeholder" :adjust-position="false" style="height: 100%;" /> </view> <view class="fzb-wifi-input"> <input v-model="wifiPassword" :placeholder="请输入WIFI密码" placeholder-class="placeholder" adjust-position="false" style="height: 100%;" /> </view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="wifiCancel">取消</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" disabled="wifiPassword.length<6" @click="wifiConfirm">确定</uv-button> </view>
</view>
</d-popup>
<d-popup :show="Application" bgType="default" pos="bottom-center" width="590rpx" height="260rpx">
<view
class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding tn-flex tn-flex-direction-column tn-flex-row-around">
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl ">
提示
</view>
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-md tn-margin-top tn-margin-bottom">
未打开 Wi-Fi 开关
</view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="launchApplication()">设置</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" @click="initWifi()">重试</uv-button>
</view>
</view>
</d-popup>
<d-message ref="toast"></d-message>
</view>
</template>

<script>
import {
judgePermission
} from "@/utlis/permission.js"
import {
asciiToArrayBuffer,
ab2hex,
hexCharCodeToStr
} from "@/utlis/bluetooth.js"
import {
saveUserDeviceMode
} from "@/api/userDveice.js"
import {
mapState
} from "vuex"
export default {
data() {
return {
moduleSSID:"",
resHex: "",
wifiSSID: "",
Application: false,
wifiPassword: "",
wifiShow: false,
wifiIndex: 0,
wifiOptions: {

    },  
    wifiList: [],  
    connectWifi: "",  
    obj: {},  
    msg: {},  
    wifiLIST: {},  
    notifyValueChange: "",  
    wifiObj: {}  
  }  
},  
onLoad(e) {  
  const {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  } = JSON.parse(e.setInit)  
  console.log("setInit");  
  console.log(JSON.parse(e.setInit));  
  this.obj = {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  }  
  this.initWifi()  
},  
computed: {  
  ...mapState(['user'])  
},  
onBackPress(e) {  
  if (e.from == "backbutton") return true; //APP安卓物理返回键逻辑  
},  
watch:{  
  resHex(value){  
    if(value!=3||value!="3"){  
          saveUserDeviceMode({  
            userId: this.user.userId,  
            deviceCode: 255  
          }).then(res => {  
            const {  
              code  
            } = res  
            if (code == 1) {  
              this.$refs.toast.show({  
                model: 'success',  
                black: false,  
                mask: true,  
                label: 'WIFI-连接成功'  
              })  
            }  
            this.moduleSSID = this.wifiOptions.SSID  
          })  
    }else{  
      this.$refs.toast.show({  
        model: 'success',  
        black: false,  
        mask: true,  
        label: '连接失败,请重新连接!'  
      })  
    }  
  }  
},  
methods: {  
  initWifi() {  
    uni.startWifi({  
      success: res => {  
        // console.log(res,'res');  
        this.Application = false  
        uni.getWifiList({  
          success: (res) => {  
            console.log(res, "res");  
          },  
          fail: (err) => {  

          },  
          complete: (err) => {  
            uni.onGetWifiList(this.onGetWifiListFun)  
            this.getWifiListFun()  
          }  
        })  
      },  
      fail: (err) => {  
        if (err.errCode == 12005) {  
          this.Application = true  
        }  
        console.log(err);  
      }  
    })  
  },  
  onGetWifiListFun(info) {  
    this.wifiList = []  
    let wifiList = []  
    const unique = (arr) => {  
      const res = new Map(); //定义常量 res,值为一个Map对象实例  
      return arr.filter((arr) => !res.has(arr.SSID) && res.set(arr.SSID, 1))  
    }  
    wifiList = unique(info.wifiList).filter(item => item.SSID != '')  
    // 按信号强度排序  
    wifiList = wifiList.sort((a, b) => {  
      return b.signalStrength - a.signalStrength  
    })  

    this.wifiList = wifiList  
    // 获取成功,关闭loading  
    // console.log('wifiList', wifiList)  
    console.log('wifiList', this.wifiList)  
    // console.log(info)  
  },  
  getWifiListFun() {  
    // #ifdef APP-PLUS  
    uni.getWifiList({  
      complete: () => {  
        // 移除监听函数  
        uni.offGetWifiList(this.onGetWifiListFun)  
        // 获取当前连接的WiFi信息  
      }  
    })  
    // #endif  
  },  

  wifiConfirm() {  
    // let msg = `${this.wifiOptions.SSID+'|'+this.wifiPassword}`  
    // let msg = `10001`  
    let msg = `${this.wifiSSID+'|'+this.wifiPassword}`  
      const buffer = asciiToArrayBuffer(msg);  
      uni.writeBLECharacteristicValue({  
        // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
        deviceId: this.obj.deviceId,  
        // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取  
        serviceId: this.obj.uuid,  
        // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取  
        characteristicId: this.obj.characteristicId,  
        // 这里的value是ArrayBuffer类型  
        // value: buffer,  
        value: buffer,  
        writeType: 'writeNoResponse',  
        success: (res) => {  
          console.log("发送成功-----", res);  
        },  
        fail: (res) => {},  
        complete: (res) => {  
          this.wifiObj = res  
          this.updateNotify(this.obj.deviceId, this.obj.uuid, this.obj.characteristicId0)  
          this.wifiCancel()  
        }  
      });  
      // let newList = this.wifiList  
      // newList.forEach((item, itemIndex) => {  
      //   if (this.wifiIndex == itemIndex) {  
      //     newList[itemIndex].is = !newList[itemIndex].is;  
      //     return;  
      //   }  
      //   newList[itemIndex].is = false;  
      // });  
  },  
  wifiCancel() {  
    this.wifiShow = false  
  },  

  /**  
   * @tips:启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用   
   */  
  async updateNotify(deviceId, serviceId, characteristicId) {  
      uni.notifyBLECharacteristicValueChange({  
        deviceId,  
        serviceId,  
        characteristicId,  
        state: true,  
        success: (res) => {  
          console.log("notify功能,订阅特征值");  
          console.log(res);  
          this.message()  
        },  
        fail: (err) => {  

        },  
        complete: (res) => {  
          this.notifyValueChange = res  
        }  
      })  

  },  
  //监听发送过来的消息  
  async message() {  
    // setTimeout(() => {  
    let show = true  
    uni.onBLECharacteristicValueChange((res) => {  
      // console.log("蓝牙返回数据为:"+receiveValue)  
      let resHex = ab2hex(res.value) // ArrayBuffer转16进制字符串示例  
      // this.resHex = resHex  
      // let result = hexCharCodeToStr(resHex) // 将16进制的内容转ascii码成我们看得懂的字符串内容  
      this.resHex = resHex  

    })  

    this.$refs.toast.hide()  

    // }, 2000)  
  },  
  // 跳转到手机系统设置页面  
  launchApplication() {  
    if (plus.os.name == "Android") {  
      plus.runtime.launchApplication({  
        pname: "com.android.settings",  
        extra: {  
          action: "android.settings.SETTINGS"  
        }  
      }, function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    } else {  
      plus.runtime.openURL("app-settings:", function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    }  
  },  
  // 链接wifi  
  linkedWifi(item, index) {  
    this.wifiOptions = item  
    console.log(item,"item");  
    this.wifiIndex = index  
    // if (this.wifiList[index].is) return  
    this.wifiShow = true  
    this.wifiPassword = ''  
  }  
}  

}
</script>

<style lang="scss">
page {
background: #FFF;
}

.fzb_discover {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #B5B5B5;
}

.fzb_buetooth_right {
width: 40rpx;
height: 40rpx;
background: #E3E3E3;
}

.fzb_connected {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #353648;
background: #EEEEEE;
border-radius: 30rpx;
width: 130rpx;
height: 60rpx;

}

.fzb-wifi-input {
height: 96rpx;
background: #F7F7F7;
border-radius: 8rpx;
padding: 30rpx;
}

.placeholder {
font-size: 24rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #BDBDBD;
}
</style>

实际结果:
<template> <view class="wifi tn-bg-white"> <d-navbar navbarType="0" title="选择WIFI"> </d-navbar> <view style="height: 30rpx;background: #F7F7F7;"></view> <view class="" style="width: 100%;padding: 30rpx;"> <view class="tn-padding-xs"> deviceId:{{obj.deviceId}} </view> <view class="tn-padding-xs"> uuid:{{obj.uuid}} </view> <view class="tn-padding-xs"> characteristicId:{{obj.characteristicId}} </view> <view class="tn-padding-xs"> <view class="tn-padding-xs"> SSID:{{wifiSSID}} </view> <view class="tn-padding-xs"> PSWD:{{wifiPassword}} </view> <view class="tn-padding-xs"> 给设备模块发送的SSID-PSWD {{wifiSSID+'|'+wifiPassword}} </view> <view class="tn-padding-xs"> APP给设备发送的信息:{{JSON.stringify(wifiObj)}} </view> <view class=""> 设备模块给APP接收的数据:{{resHex}} </view> <view class=""> 是否支持notify监听数据:{{JSON.stringify(notifyValueChange)}} </view> code:信息<br /> 10000: '未初始化蓝牙适配器',<br /> 10001: '当前蓝牙适配器不可用,请打开蓝牙后重试',<br /> 10002: '没有找到指定设备',<br /> 10003: '连接失败,请检查蓝牙设备是否打开',<br /> 10004: '没有找到指定服务',<br /> 10005: '没有找到指定特征值',<br /> 10006: '当前连接已断开',<br /> 10007: '当前特征值不支持此操作',<br /> 10008: '其余所有系统上报的异常',<br /> 10009: '系统特有,系统版本低于 4.3 不支持 BLE',<br /> 10010: '已连接',<br /> 10011: '配对设备需要配对码',<br /> 10012: '连接超时',<br /> 10013: '连接 deviceId 为空或者是格式不正确', </view> </view> <view class="tn-padding"> <view class="fzb_discover">请选择您的WIFI</view> <view class="" v-for="(item,index) in wifiList" :key="index"> <view class="tn-flex tn-flex-col-center tn-flex-row-between tn-padding-top tn-padding-bottom " style="height: 120rpx" :class="index!=wifiList.length-1 ?'solid-bottom':''" @click="linkedWifi(item,index)"> <view class="tn-flex tn-flex-col-center"> <view class="tn-text-md">{{item.SSID}}</view> </view> <view class="tn-flex tn-flex-col-center " v-if="item.SSID!=moduleSSID"> <view class="tn-margin-right tn-flex tn-flex-col-center"> <uv-icon name="lock-fill" size="40rpx" class=""></uv-icon> <view class="cuIcon-wifi tn-margin-left-xs" style="font-size: 40rpx;"> </view> </view> <view class="fzb_buetooth_right tn-round tn-flex tn-flex-col-center tn-flex-row-center"> <d-icon type="right" size="20" color="#979797"></d-icon> </view> </view> <view class="fzb_connected tn-flex tn-flex-col-center tn-flex-row-center" v-if="item.SSID==moduleSSID"> 已连接 </view> </view> </view> </view> <d-popup :show="wifiShow" width="590rpx" height="456rpx"> <view class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding-lg tn-flex tn-flex-direction-column tn-flex-row-around"> <view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl "> 输入WIFI密码 </view> <view class="fzb-wifi-input"> <input v-model="wifiSSID" :placeholder="请输入SSID" placeholder-class="placeholder" :adjust-position="false" style="height: 100%;" /> </view> <view class="fzb-wifi-input"> <input v-model="wifiPassword" :placeholder="请输入WIFI密码" placeholder-class="placeholder" adjust-position="false" style="height: 100%;" /> </view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="wifiCancel">取消</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" disabled="wifiPassword.length<6" @click="wifiConfirm">确定</uv-button> </view>
</view>
</d-popup>
<d-popup :show="Application" bgType="default" pos="bottom-center" width="590rpx" height="260rpx">
<view
class="tn-bg-white tn-width-full tn-height-full tn-radius tn-padding tn-flex tn-flex-direction-column tn-flex-row-around">
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-xl ">
提示
</view>
<view class="fzb-wifi-header tn-text-center text-hei text-bold5 tn-text-md tn-margin-top tn-margin-bottom">
未打开 Wi-Fi 开关
</view>
<view class="tn-flex tn-flex-col-center tn-flex-row-between">
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#fff',border: '1rpx solid #353648',borderRadius: '8rpx'}" @click="launchApplication()">设置</uv-button>
<uv-button customStyle="{width: '230rpx',height:'80rpx',background:'#FDB531',borderRadius: '8rpx',color:'#fff'}" @click="initWifi()">重试</uv-button>
</view>
</view>
</d-popup>
<d-message ref="toast"></d-message>
</view>
</template>

<script>
import {
judgePermission
} from "@/utlis/permission.js"
import {
asciiToArrayBuffer,
ab2hex,
hexCharCodeToStr
} from "@/utlis/bluetooth.js"
import {
saveUserDeviceMode
} from "@/api/userDveice.js"
import {
mapState
} from "vuex"
export default {
data() {
return {
moduleSSID:"",
resHex: "",
wifiSSID: "",
Application: false,
wifiPassword: "",
wifiShow: false,
wifiIndex: 0,
wifiOptions: {

    },  
    wifiList: [],  
    connectWifi: "",  
    obj: {},  
    msg: {},  
    wifiLIST: {},  
    notifyValueChange: "",  
    wifiObj: {}  
  }  
},  
onLoad(e) {  
  const {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  } = JSON.parse(e.setInit)  
  console.log("setInit");  
  console.log(JSON.parse(e.setInit));  
  this.obj = {  
    deviceId,  
    uuid,  
    characteristicId,  
    characteristicId0  
  }  
  this.initWifi()  
},  
computed: {  
  ...mapState(['user'])  
},  
onBackPress(e) {  
  if (e.from == "backbutton") return true; //APP安卓物理返回键逻辑  
},  
watch:{  
  resHex(value){  
    if(value!=3||value!="3"){  
          saveUserDeviceMode({  
            userId: this.user.userId,  
            deviceCode: 255  
          }).then(res => {  
            const {  
              code  
            } = res  
            if (code == 1) {  
              this.$refs.toast.show({  
                model: 'success',  
                black: false,  
                mask: true,  
                label: 'WIFI-连接成功'  
              })  
            }  
            this.moduleSSID = this.wifiOptions.SSID  
          })  
    }else{  
      this.$refs.toast.show({  
        model: 'success',  
        black: false,  
        mask: true,  
        label: '连接失败,请重新连接!'  
      })  
    }  
  }  
},  
methods: {  
  initWifi() {  
    uni.startWifi({  
      success: res => {  
        // console.log(res,'res');  
        this.Application = false  
        uni.getWifiList({  
          success: (res) => {  
            console.log(res, "res");  
          },  
          fail: (err) => {  

          },  
          complete: (err) => {  
            uni.onGetWifiList(this.onGetWifiListFun)  
            this.getWifiListFun()  
          }  
        })  
      },  
      fail: (err) => {  
        if (err.errCode == 12005) {  
          this.Application = true  
        }  
        console.log(err);  
      }  
    })  
  },  
  onGetWifiListFun(info) {  
    this.wifiList = []  
    let wifiList = []  
    const unique = (arr) => {  
      const res = new Map(); //定义常量 res,值为一个Map对象实例  
      return arr.filter((arr) => !res.has(arr.SSID) && res.set(arr.SSID, 1))  
    }  
    wifiList = unique(info.wifiList).filter(item => item.SSID != '')  
    // 按信号强度排序  
    wifiList = wifiList.sort((a, b) => {  
      return b.signalStrength - a.signalStrength  
    })  

    this.wifiList = wifiList  
    // 获取成功,关闭loading  
    // console.log('wifiList', wifiList)  
    console.log('wifiList', this.wifiList)  
    // console.log(info)  
  },  
  getWifiListFun() {  
    // #ifdef APP-PLUS  
    uni.getWifiList({  
      complete: () => {  
        // 移除监听函数  
        uni.offGetWifiList(this.onGetWifiListFun)  
        // 获取当前连接的WiFi信息  
      }  
    })  
    // #endif  
  },  

  wifiConfirm() {  
    // let msg = `${this.wifiOptions.SSID+'|'+this.wifiPassword}`  
    // let msg = `10001`  
    let msg = `${this.wifiSSID+'|'+this.wifiPassword}`  
      const buffer = asciiToArrayBuffer(msg);  
      uni.writeBLECharacteristicValue({  
        // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取  
        deviceId: this.obj.deviceId,  
        // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取  
        serviceId: this.obj.uuid,  
        // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取  
        characteristicId: this.obj.characteristicId,  
        // 这里的value是ArrayBuffer类型  
        // value: buffer,  
        value: buffer,  
        writeType: 'writeNoResponse',  
        success: (res) => {  
          console.log("发送成功-----", res);  
        },  
        fail: (res) => {},  
        complete: (res) => {  
          this.wifiObj = res  
          this.updateNotify(this.obj.deviceId, this.obj.uuid, this.obj.characteristicId0)  
          this.wifiCancel()  
        }  
      });  
      // let newList = this.wifiList  
      // newList.forEach((item, itemIndex) => {  
      //   if (this.wifiIndex == itemIndex) {  
      //     newList[itemIndex].is = !newList[itemIndex].is;  
      //     return;  
      //   }  
      //   newList[itemIndex].is = false;  
      // });  
  },  
  wifiCancel() {  
    this.wifiShow = false  
  },  

  /**  
   * @tips:启用低功耗蓝牙设备特征值变化时的notify功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用   
   */  
  async updateNotify(deviceId, serviceId, characteristicId) {  
      uni.notifyBLECharacteristicValueChange({  
        deviceId,  
        serviceId,  
        characteristicId,  
        state: true,  
        success: (res) => {  
          console.log("notify功能,订阅特征值");  
          console.log(res);  
          this.message()  
        },  
        fail: (err) => {  

        },  
        complete: (res) => {  
          this.notifyValueChange = res  
        }  
      })  

  },  
  //监听发送过来的消息  
  async message() {  
    // setTimeout(() => {  
    let show = true  
    uni.onBLECharacteristicValueChange((res) => {  
      // console.log("蓝牙返回数据为:"+receiveValue)  
      let resHex = ab2hex(res.value) // ArrayBuffer转16进制字符串示例  
      // this.resHex = resHex  
      // let result = hexCharCodeToStr(resHex) // 将16进制的内容转ascii码成我们看得懂的字符串内容  
      this.resHex = resHex  

    })  

    this.$refs.toast.hide()  

    // }, 2000)  
  },  
  // 跳转到手机系统设置页面  
  launchApplication() {  
    if (plus.os.name == "Android") {  
      plus.runtime.launchApplication({  
        pname: "com.android.settings",  
        extra: {  
          action: "android.settings.SETTINGS"  
        }  
      }, function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    } else {  
      plus.runtime.openURL("app-settings:", function(e) {  
        console.log("打开系统设置成功");  
      }, function(e) {  
        console.log("打开系统设置失败:", e);  
      });  
    }  
  },  
  // 链接wifi  
  linkedWifi(item, index) {  
    this.wifiOptions = item  
    console.log(item,"item");  
    this.wifiIndex = index  
    // if (this.wifiList[index].is) return  
    this.wifiShow = true  
    this.wifiPassword = ''  
  }  
}  

}
</script>

<style lang="scss">
page {
background: #FFF;
}

.fzb_discover {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #B5B5B5;
}

.fzb_buetooth_right {
width: 40rpx;
height: 40rpx;
background: #E3E3E3;
}

.fzb_connected {
font-size: 30rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #353648;
background: #EEEEEE;
border-radius: 30rpx;
width: 130rpx;
height: 60rpx;

}

.fzb-wifi-input {
height: 96rpx;
background: #F7F7F7;
border-radius: 8rpx;
padding: 30rpx;
}

.placeholder {
font-size: 24rpx;
font-family: PingFangSC-Regular, PingFang SC;
font-weight: 400;
color: #BDBDBD;
}
</style>

bug描述:

使用vivo手机,搜索wifi列表会闪退

2023-11-08 21:17 负责人:无 分享
已邀请:
DCloud_Android_DQQ

DCloud_Android_DQQ

提供一下报错日志

要回复问题请先登录注册