// pages/bluConnect/bleconnect.js const app = getApp(); Page( { /** * 页面的初始数据 */ data: { scopeBluetooth: false, isScaning: 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; switch (_this.connectStatus) { case 1: // 搜索中 _this.connectTips = "正在搜索设备,请保持开机状态..."; break; case 2: // 连接中 _this.connectText = "连接中..."; _this.connectPic = ""; break; case 3: // 连接失败 _this.connectText = "连接失败"; _this.connectPic = "../../img/i.png"; break; case 4: // 连接成功 _this.connectText = "连接成功"; _this.connectPic = "../../img/h.png"; break; case 5: // 搜索成功 _this.deviceItem = { "pic": "../../img/g.png", "address": "xxxxxx", "name": "猫王钢琴音箱", "devName": "名称1", "state": "online", "power": 40 }; _this.connectTips = "搜索到" + _this.deviceItem.name; _this.buttonTips = "连接设备"; _this.isScaning = false; _this.connectText = "搜索成功"; break; } _this.setData({ buttonTips: _this.buttonTips, connectTips: _this.connectTips, isScaning: _this.isScaning, connectPic: _this.connectPic, connectText: _this.connectText, deviceItem: _this.deviceItem, connectStatus: _this.connectStatus }) }, getBluetoothStatusCallck(v) { this.connectStatus = 1; this.updateBleStatus(); this.setData({ scopeBluetooth: v, }) if (v) { this.scanDevice(); } }, setBluetooth() { wx.openSetting({ complete() { // app.getBluetoothStatus(); } }) }, connectDeviceTap() { const _this = this; if (_this.isScaning) { console.log("连接中2"); return; } // todo 测试 console.log("连接中"); this.connectStatus = 4; this.updateBleStatus(); setTimeout(() => { const eventChannel = _this.getOpenerEventChannel(); eventChannel.emit('getBackData', { backData: _this.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() { this.setData({ isScaning: true, }); setTimeout(() => { console.log("2324"); this.connectStatus = 5; this.updateBleStatus(); }, 3000); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { if (options.isLognlink) { } app.getBluetoothStatus(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })