更换WIFI
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
const app = getApp();
|
||||
import $ from "../../utils/request";
|
||||
|
||||
Page({
|
||||
data: {
|
||||
progress: 0,
|
||||
|
||||
BLUE_STATE: {},
|
||||
connectState: "",
|
||||
|
||||
device: null,
|
||||
wifiInfo: {
|
||||
ssid: "",
|
||||
password: ""
|
||||
}
|
||||
},
|
||||
|
||||
// 存储回调函数的引用,以便在 detached 时取消监听
|
||||
_connectStateWatcher: null,
|
||||
|
||||
onLoad(options) {
|
||||
this.checkBluetoothPermissionAndInit();
|
||||
|
||||
|
||||
app.globalData.ppScale.plugin.bus.subscribe("devicesModel", (res) => {
|
||||
console.log("searchDevice ===》devicesModel", res);
|
||||
app.globalData.ppScale.device.mac = res.deviceMac;
|
||||
|
||||
app.globalData.ppScale.plugin.bus.subscribe("deviceConnect", (res) => {
|
||||
console.log('connectedDevice ===》deviceConnect', res);
|
||||
|
||||
app.globalData.ppScale.plugin.ScaleAction.startDataProgress(true);
|
||||
app.globalData.ppScale.activeProtocol = app.globalData.ppScale.plugin.ScaleAction.getActiveProtocol();
|
||||
|
||||
app.globalData.ppScale.activeProtocol.codeUpdateMTU((res) => {
|
||||
console.log("connectedDevice ===》codeUpdateMTU", res);
|
||||
|
||||
|
||||
app.globalData.ppScale.device.name = this.data.device.name;
|
||||
app.globalData.ppScale.device.connection = this.data.device;
|
||||
this.selectComponent('#replaceNetwork1Component').getWiFiList();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`[Component] connectState 获取: ${app.globalData.ppScale.device.connectState}`);
|
||||
this.setData({
|
||||
BLUE_STATE: app.globalData.ppScale.plugin.BLUE_STATE,
|
||||
connectState: app.globalData.ppScale.device.connectState
|
||||
})
|
||||
this._connectStateWatcher = (newValue, oldValue) => {
|
||||
console.log(`[Component] connectState 变化: ${oldValue} -> ${newValue}`);
|
||||
this.setData({
|
||||
connectState: newValue || ''
|
||||
});
|
||||
};
|
||||
app.watch('ppScale.device.connectState', this._connectStateWatcher);
|
||||
},
|
||||
|
||||
checkBluetoothPermissionAndInit() {
|
||||
this.setData({
|
||||
initText: "正在检查蓝牙权限..."
|
||||
});
|
||||
wx.getSetting({
|
||||
success: (res) => {
|
||||
if (res.authSetting['scope.bluetooth']) {
|
||||
this.openBluetoothAdapter();
|
||||
} else {
|
||||
wx.authorize({
|
||||
scope: 'scope.bluetooth',
|
||||
success: () => {
|
||||
this.openBluetoothAdapter();
|
||||
},
|
||||
fail: (err) => {
|
||||
this.setData({
|
||||
initText: "蓝牙权限被拒绝。"
|
||||
});
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '您拒绝了蓝牙权限,将无法连接蓝牙秤。是否前往设置开启?',
|
||||
confirmText: '去开启',
|
||||
cancelText: '不开启',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
wx.openSetting({
|
||||
success: (settingRes) => {
|
||||
console.log('openSetting success', settingRes);
|
||||
},
|
||||
fail: (settingErr) => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
this.setData({
|
||||
initText: "获取权限设置失败。"
|
||||
});
|
||||
wx.showToast({
|
||||
title: '获取权限设置失败',
|
||||
icon: 'none'
|
||||
});
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
openBluetoothAdapter() {
|
||||
this.setData({
|
||||
initText: "正在初始化蓝牙..."
|
||||
});
|
||||
wx.openBluetoothAdapter({
|
||||
success: () => {
|
||||
this.setData({
|
||||
initText: "蓝牙初始化成功"
|
||||
});
|
||||
this.getDeviceSettingList();
|
||||
},
|
||||
fail: (err) => {
|
||||
this.setData({
|
||||
initText: "蓝牙初始化失败。"
|
||||
});
|
||||
if (err.errCode === 10001) {
|
||||
wx.showModal({
|
||||
title: "提示",
|
||||
content: "请确保手机蓝牙已开启。",
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if (err.errMsg && err.errMsg.includes('permission denied')) {
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '蓝牙权限仍被拒绝,无法使用蓝牙功能。',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: `蓝牙适配器打开失败: ${err.errMsg || err.errCode}`,
|
||||
icon: 'none',
|
||||
success: () => {
|
||||
wx.navigateBack({
|
||||
delta: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
// 获取自己已经配对上的设备
|
||||
getDeviceSettingList() {
|
||||
let params = {};
|
||||
$.ajax("weighingScale/list/device", params, "GET", true, "获取设备列表").then((res) => {
|
||||
this.setData({
|
||||
initText: "设备搜索中..."
|
||||
})
|
||||
app.globalData.ppScale.device.list = [];
|
||||
let seviceList = app.globalData.ppScale.device.setting.find((item) => {
|
||||
return item.deviceName === res.result[0].type
|
||||
});
|
||||
app.globalData.ppScale.plugin.Blue.setDeviceSetting([seviceList]);
|
||||
app.globalData.ppScale.plugin.Blue.start(res.result[0].type, false);
|
||||
app.globalData.ppScale.plugin.bus.subscribe("devicesList", (res_) => {
|
||||
console.log("searchDevice ===> devicesList", res_);
|
||||
|
||||
let fIndex = res_.findIndex(item => item.deviceId === res.result[0].deviceId);
|
||||
if (fIndex >= 0) {
|
||||
app.globalData.ppScale.plugin.Blue.stopBluetoothDevicesDiscovery();
|
||||
this.setData({
|
||||
device: res_[fIndex]
|
||||
})
|
||||
app.globalData.ppScale.plugin.Blue.createBLEConnection(res_[fIndex]);
|
||||
}
|
||||
});
|
||||
})
|
||||
},
|
||||
|
||||
// 点击设备重连
|
||||
connectedDevice() {
|
||||
let device = this.data.device;
|
||||
if(device) {
|
||||
ppScale.plugin.Blue.createBLEConnection(device);
|
||||
}
|
||||
},
|
||||
|
||||
// 选择了某个wifi
|
||||
wifiSSIDChange(e) {
|
||||
let status = e.detail.status;
|
||||
if(status) {
|
||||
this.setData({
|
||||
['wifiInfo.ssid']: e.detail.data.ssid,
|
||||
progress: this.data.progress + 1
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 设置wifi密码
|
||||
wifiPWDChange(e) {
|
||||
let status = e.detail.status;
|
||||
if(status) {
|
||||
this.setData({
|
||||
['wifiInfo.password']: e.detail.data,
|
||||
progress: this.data.progress + 1
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
wifiSetChange(e) {
|
||||
let status = e.detail.status;
|
||||
if(status) {
|
||||
this.setData({
|
||||
progress: this.data.progress + 1
|
||||
})
|
||||
} else {
|
||||
this.setData({
|
||||
progress: 0
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 卸载事件监听
|
||||
onUnload() {
|
||||
if (this._connectStateWatcher) {
|
||||
app.unwatch('ppScale.device.connectState', this._connectStateWatcher);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"usingComponents": {
|
||||
"replaceNetwork1": "/components/replaceNetwork_1/replaceNetwork_1",
|
||||
"replaceNetwork2": "/components/replaceNetwork_2/replaceNetwork_2",
|
||||
"replaceNetwork3": "/components/replaceNetwork_3/replaceNetwork_3",
|
||||
"replaceNetwork4": "/components/replaceNetwork_4/replaceNetwork_4"
|
||||
},
|
||||
"navigationBarTitleText": "更换Wi-Fi"
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<view class="replaceNetwork">
|
||||
<view class="header">
|
||||
<view>
|
||||
<view>智能蓝牙秤</view>
|
||||
<view>型号:{{device.name}}</view>
|
||||
</view>
|
||||
<view>
|
||||
<block wx:if="{{connectState == BLUE_STATE.CONNECTSUCCESS}}">
|
||||
<view style="background-color: #2AC79F;"></view>
|
||||
<view style="color: #2AC79F;">连接成功</view>
|
||||
</block>
|
||||
<block wx:else>
|
||||
<view style="background-color: #F24439;"></view>
|
||||
<view bind:tap="connectedDevice" style="color: #F24439;">连接失败,点击重试</view>
|
||||
</block>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="section">
|
||||
<block wx:if="{{progress === 0}}">
|
||||
<replaceNetwork1 id="replaceNetwork1Component" bind:wifiEvent="wifiSSIDChange"></replaceNetwork1>
|
||||
</block>
|
||||
<block wx:if="{{progress === 1}}">
|
||||
<replaceNetwork2 ssid="{{wifiInfo.ssid}}" id="replaceNetwork2Component" bind:wifiEvent="wifiPWDChange"></replaceNetwork2>
|
||||
</block>
|
||||
<block wx:if="{{progress === 2}}">
|
||||
<replaceNetwork3 ssid="{{wifiInfo.ssid}}" password="{{wifiInfo.password}}" bind:wifiEvent="wifiSetChange"></replaceNetwork3>
|
||||
</block>
|
||||
<block wx:if="{{progress === 3}}">
|
||||
<replaceNetwork4></replaceNetwork4>
|
||||
</block>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,69 @@
|
||||
.replaceNetwork {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
display: flex;
|
||||
flex-flow: column;
|
||||
padding: 24rpx 30rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.header {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-wrap: nowrap;
|
||||
align-items: center;
|
||||
padding: 30rpx 40rpx;
|
||||
box-sizing: border-box;
|
||||
border-radius: 16rpx;
|
||||
margin-bottom: 24rpx;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(1) {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(1)>view:nth-of-type(1) {
|
||||
color: #252535;
|
||||
height: 32rpx;
|
||||
font-weight: bold;
|
||||
font-size: 32rpx;
|
||||
line-height: 32rpx;
|
||||
margin-bottom: 16rpx;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(1)>view:nth-of-type(2) {
|
||||
color: #808080;
|
||||
height: 26rpx;
|
||||
font-size: 26rpx;
|
||||
line-height: 26rpx;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(2) {
|
||||
height: 28rpx;
|
||||
display: flex;
|
||||
flex-wrap: nowrap;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(2)>view:nth-of-type(1) {
|
||||
width: 12rpx;
|
||||
height: 12rpx;
|
||||
border-radius: 50%;
|
||||
margin-right: 14rpx;
|
||||
}
|
||||
|
||||
.header>view:nth-of-type(2)>view:nth-of-type(2) {
|
||||
height: 28rpx;
|
||||
font-weight: bold;
|
||||
font-size: 28rpx;
|
||||
line-height: 28rpx;
|
||||
}
|
||||
|
||||
|
||||
.section {
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
border-radius: 16rpx;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
Reference in New Issue
Block a user