Files
bodyWeight/pages/connectedDevice/connectedDevice.js
T
2025-03-25 20:52:15 +08:00

107 lines
2.8 KiB
JavaScript

const app = getApp();
import $ from "../../utils/request";
Page({
data: {
name: "",
deviceSelect: null
},
onLoad(options) {
let name = app.globalData.ppScale.device.name;
let deviceSelect = app.globalData.ppScale.device.connection;
this.setData({
name: name ? name : deviceSelect.name,
deviceSelect: deviceSelect
})
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
console.log('connectedDevice ===》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("connectedDevice ===》codeUpdateMTU", res);
app.globalData.ppScale.activeProtocol.codeFetchBindingState((res) => {
console.log("connectedDevice ===》codeFetchBindingState", res);
wx.hideLoading();
if (res === 1) {
wx.showModal({
title: '提示',
content: '当前设备已被绑定,你确定要覆盖绑定吗?',
success: (res) => {
if (res.confirm) {
app.globalData.ppScale.activeProtocol.codeClearDeviceData("00", (res) => {
console.log("deviceInfo === codeClearDeviceData", res);
let scaleDeviceId = app.globalData.ppScale.device.mac.replace(/:/g, '');
let params = {
equipmentCode: scaleDeviceId,
};
$.ajax("weighingScale/del/device", params, "POST", true, "正在初始化设备...").then(res => {
wx.showToast({
icon: "none",
title: res.message,
})
let pages = getCurrentPages();
let prevPage = pages[pages.length - 2];
prevPage.setData({
getDeviceSetting: true
})
setTimeout(() => {
wx.navigateBack({
delta: 1
})
}, 1500);
});
})
} else if (res.cancel) {
app.globalData.ppScale.plugin.Blue.disconnect();
}
}
})
} else {
app.globalData.ppScale.device.name = this.data.name;
wx.navigateTo({
url: "/pages/connectionSuccessful/connectionSuccessful"
})
}
})
});
});
},
nameInput(e) {
this.setData({
name: e.detail.value
})
},
// 配对
openConnectionSuccessful() {
let deviceSelect = this.data.deviceSelect;
if (deviceSelect) {
let name = this.data.name;
if (name) {
wx.showLoading({
title: "蓝牙配对中...",
mask: true
});
app.globalData.ppScale.plugin.Blue.createBLEConnection(deviceSelect);
} else {
wx.showToast({
icon: "none",
title: "设备名称不能为空"
})
}
} else {
wx.showToast({
icon: "none",
title: "配对设备不能为空"
})
}
},
})