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

114 lines
2.8 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
Page({
data: {
deviceConnect: false,
deviceInfo: null,
},
onLoad(options) {
this.getDevice();
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
console.log('deviceInfo ===》deviceConnect', res);
app.globalData.ppScale.plugin.ScaleAction.startDataProgress();
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => {
console.log("deviceInfo ===》codeUpdateMTU", res);
this.setData({
deviceConnect: true
})
});
});
},
// 初始化蓝牙
initBluetooth() {
wx.openBluetoothAdapter({
success: () => {
this.getDeviceSettingList();
},
fail: (err) => {
if (err.errCode === 10001) {
wx.showModal({
title: "提示",
content: "请打开手机蓝牙",
});
}
},
});
},
getDeviceSettingList() {
let seviceList = [];
app.globalData.ppScale.device.setting.map(item => {
if(this.data.deviceInfo.type === item.deviceName) {
seviceList.push(item);
}
})
if(seviceList && seviceList.length) {
app.globalData.ppScale.plugin.Blue.setDeviceSetting(seviceList);
app.globalData.ppScale.plugin.Blue.start(seviceList[0].deviceName, false);
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res) => {
console.log("deviceInfo ===> devicesList", res);
let fIndex = res.findIndex(item => item.deviceId === this.data.deviceInfo.deviceId);
if (fIndex >= 0) {
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
app.globalData.ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
return;
}
});
}
},
// 获取已经绑定的设备列表
getDevice() {
let params = {};
$.ajax("weighingScale/list/device", params, "GET", false).then((res) => {
this.setData({
deviceInfo: res.result[0]
})
this.initBluetooth();
})
},
delDevice() {
if(this.data.deviceConnect) {
app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
console.log("deviceInfo === codeClearDeviceData", res);
let scaleDeviceId = this.data.deviceInfo.macAddress.replace(/:/g, '');
let params = {
equipmentCode: scaleDeviceId,
};
$.ajax("weighingScale/del/device", params, "POST", true, "正在解绑设备...").then(res_ => {
wx.showToast({
icon: "none",
title: res_.message,
})
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500);
});
})
} else {
wx.showToast({
icon: "none",
title: "删除失败,请靠近设备并点亮后再试"
})
app.globalData.ppScale.plugin.Blue.stop();
this.getDeviceSettingList();
}
},
onUnload() {
app.globalData.ppScale.plugin.Blue.stop();
}
})