Lösung des Problems, dass das Uniapp-Applet nach erfolgreicher Bluetooth-Verbindung zu anderen Seiten springt und die Einstellungsanweisungen nicht rechtzeitig aktualisiert werden

1. Das Bluetooth auf der Ladedetailseite hat sich erfolgreich verbunden und den Status erhalten. Sie müssen zu den Plug-and-Charge-Seiteneinstellungen springen. Es besteht das Problem, dass die Einstellung nicht rechtzeitig nach erfolgreicher Einstellung aktualisiert wird. 2. Lösungen : A. Klicken Sie auf Plug-and-Charge, um
Plug -
and-Charge auszuführen. Schalten Sie beim Anzeigen der Seite zunächst alle Monitore auf dieser Seite aus.
B. Wenn Sie auf der Plug-and-Charge-Seite auf die Einstellungen klicken, wird der Befehl geschrieben und dann der Monitor eingeschaltet on. Nachdem Sie den entsprechenden Rückgabewert erhalten haben, kehren Sie zur Detailseite zurück und entladen Sie ihn in die Lebenszyklusfunktionen onHide und onUnload. Abhörcode
: Springen Sie zur Plug-and-Play-Seite

			/*跳转即插即用设置*/
			goPlusAndPlay() {
    
    
				if (this.contactFlag == false) {
    
    
					uni.showToast({
    
    
						title: '请先连接蓝牙'
					})
					return false;
				}
				if (this.currentPile.pileStatusName == '充电中') {
    
    
					uni.showToast({
    
    
						title: '充电中无法设置即插即充'
					})
					return false;
				}
				if (this.currentPile.pileStatus == '03' || this.currentPile.pileStatus == '06') {
    
    
					uni.showToast({
    
    
						title: '请先拔枪后再设置即插即充'
					})
					return false;
				}
				if (this.currentPile.noninductCode == true) {
    
    
					uni.showToast({
    
    
						title: '桩开启蓝牙无感,不能设置即插即充'
					})
					return false;
				}
				if (this.currentPile.reservation == '01' || this.currentPile.reservation == '03') {
    
    
					uni.showToast({
    
    
						title: '预约充电中无法设置'
					})
					return false;
				}
				// this.listenValueChange()
				my.offBLECharacteristicValueChange(this.listener);
				my.navigateTo({
    
    
					url: '/pages/bluetoothPlug/index?plugAndPlay=' + this.currentPile.plugAndPlay + '&scanObj=' +
						encodeURIComponent(JSON
							.stringify(this
								.scanObj))
				})
			},

Code: Plug-and-Play-Seitencode

<template>
	<view style="height:100%;padding-top: 40rpx;">
		<!-- 自定义导航栏 -->
		<!-- <u-navbar back-text="即插即充设置"></u-navbar> -->
		<view class="titleHead">即插即充</view>
		<view class="content">
			<view class="card">
				<view>
					<view class="card-title">
						<view>即插即充</view>
						<view>插枪充电</view>
					</view>
					<view class="card-swit">
						<!-- 	<u-switch v-model="checked" size="40">
						</u-switch> -->
						<u-switch v-model="checked"></u-switch>
					</view>
				</view>
				<view class="tips">
					注意:此模式在开启充电或无感充电时不生效
				</view>
			</view>
			<view class="btn">
				<my-btn @click="save">保存设置</my-btn>
			</view>
		</view>
		<animateLoading v-if="loadingFlag"></animateLoading>
	</view>
</template>

