286 lines
6.9 KiB
JavaScript
286 lines
6.9 KiB
JavaScript
const app = getApp();
|
|
const ppScale = app.globalData.ppScale;
|
|
|
|
Page({
|
|
data: {
|
|
progress: {
|
|
index: 0,
|
|
data: ['配置蓝牙', '初始化用户信息', '配备网络', '完成']
|
|
},
|
|
|
|
progressNext: false,
|
|
device: null
|
|
},
|
|
|
|
onLoad() {
|
|
// 一次性数据初始化,订阅逻辑在 onShow 中注册
|
|
},
|
|
|
|
onShow() {
|
|
console.log("configureDevice onShow");
|
|
// 延迟 300ms 确保 stop() 完全释放后再重新注册(参照官方 demo)
|
|
setTimeout(() => {
|
|
// 重新注册 app 级别的全局 bus 订阅
|
|
app.registerBusListeners();
|
|
|
|
// 页面级 bus 订阅
|
|
ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
|
console.log("===》devicesModel", res);
|
|
ppScale.device.mac = res.deviceMac;
|
|
this._macReady = true;
|
|
this._tryProgress();
|
|
});
|
|
|
|
ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
|
console.log('===》deviceConnect', res);
|
|
|
|
ppScale.plugin.ScaleAction.startDataProgress(true);
|
|
ppScale.activeProtocol = ppScale.plugin.ScaleAction.getActiveProtocol();
|
|
|
|
let currentStep = this.data.progress.index;
|
|
|
|
// 步骤 1/2/3:最小化后重连,只需恢复连接状态(跳过绑定检查)
|
|
if (currentStep >= 1) {
|
|
ppScale.activeProtocol.codeUpdateMTU((res) => {
|
|
console.log("===》重连 codeUpdateMTU", res);
|
|
|
|
ppScale.device.name = this.data.device.name;
|
|
ppScale.device.connection = this.data.device;
|
|
|
|
wx.hideLoading();
|
|
this._startKeepAlive();
|
|
|
|
// 配网步骤:重新获取 WiFi 列表
|
|
if (currentStep === 2) {
|
|
const comp = this.selectComponent('#configureDevice3');
|
|
if (comp) {
|
|
comp.reInitWifi();
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 步骤 0:首次连接设备
|
|
ppScale.activeProtocol.codeUpdateMTU((res) => {
|
|
console.log("===》codeUpdateMTU", res);
|
|
|
|
ppScale.activeProtocol.codeSyncTime((codeSyncTime) => {
|
|
console.log("===》codeSyncTime", codeSyncTime)
|
|
|
|
wx.hideLoading();
|
|
this._startKeepAlive();
|
|
ppScale.device.name = this.data.device.name;
|
|
ppScale.device.connection = this.data.device;
|
|
const nameArr = this.data.device.name.split("-");
|
|
ppScale.version = nameArr.length === 5 ? "domain2" : "domain1";
|
|
this._connectReady = true;
|
|
this._tryProgress();
|
|
})
|
|
});
|
|
});
|
|
|
|
// 如果之前已选择过设备,自动扫描并重连
|
|
if (this.data.device) {
|
|
wx.showLoading({ title: "正在重新连接...", mask: true });
|
|
ppScale.plugin.bus.subscribe("devicesList", (res) => {
|
|
let fIndex = res.findIndex(item => item.deviceId === this.data.device.deviceId);
|
|
if (fIndex >= 0) {
|
|
ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
|
ppScale.plugin.Blue.createBLEConnection(res[fIndex]);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 通过完整链路重新初始化蓝牙(权限检查 → 打开适配器 → start)
|
|
this.checkBluetoothPermissionAndInit();
|
|
}, 300);
|
|
},
|
|
|
|
// 选择了某个设备
|
|
deviceChange(e) {
|
|
let status = e.detail.status;
|
|
if (status) {
|
|
wx.showLoading({
|
|
title: "蓝牙配对中...",
|
|
mask: true
|
|
});
|
|
this.setData({
|
|
progressNext: true
|
|
})
|
|
let device = e.detail.device;
|
|
this.connectedDevice_(device);
|
|
}
|
|
},
|
|
|
|
connectedDevice(e) {
|
|
app.resetReconnectCount();
|
|
wx.showLoading({
|
|
title: "正在尝试连接...",
|
|
mask: true
|
|
});
|
|
this.setData({
|
|
progressNext: false
|
|
})
|
|
let device = e.detail.device;
|
|
// 先断开旧连接再重新连接
|
|
ppScale.plugin.Blue.disconnect((disres) => {
|
|
this.connectedDevice_(device);
|
|
});
|
|
},
|
|
|
|
connectedDevice_(device) {
|
|
if (device) {
|
|
this.setData({
|
|
device: device
|
|
})
|
|
ppScale.plugin.Blue.createBLEConnection(device);
|
|
}
|
|
},
|
|
|
|
setDeviceUserList(e) {
|
|
let status = e.detail.status;
|
|
if (status) {
|
|
this.setProgress();
|
|
}
|
|
},
|
|
|
|
setDeviceConfig(e) {
|
|
let status = e.detail.status;
|
|
if (status) {
|
|
this.setProgress();
|
|
}
|
|
},
|
|
|
|
setConfigSuccessful() {
|
|
ppScale.plugin.Blue.stop();
|
|
wx.switchTab({
|
|
url: "/pages/home/home"
|
|
})
|
|
},
|
|
|
|
setProgress() {
|
|
this.setData({
|
|
["progress.index"]: this.data.progress.index + 1
|
|
})
|
|
},
|
|
|
|
// 连接完成 + mac 获取完成,双条件满足后才进入下一步
|
|
_connectReady: false,
|
|
_macReady: false,
|
|
_tryProgress() {
|
|
if (this._connectReady && this._macReady && this.data.progressNext) {
|
|
this._connectReady = false;
|
|
this._macReady = false;
|
|
this.setData({ progressNext: false });
|
|
this.setProgress();
|
|
}
|
|
},
|
|
|
|
// 启动保活定时器,每10秒发送一次保活指令
|
|
_keepAliveTimer: null,
|
|
_startKeepAlive() {
|
|
this._stopKeepAlive();
|
|
this._keepAliveTimer = setInterval(() => {
|
|
if (ppScale.activeProtocol) {
|
|
ppScale.activeProtocol.sendKeepAliveCode();
|
|
}
|
|
}, 15000);
|
|
},
|
|
|
|
// 清除保活定时器
|
|
_stopKeepAlive() {
|
|
if (this._keepAliveTimer) {
|
|
clearInterval(this._keepAliveTimer);
|
|
this._keepAliveTimer = null;
|
|
}
|
|
},
|
|
|
|
checkBluetoothPermissionAndInit() {
|
|
wx.getSetting({
|
|
success: (res) => {
|
|
if (res.authSetting['scope.bluetooth']) {
|
|
this.openBluetoothAdapter();
|
|
} else {
|
|
wx.authorize({
|
|
scope: 'scope.bluetooth',
|
|
success: () => {
|
|
this.openBluetoothAdapter();
|
|
},
|
|
fail: () => {
|
|
wx.hideLoading();
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: '蓝牙权限被拒绝,无法连接设备。是否前往设置开启?',
|
|
confirmText: '去开启',
|
|
cancelText: '不开启',
|
|
success: (modalRes) => {
|
|
if (modalRes.confirm) {
|
|
wx.openSetting();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
},
|
|
fail: () => {
|
|
wx.hideLoading();
|
|
}
|
|
});
|
|
},
|
|
|
|
openBluetoothAdapter() {
|
|
wx.openBluetoothAdapter({
|
|
success: () => {
|
|
// 蓝牙适配器打开成功,设置设备配置后启动扫描
|
|
let setting = ppScale.device.setting;
|
|
ppScale.plugin.Blue.setDeviceSetting(setting);
|
|
let deviceNames = setting.map(item => item.deviceName);
|
|
ppScale.plugin.Blue.start(deviceNames, false);
|
|
},
|
|
fail: (err) => {
|
|
wx.hideLoading();
|
|
if (err.errCode === 10001) {
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: "请确保手机蓝牙已开启。",
|
|
showCancel: false
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
onHide() {
|
|
console.log("configureDevice onHide");
|
|
this._stopKeepAlive();
|
|
// 配网步骤:立即清空 WiFi 列表(stop 后数据已失效)
|
|
if (this.data.progress.index === 2) {
|
|
const comp = this.selectComponent('#configureDevice3');
|
|
if (comp) {
|
|
comp.setData({
|
|
progress: 0,
|
|
wifiList: [],
|
|
selectWifi: null,
|
|
selectWifiPWD: ""
|
|
});
|
|
}
|
|
}
|
|
app.resetReconnectCount();
|
|
app.setGlobalData('ppScale.device.connectState', null);
|
|
this._connectReady = false;
|
|
this._macReady = false;
|
|
ppScale.plugin.Blue.stop();
|
|
},
|
|
|
|
onUnload() {
|
|
console.log("configureDevice onUnload");
|
|
this._stopKeepAlive();
|
|
app.resetReconnectCount();
|
|
app.setGlobalData('ppScale.device.connectState', null);
|
|
this._connectReady = false;
|
|
this._macReady = false;
|
|
ppScale.plugin.Blue.stop();
|
|
}
|
|
}) |