var ble = require('../../services/ble') var i18n = require('../../i18n/index') Page({ data: { statusBarHeight: 44, battery: 0, connected: false, reconnecting: false }, onShow: function () { i18n.bind(this) }, 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: i18n.t('common.deviceNotConnected'), icon: 'none' }) return } wx.redirectTo({ url: '/pages/treatment-setup/treatment-setup' }) }, onRetry: function () { var self = this if (self.data.reconnecting) return if (ble.isConnected()) { self.setData({ connected: true }) return } self.setData({ connected: false, reconnecting: true }) ble.disconnect() setTimeout(function () { ble.startScan({ onFound: function () {}, onConnected: function () { clearTimeout(self._reconnectTimer) self.setData({ connected: true, reconnecting: false }) wx.showToast({ title: i18n.t('wearCheck.connectSuccess'), icon: 'success' }) }, onError: function (err) { clearTimeout(self._reconnectTimer) self.setData({ reconnecting: false }) wx.showToast({ title: err.msg || i18n.t('wearCheck.connectFailed'), icon: 'none' }) } }) self._reconnectTimer = setTimeout(function () { ble.stopScan() self.setData({ reconnecting: false }) wx.showToast({ title: i18n.t('wearCheck.searchTimeout'), icon: 'none' }) }, 5000) }, 500) }, onUnload: function () { clearTimeout(this._reconnectTimer) ble.stopScan() } })