<script>
	// import tabbar from '../../component/tabbar.vue'
	import myBtn from '../../component/my-button.vue';
	import animateLoading from '../../component/animateLoading.vue';
	import blueteethCmd from '../../lib/blueteeth-cmd.js';
	import utils from '../../lib/utils.js';
	export default {
    
    
		components: {
    
    
			myBtn,
			animateLoading
		},
		data() {
    
    
			return {
    
    
				checked: false,
				loadingFlag: false,
				noninductCode: false,
				scanObj: {
    
    },
			}
		},
		onLoad(options) {
    
    
			console.log('进入了蓝牙即插即用设置页面')
			this.checked = JSON.parse(options.plugAndPlay)
			console.log('plugAndPlay=' + options.plugAndPlay)
			this.scanObj = JSON.parse(decodeURIComponent(options.scanObj))
			console.log(options)
			console.log(options.plugAndPlay)
			console.log('进入了蓝牙即插即用设置页面')

		},
		onReady() {
    
    

		},
		onShow() {
    
    

		},
		onHide() {
    
    
			my.offBLECharacteristicValueChange()
		},
		onUnload() {
    
    
			my.offBLECharacteristicValueChange()
		},
		computed: {
    
    
			counts() {
    
    
				return this.$store.state.count
			}
		},
		methods: {
    
    
			/*保存设置*/
			save() {
    
    
				console.log('开始设置即插即用')
				let chargeMode = '00'
				let cmdCode = ''
				if (this.checked) {
    
    
					chargeMode = '01'
					cmdCode = blueteethCmd.gen0047CmdPlug(chargeMode);
				} else {
    
    
					cmdCode = blueteethCmd.gen0047CmdPlug();
				}
				console.log(cmdCode)
				console.log('开始设置即插即用')
				this.writeDataToEquip(cmdCode)
				this.listenValueChange()
			},
			/*监听蓝牙值的变化*/
			listenValueChange() {
    
    
				let that = this;
				console.log('监听到桩传回来值')
				that.contactFlag = true;
				my.onBLECharacteristicValueChange(res => {
    
    
					let upWord = res.value.toUpperCase()
					that.handleEquipNotifyBackCode(upWord);
					console.log('xxxxxxxxx')
					console.log(upWord)
					console.log('xxxxxxxxx')
				})
			},
			/*处理桩返回的信号*/
			handleEquipNotifyBackCode(notifyCode) {
    
    
				console.log('返回指令')
				console.log(notifyCode)

				console.log('返回指令')
				let _ = this;
				let startIndex = notifyCode.indexOf('7E');
				let endIndex = notifyCode.lastIndexOf('7E');
				if (startIndex >= 0 && endIndex > 0) {
    
    
					const cmdCodeType = notifyCode.substring(startIndex + 2, startIndex + 6);
					if ('8047' == cmdCodeType) {
    
     //即插即用设置返回
						console.log('cmdCodeType=' + cmdCodeType)
						my.navigateBack(); // 返回上一页
						let msgBody = notifyCode.substring(26, notifyCode.length - 4);
						let res = msgBody.substring(4, 6)
						console.log('res=' + res)
						if (res == '00') {
    
     //'00'成功
							console.log('即插即用设置成功')

						} else if (res == '01') {
    
     //'01'失败
							console.log('即插即用设置失败')

						}
					}
				}
			},
			/*往桩写入数据方法*/
			writeDataToEquip(msg) {
    
    
				let _ = this;

				/*16进制字符串转字节流数组发送桩*/
				function hexStringToArrayBuffer(str) {
    
    
					if (!str) {
    
    
						return new ArrayBuffer(0);
					}
					var buffer = new ArrayBuffer(str.length / 2);
					let dataView = new DataView(buffer)

					let ind = 0;
					for (var i = 0, len = str.length / 2; i < len; i += 1) {
    
    
						let code = parseInt(str.substr(2 * i, 2), 16)

						dataView.setUint8(ind, code)
						ind++
					}
					return buffer;
				}

				const bufferNew = hexStringToArrayBuffer(msg)

				my.writeBLECharacteristicValue({
    
    
					deviceId: _.scanObj.deviceId, // 设备ID,在【4】里获取到
					serviceId: _.scanObj.serviceId, // 服务UUID,在【6】里能获取到
					characteristicId: _.scanObj.writeCharacteristicId, // 特征值,在【7】里能获取到
					value: bufferNew,
					success(res) {
    
    
						console.log('即插即用指令写入')
						console.log('写入指令成功===' + msg)
						console.log('即插即用指令写入')
						// my.navigateBack();
					},
					fail(err) {
    
    
						console.error(err)
						utils.errMsg('写入失败,请重新连接');
						// 断开连接,重新连接
						_.closeDeviceConnect();
					}
				})
			},
			/*停止蓝牙连接*/
			closeDeviceConnect() {
    
    
				let that = this;
				my.disconnectBLEDevice({
    
    
					deviceId: that.scanObj.deviceId,
					success: (res) => {
    
    
						console.log(res, "连接情况");
					},
					fail: (error) => {
    
    
						console.log(error, "断开情况");
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.titleHead {
    
    
		width: 232rpx;
		height: 66rpx;
		font-size: 48rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #2D3748;
		line-height: 66rpx;
		margin: 0rpx 50rpx 0;
	}

	.content {
    
    
		box-sizing: border-box;
		padding: 0 50rpx 5rpx 50rpx;
		display: flex;
		height: 100%;
		flex-direction: column;

		.card {
    
    
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: center;
			width: 100%;
			margin-top: 50rpx;
			font-size: 28rpx;

			.card-title {
    
    
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				>view {
    
    
					&:nth-child(1) {
    
    
						width: 100%;
						font-size: 28rpx;
						font-family: PingFangSC-Semibold, PingFang SC;
						font-weight: 800;
						color: #2D3748;
					}

					&:nth-child(2) {
    
    
						width: 80rpx;
						font-size: 20rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #1677FF;
					}
				}
			}

			.card-swit {
    
    
				height: 100%;
				display: flex;
				flex-direction: column;
				justify-content: flex-start;
			}

			>view {
    
    
				&:nth-child(1) {
    
    
					display: flex;
					padding: 40rpx 40rpx;
					width: 100%;
					justify-content: space-between;
					box-shadow: 0rpx 0rpx 30rpx 14rpx rgba(198, 189, 189, 0.13);
					align-items: center;
					height: 164rpx;
					background: #FFFFFF;
					border-radius: 20rpx;
					font-family: PingFangSC-Regular, PingFang SC;
				}

				&:nth-child(2) {
    
    
					width: 100%;
					color: #1677FF;
					text-align: center;
					font-size: 24rpx;
					margin-top: 50rpx;
					font-family: PingFangSC-Regular, PingFang SC;
				}
			}
		}

		.btn {
    
    
			width: 630rpx;
			margin: 904rpx auto 0rpx;
		}
	}
</style>

Ich denke du magst

Origin blog.csdn.net/weixin_44705979/article/details/131594624
Empfohlen
Rangfolge