const app = getApp(); let xBlufi = require("../../utils/blufi/xBlufi.js"); import lexin_devices from '../../utils/lexin/devices'; 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, }, onLoad() { const that = this; 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) { const 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) { if (options.data.filter((v) => v.name.indexOf(lexin_devices.leXin) !== -1).length > 0) { ///过滤乐鑫设备 that.setData({ islanya: true, devicesList: options.data.filter((v) => v.name.indexOf(lexin_devices.leXin) !== -1) }); } } 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.setData({ isSubmit: false, islanya: false, devicesList: [], isOnBluetooth: false, }); wx.navigateTo({ url: '../deviceConnect3/deviceConnect3?deviceId=' + options.data.deviceId + '&name=' + options.data.name, }); }, 200); } else { 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.setData({ isSubmit: true }); // 停止搜索 xBlufi.notifyStartDiscoverBle({ 'isStart': false, }); //开始连接 let name = devicesList[0].name; // console.log('点击了,蓝牙准备连接的deviceId:' + devicesList[0].deviceId) xBlufi.notifyConnectBle({ isStart: true, deviceId: devicesList[0].deviceId, name }); wx.showLoading({ title: '请稍后', }); }, getBluetoothStatus() { app.getBluetoothStatus(); }, notRoter() { wx.navigateBack({ delta: 1 }); }, onUnload: function () { var that = this; xBlufi.listenDeviceMsgEvent(false, that.funListenDeviceMsgEvent); }, })