// 示例demo const plugin = requirePlugin('ppScale-plugin') var app = getApp(); Page({ data: { deviceslist:[ ], param: { appKey:"lefu7afd0a504245139b" } }, onLoad() { }, onUnload() { // 业务结束后需要调用此方法 plugin.Blue.stop() }, // 选择设备 selItem: function(res) { var that = this; var devItem = res.currentTarget.dataset.item; var device = this.data.deviceslist.find(item => item.deviceId === devItem.deviceId) app.globalData.selmac = device; console.log("this.globalData",app.globalData.selmac) that.setData({ selmac: device }) }, refresh: function(res) { // plugin.Blue.startBluetoothDevicesDiscovery(); plugin.Blue.start([]); console.log("刷新", plugin); }, doback: function(res) { if(app.globalData.selmac){ wx.navigateTo({ url: '/pages/devicedetail/index' }) } else if(this.data.deviceslist.length <= 0) { wx.showModal({ title: '温馨提示', content: `请打开手机蓝牙并且双脚站上蓝牙称扫描设备`, showCancel: false, }) }else { wx.showModal({ title: '温馨提示', content: `请选择上面要连接的设备`, showCancel: false, }) console.log('请选择设备') } }, doback2: function(res) { if(app.globalData.selmac){ wx.navigateTo({ url: '/pages/devicedetailfood/index' }) }else { wx.showModal({ title: '温馨提示', content: `请选择上面要连接的设备`, showCancel: false, }) console.log('请选择设备') } }, /** * 刷新token * @param {function} fn 获取token后执行函数 * @param {boolean} flag 请求刷新token */ async refreshToken(fn, flag) { let bodyToken = wx.getStorageSync('bodyToken'); let bodyTokenTime = wx.getStorageSync('bodyTokenTime'); if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) { let res = await plugin.refreshToken({ url: 'https://uniquehealth.lefuenergy.com', data: { "appSecret":"AlgH7XDNWG1nABz3LSKdhaYTFUdETSBmKL4pRmD5mS4=", "appKey": "lefu7afd0a504245139b" } }); console.log("res", res); if (res.data.code == 200) { wx.setStorageSync('bodyToken', res.data.data.token); wx.setStorageSync('bodyTokenTime', res.data.data.expireTime); fn(); } } else { fn(); } }, getDeviceSettingList(param) { let bodyToken = wx.getStorageSync('bodyToken'); console.log("获取最新设备列表"); console.log("param",param) plugin.getDeviceSettingList({ url: 'https://uniquehealth.lefuenergy.com', data: param, header: { "token": bodyToken, "Accept-Language": wx.getStorageSync("lang") || 'zh' } }).then(this.handleResponse); }, handleResponse(res) { if (res.data.code == 200) { // 配置蓝牙设备列表 console.log("res.data.data",res.data.data) plugin.Blue.setDeviceSetting(res.data.data) } else if (res.data.code == 401) { // token过期 重新请求token解析数据 this.refreshToken(() => { this.getDeviceSettingList(this.data.param) }, true); } }, onShow() { // 高于1.0.5版本 如无法扫描设备 需请求获取并设置最新设备配置列表 console.log("开始扫描周边设备"); /* this.getDeviceSettingList(this.data.param) */ plugin.Blue.setDeviceSetting([ { "createBy": null, "createTime": "2023-10-11 16:57:08", "updateBy": null, "updateTime": "2023-10-20 14:05:03", "remark": null, "id": 39, "deviceName": "LFSmart Scale", "sign": "CE4004", "advLength": 20, "macAddressStart": 7, "deviceConnectType": 2, "deviceType": 1, "deviceProtocolType": 2, "deviceCalcuteType": 3, "devicePowerType": 1, "deviceFuncType": 67, "deviceAccuracyType": 2, "deviceUnitType": "0,1,11", "calorieStatus": 1, "uhStatus": 1, "status": 0, "imgUrl": null }, ]) console.log("plugin.Blue",plugin) plugin.Blue.start([],false) // 获取扫描设备列表 plugin.bus.subscribe("devicesList", (res) => { console.log("devicesList'", JSON.parse(JSON.stringify(res))) this.setData({ deviceslist: res }); }); }, })