// pages/bluConnect/bleconnect.js const app = getApp(); Page( { /** * 页面的初始数据 */ data: { scopeBluetooth: false, connectStatus: 0, connectText: "连接中", connectPic: "", connectTips: "正在搜索设备,请保持开机状态...", buttonTips: "正在搜索设备", userFuzzyLocation: true, nvabarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '设备连接', //导航栏 中间的标题 callback: () => { wx.switchTab({ url: '../home/home', }) } }, isNotruter: false, deviceItem: { "pic": "../../img/g.png", "address": "xxxxxx", "name": "猫王钢琴音箱", "devName": "名称1", "state": "online", "power": 40 } }, updateBleStatus() { var _this = this; var deviceDic; switch (_this.data.connectStatus) { case 1: // 搜索中 deviceDic = { connectTips: "正在搜索设备,请保持开机状态..." } break; case 2: // 连接中 deviceDic = { connectText: "连接中...", connectPic: "" } break; case 3: // 连接失败 deviceDic = { connectText: "连接失败", connectPic: "../../img/i.png" } break; case 4: // 连接成功 deviceDic = { connectText: "连接成功", connectPic: "../../img/h.png", connectTips:"" } break; case 5: // 搜索到设备 var deviceItem = { "pic": "../../img/g.png", "address": "xxxxxx"+Math.random() , "name": "猫王钢琴音箱", "devName": "猫王钢琴音箱", "state": "online", "power": 40 } deviceDic = { connectTips: "搜索到" + deviceItem.name, buttonTips: "连接设备", connectText: "搜索成功", deviceItem: deviceItem } break; default: break; } if (deviceDic != null) { Object.assign(deviceDic, {"connectStatus": _this.data.connectStatus}) _this.setData(deviceDic) } }, getBluetoothStatusCallck(v) { this.updateBleStatus(); this.setData({ scopeBluetooth: v, connectStatus: v ? 1 : 0 }) if (v) { this.scanDevice(); } }, setBluetooth() { wx.openSetting({ complete() { // app.getBluetoothStatus(); } }) }, connectDeviceTap() { const _this = this; if (_this.data.connectStatus === 1) { return; } // todo 测试 this.data.connectStatus = 4; this.updateBleStatus(); setTimeout(() => { const eventChannel = _this.getOpenerEventChannel(); eventChannel.emit('getBackData', { backData: _this.data.deviceItem }); wx.navigateBack(); }, 1000); return; wx.openBluetoothAdapter({ success(res) { // var _this = this; // const eventChannel = this.getOpenerEventChannel(); // eventChannel.emit('getBackData', { // backData: _this.deviceItem // }); // wx.navigateBack(); }, fail(res) { console.log("连接失败:" + res); if (res.errCode === 10001) { wx.showToast({ title: '蓝牙未打开', icon: "none", duration: 2000 }) } } }); }, scanDevice() { //todo 扫描代码 setTimeout(() => { this.data.connectStatus = 5; this.updateBleStatus(); }, 3000); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (options.isLognlink) { } app.getBluetoothStatus(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })