const { BtHelper } = require('../../devices/bt_helper'); import route_constant from '../../utils/route_constant.js' import route_util from '../../utils/route_util.js'; Page({ data: { connectDevice: {}, nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '连接设备', //导航栏 中间的标题 callback() { wx.navigateBack({ delta: 1, }) } }, connectStatus: 0, searchTips: "正在搜索设备,请保持开机状态…", subTips: "确认手机蓝牙已打开", buttonTips: "正在搜索设备", deviceImg: null, }, onLoad(options) { var that = this; var json = JSON.parse(options.param) console.log("要连接设备:", json) that.setData({ connectDevice: json }); that.startSearch(); }, ///搜索设备 async startSearch() { var that = this; that.setStatus(0); BtHelper.getInstance().startScan( that.data.connectDevice, async function (b) { if (!b) { that.setStatus(1); } }, async function (res) { await BtHelper.getInstance().stopSearch(); that.setStatus(2); that.data.connectDevice.deviceId = res.deviceId; that.data.connectDevice.connectable = res.connectable; that.data.connectDevice.mac = res.mac; } ); }, async connectToDvice() { var that = this; ///当前的同一个蓝牙 var isTheSame = getCurrentPages()[0].isTheSameBlue(that.data.connectDevice); if (isTheSame) { route_util.goBackRoute(route_constant.indexRoot); return; } ///断开蓝牙连接 wx.showLoading({ title: '请稍后' }); // await getCurrentPages()[0].cancelCurrent(); BtHelper.getInstance().connect(that.data.connectDevice, function (data) { console.log("连接成功:", data) wx.hideLoading(); that.setStatus(data ? 3 : 4) if (data) { that.data.connectDevice.connectType = 1; that.data.connectDevice.state = 'online'; getCurrentPages()[0].addBlueDevice(that.data.connectDevice); route_util.goBackRoute(route_constant.indexRoot); } }); }, ///连接设备按钮 connectDeviceTap() { var that = this; console.log("点击搜索状态", that.data.connectStatus) switch (that.data.connectStatus) { case 0: // 搜索中 break; case 1: case 4: // 搜索失败,点击重新搜索了 that.startSearch() break; case 2: that.connectToDvice() break; case 3: // 连接成功 break; } }, setStatus(bleType) { var that = this; var searchTips = "" var subTips = "" var buttonTips = "" let name = that.data.connectDevice.name; let typeList = that.data.connectDevice.typeList ?? [] let bleTypes = typeList.find(v => v.connectType == 1) let deviceLinkResp = bleTypes.deviceLinkResp ?? {} console.log("搜索状态", bleType) switch (bleType) { case 0: // 搜索中 searchTips = "正在搜索设备,请保持开机状态…" subTips = "确认手机蓝牙已打开" buttonTips = "正在搜索设备" that.data.deviceImg = deviceLinkResp.icon1 break; case 1: // 搜索失败 searchTips = "未搜索到" + name subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。" buttonTips = "重新搜索" that.data.deviceImg = deviceLinkResp.icon1 break; case 2: // 搜索到 searchTips = "搜索到" + name subTips = "" buttonTips = "连接" that.data.deviceImg = deviceLinkResp.icon2 break; case 3: // 连接成功 searchTips = "连接到" + name subTips = "" buttonTips = "连接成功" that.data.deviceImg = deviceLinkResp.icon2 break; case 4: // 连接失败 searchTips = "未连接到" + name subTips = "请检查设备是否被其他手机连接,或在手机蓝牙忽略掉原来的蓝牙连接重新连接。" buttonTips = "重新搜索" that.data.deviceImg = deviceLinkResp.icon1 break; } that.setData({ searchTips: searchTips, subTips: subTips, buttonTips: buttonTips, connectStatus: bleType, deviceImg: that.data.deviceImg }) }, ///关闭界面时候触发 onUnload: function () { BtHelper.getInstance().stopSearch(); } }) // wx.getSystemInfo({ // success: function (res) { // // { "screenWidth": 407, "cpuType": "unknown", "phoneCalendarAuthorized": true, "windowHeight": 888, "bluetoothEnabled": true, "bluetoothAuthorized": true, "language": "zh_CN", "microphoneAuthorized": true, "fontSizeScaleFactor": 1, "locationAuthorized": true, "notificationAuthorized": true, "model": "23078RKD5C", "statusBarHeight": 35, "safeArea": { "width": 407, "right": 407, "top": 35, "left": 0, "bottom": 888, "height": 853 }, "brand": "Redmi", "windowWidth": 407, "locationEnabled": true, "benchmarkLevel": 15, "screenHeight": 888, "abi": "arm64-v8a", "version": "8.0.53", "cameraAuthorized": true, "deviceAbi": "arm64-v8a", "system": "Android 14", "memorySize": 15367, "fontSizeSetting": 16, "pixelRatio": 3, "deviceOrientation": "portrait", "wifiEnabled": true, "screenTop": 0, "errMsg": "getSystemInfo:ok", "platform": "android", "SDKVersion": "3.6.6", "enableDebug": false, "devicePixelRatio": 3, "host": { "env": "WeChat", "version": 671102271 }, "mode": "default" } // console.log('UUID: ' + JSON.stringify(res)); // console.log('MAC: ' + res.macaddress); // } // }); // didFindDevice(element) { // let deviceId = element.deviceId // var that = this; // // todo 暂定这样 // if ( // // deviceId.includes("D8:24:07:89:31") || // // // 2axk // // element.deviceId.includes("F5:A5:43:70:C8:F1") || // /// sr1 // element.deviceId.includes("E4:9F:80:09:40:EC") || // // 黑色2x // // element.deviceId.includes("F6:61:D8:24:E5:98") // // /// mac: D7:92:84:87:09:7D // // || // // element.deviceId.includes("F6:61:D8:24:E5:98") || // // 黑胶 // deviceId.includes("00:33:52:A7:3E:D0") // ) { // if (element.connectable == true) { // console.log("找到设备ble", element); // that.data.connectDevice.mac = element.mac // that.data.connectDevice.deviceId = element.deviceId // BtHelper.getInstance().stopSearch() // that.setStatus(2) // return true // } // } // return false // },