Files
bodyWeight/pages/searchDevice/searchDevice.js
T
2025-04-08 14:08:04 +08:00

170 lines
4.7 KiB
JavaScript

const app = getApp();
Page({
data: {
getDeviceSetting: false,
initText: "",
devicesList: [],
searchDevicesList: true,
},
onLoad(options) {
this.initBluetooth();
},
onShow() {
if (this.data.getDeviceSetting) {
this.getDeviceSettingList();
}
},
// 初始化蓝牙
initBluetooth() {
this.setData({
initText: "正在初始化蓝牙..."
})
wx.openBluetoothAdapter({
success: () => {
this.setData({
initText: "蓝牙初始化成功"
})
this.getDeviceSettingList();
},
fail: (err) => {
this.setData({
initText: "蓝牙初始化失败"
})
if (err.errCode === 10001) {
wx.showModal({
title: "提示",
content: "请打开手机蓝牙",
});
}
},
});
},
// async refreshToken(fn, flag) {
// let bodyToken = wx.getStorageSync('bodyToken');
// let bodyTokenTime = wx.getStorageSync('bodyTokenTime');
// if (!bodyToken || bodyTokenTime * 1000 < +new Date() || flag) {
// let res = await app.globalData.ppScale.plugin.refreshToken({
// url: "https://uniquehealth.lefuenergy.com",
// data: {
// "appKey": app.globalData.ppScale.options.key,
// "appSecret": app.globalData.ppScale.options.secret
// }
// });
// if (res.data.code == 200) {
// wx.setStorageSync('bodyToken', res.data.data.token);
// wx.setStorageSync('bodyTokenTime', res.data.data.expireTime);
// fn();
// }
// } else {
// fn();
// }
// },
// getDeviceSettingList() {
// this.setData({
// initText: "获取设备配置中..."
// })
// let bodyToken = wx.getStorageSync("bodyToken");
// app.globalData.ppScale.plugin.getDeviceSettingList({
// url: "https://uniquehealth.lefuenergy.com",
// data: {
// appKey: app.globalData.ppScale.options.key
// },
// header: {
// "token": bodyToken,
// "Accept-Language": wx.getStorageSync("lang") || 'zh'
// }
// }).then((res) => {
// if (res.data.code == 200) {
// this.setData({
// initText: "设备搜索中..."
// });
// app.globalData.ppScale.plugin.Blue.setDeviceSetting(res.data.data);
// app.globalData.ppScale.device.setting = res.data.data;
// let deviceNames = res.data.data.map(item => item.deviceName);
// app.globalData.ppScale.plugin.Blue.start(deviceNames, false);
// app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
// console.log("searchDevice ===> devicesList", res_);
// this.setData({
// getDeviceSetting: false,
// initText: "设备搜索完成"
// });
// app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
// app.globalData.ppScale.device.list = res_;
// app.globalData.ppScale.device.connection = res_[0];
// wx.navigateTo({
// url: "/pages/connectedDevice/connectedDevice"
// })
// });
// } else if (res.data.code == 401 || res.data.code == 4008) {
// this.refreshToken(() => {
// this.getDeviceSettingList();
// }, true);
// }
// });
// },
getDeviceSettingList() {
// app.globalData.ppScale.plugin.Blue.visibleLog(true);
this.setData({
initText: "设备搜索中..."
});
let seviceList = app.globalData.ppScale.device.setting;
app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList);
let deviceNames = seviceList.map(item => item.deviceName);
app.globalData.ppScale.plugin.Blue.start(deviceNames, false);
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => {
console.log("searchDevice ===> devicesList", res);
this.setData({
devicesList: res
});
this.selectDevice();
});
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
console.log("searchDevice ===》devicesModel", res);
app.globalData.ppScale.device.mac = res.deviceMac;
});
},
selectDevice() {
if(this.data.devicesList && this.data.devicesList.length && this.data.searchDevicesList) {
this.setData({
searchDevicesList: false
});
setTimeout(() => {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
app.globalData.ppScale.device.list = this.data.devicesList;
if(this.data.devicesList.length === 1) {
app.globalData.ppScale.device.connection = this.data.devicesList[0];
wx.navigateTo({
url: "/pages/connectedDevice/connectedDevice"
})
} else {
let itemLists = this.data.devicesList.map(item => item.name);
wx.showActionSheet({
itemList: itemLists,
alertText: "请选择你要使用的设备",
success: (res) => {
app.globalData.ppScale.device.connection = this.data.devicesList[res.tapIndex];
wx.navigateTo({
url: "/pages/connectedDevice/connectedDevice"
})
},
fail: () => {
wx.navigateBack({
delta: 1
})
}
})
}
}, 3000);
}
}
})