const app = getApp(); let xBlufi = require("../../../utils/blufi/xBlufi.js"); import strings from '../../../utils/strings.js'; import lexin_devices from '../../../utils/lexin/devices'; import junzhengUtil from '../../../utils/junzheng/util'; import routeUtil from '../../../utils/routeUtil.js'; import routePath from '../../../utils/routePath.js'; Page({ data: { nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '连接配网', //导航栏 中间的标题 }, devicesList: [], searching: false, version: '2.0', name: '', connectedDeviceId: '', connected: true, deviceInfo: null, isInitOK: false, customData: '', isOne: true, isSubmit: false, islanya: false, isOnBluetooth: false, imageUrl: "./../../../img/f.png", connectDevice: {}, }, onLoad(options) { const that = this; var param = options.param; if (!strings.isEmpty(param)) { param = JSON.parse(options.param); that.data.connectDevice = param; if (!strings.isEmpty(param.typeList)) { var typeList = param.typeList; typeList = typeList.filter((v) => v.connectType == 3); if (!strings.isEmpty(typeList)) { that.setData({ imageUrl: typeList[0].deviceLinkResp.icon2, }); } } } const isOne = setTimeout(() => { clearTimeout(isOne); if (that.data.isOne) { that.setData({ isOne: false, }); } }, 2000); if (app.globalData.oneInitBluetooth) { app.globalData.oneInitBluetooth = false; xBlufi.initXBlufi(1); }; xBlufi.listenDeviceMsgEvent(true, that.funListenDeviceMsgEvent); that.Search(); }, onShow() { var that = this; if (!that.data.isOne) { that.Search(); } }, ///开始或结束,也可以过滤名称 Search: function () { xBlufi.notifyStartDiscoverBle({ 'isStart': true, }); }, // {"type":" 41","result":true,"data":{"errno":0,"errCode":0,"isDiscovering":true,"errMsg":"startBluetoothDevicesDiscovery:ok"}} // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]} // {"type":"1","result":true,"data":[{"deviceId":"57:F6:7D:62:D6:C0","name":"","RSSI":-97,"connectable":true,"advertisData":"4c0010072a1fbee24315680100100000000000000000000000000000","advertisServiceUUIDs":[],"localName":"","serviceData":{}},{"deviceId":"42:E0:22:C2:6E:25","name":"","RSSI":-92,"connectable":true,"advertisData":"4c001007221fa8d7a84ba8","advertisServiceUUIDs":[],"localName":"","serviceData":{}}]} funListenDeviceMsgEvent: function (options) { var that = this; switch (options.type) { //发现设备列表回调开始 case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_START: if (!options.result) { console.log("蓝牙未开启 fail =》", options) wx.showToast({ title: '蓝牙未开启', icon: 'none' }) } else { //蓝牙搜索开始 that.setData({ searching: true }); } break; // [{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0","RSSI":-62,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fd3af0","serviceData":{}}] ///发现设备列表回调 case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS: // 搜索到蓝牙 if (options.result) { var devicesList = options.data.filter((v) => { var name = v.name; var isLexin = name.indexOf(lexin_devices.leXin) !== -1; var isJungzheng1 = junzhengUtil.isJungzheng1(name); var isJungzheng2 = junzhengUtil.isJungzheng2(name); return isLexin || isJungzheng1 || isJungzheng2; ///2AXD // return v.name == "AIrSMArT_d0311004155e"; ///X4 // return v.name == "AIrSMArT_861210052356337"; }); if (!strings.isEmpty(devicesList)) { ///过滤乐鑫设备 that.setData({ islanya: true, devicesList: devicesList, }); } } break; // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FD:3A:F2","name":"BLUFI_7cdfa1fd3af0"}} // {"type":"0","result":true,"data":{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24"}} ///主动去连接连接 case xBlufi.XBLUFI_TYPE.TYPE_CONNECTED: wx.hideLoading(); if (options.result) { if (that.data.isOnBluetooth) { return; }; that.setData({ isOnBluetooth: true, }); setTimeout(() => { that.data.isSubmit = false; that.setData({ islanya: false, devicesList: [], isOnBluetooth: false, }); var param = 'deviceId=' + options.data.deviceId + '&name=' + options.data.name + "¶m=" + param; routeUtil.redirectParam(routePath.deviceConnect3, param); }, 200); } else { that.data.isSubmit = false; wx.showModal({ title: '提示', content: '连接失败', showCancel: false }); } break; ///停止发现设备列表回调 case xBlufi.XBLUFI_TYPE.TYPE_GET_DEVICE_LISTS_STOP: if (options.result) { //蓝牙停止搜索ok console.log('蓝牙停止搜索ok') } else { //蓝牙停止搜索失败 console.log('蓝牙停止搜索失败') } that.setData({ searching: false }); break; } }, // [{"deviceId":"7C:DF:A1:FC:BB:26","name":"BLUFI_7cdfa1fcbb24","RSSI":-51,"connectable":true,"advertisData":"","advertisServiceUUIDs":["0000FFFF-0000-1000-8000-00805F9B34FB"],"localName":"BLUFI_7cdfa1fcbb24","serviceData":{}}] async Connect(e) { var that = this; const devicesList = that.data.devicesList; if (devicesList.length < 1) { wx.showToast({ title: '请短按设备配对键', icon: 'none', duration: 2000 }); return; }; if (that.data.isSubmit) { return; }; that.data.isSubmit = true; // 停止搜索 xBlufi.notifyStartDiscoverBle({ 'isStart': false, }); var name = devicesList[0].name; var deviceId = devicesList[0].deviceId; var isJungzheng1 = junzhengUtil.isJungzheng1(name); var isJungzheng2 = junzhengUtil.isJungzheng2(name); if (isJungzheng1 || isJungzheng2) { var param = 'deviceId=' + deviceId + '&name=' + name + "¶m=" + JSON.stringify(that.data.connectDevice) routeUtil.redirectParam(routePath.deviceConnect4, param); return; } //开始连接 var name = devicesList[0].name; // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId) xBlufi.notifyConnectBle({ isStart: true, name: name, deviceId: deviceId, }); wx.showLoading({ title: '请稍后', }); }, getBluetoothStatus() { app.getBluetoothStatus(); }, notRoter() { wx.navigateBack({ delta: 1 }); }, onUnload: function () { var that = this; xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent); }, })