var ble = require('../../services/ble') Page({ data: { statusBarHeight: 44, battery: 0, connected: false }, onBack: function () { wx.navigateBack({ fail: function () { wx.reLaunch({ url: '/pages/index/index' }) } }) }, onLoad: function () { var app = getApp() this.setData({ statusBarHeight: app.globalData.statusBarHeight, connected: ble.isConnected() }) var device = app.globalData.currentDevice || {} this.setData({ battery: device.battery || 0 }) }, onNext: function () { if (!ble.isConnected()) { wx.showToast({ title: '设备未连接', icon: 'none' }) return } wx.redirectTo({ url: '/pages/treatment-setup/treatment-setup' }) }, onRetry: function () { var self = this if (ble.isConnected()) { self.setData({ connected: true }) return } self.setData({ connected: false }) ble.disconnect() setTimeout(function () { ble.startScan({ onFound: function () {}, onConnected: function () { clearTimeout(self._reconnectTimer) self.setData({ connected: true }) wx.showToast({ title: '连接成功', icon: 'success' }) }, onError: function (err) { clearTimeout(self._reconnectTimer) wx.showToast({ title: err.msg || '连接失败', icon: 'none' }) } }) self._reconnectTimer = setTimeout(function () { ble.stopScan() wx.showToast({ title: '搜索超时', icon: 'none' }) }, 15000) }, 500) }, onUnload: function () { clearTimeout(this._reconnectTimer) ble.stopScan() } })