From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期三, 28 五月 2025 16:48:52 +0800
Subject: [PATCH] 初始化项目

---
 pages/util/bleConnect.vue |  376 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 376 insertions(+), 0 deletions(-)

diff --git a/pages/util/bleConnect.vue b/pages/util/bleConnect.vue
new file mode 100644
index 0000000..a12812b
--- /dev/null
+++ b/pages/util/bleConnect.vue
@@ -0,0 +1,376 @@
+<template>
+	<view class="content">
+		
+		<button class="btn" type="primary" :loading="isSearching" @tap="startSearch">寮�濮嬫悳绱� </button>
+		<button class="btn" type="warn" @tap="stopSearch">鍋滄鎼滅储</button>
+		
+		
+		<view v-for="(item) in list" :data-title="item.deviceId" :data-name="item.name" :data-advertisData="item.advertisServiceUUIDs"
+		 :key="item.deviceId" @tap="bindViewTap">
+			<view class="item">
+				<view class="deviceId block">{{item.deviceId}}</view>
+				<view class="name block">{{item.name}}</view>
+			</view>
+		</view>
+	</view>
+</template>
+
+<script>
+	import {
+		mapState
+	} from 'vuex';
+	export default {
+		data() {
+			return {
+				isSearching: false, //鏄惁姝e湪鎼滅储涓�
+				list: [],
+				services: [],
+				serviceId: 0,
+				writeCharacter: false,
+				readCharacter: false,
+				notifyCharacter: false,
+				printData:null
+			};
+		},
+		computed: mapState(['sysinfo', 'Bluetooth']),
+		onLoad(params) {
+			if (params.data) {
+				this.printData =JSON.parse(params.data);
+			}
+		},
+		onUnload() {
+			//鍋滄鎼滅储钃濈墮璁惧
+			if (this.isSearching) {
+				uni.stopBluetoothDevicesDiscovery();
+			}
+		},
+		methods: {
+			//閿欒鐮佹彁绀�
+			errorCodeTip(code) {
+				if (code == 0) {
+					//姝e父
+				} else if (code == 10000) {
+					uni.showToast({
+						title: '鏈垵濮嬪寲钃濈墮閫傞厤鍣�',
+						icon: 'none'
+					})
+				} else if (code == 10001) {
+					uni.showToast({
+						title: '褰撳墠钃濈墮閫傞厤鍣ㄤ笉鍙敤',
+						icon: 'none'
+					})
+				} else if (code == 10002) {
+					uni.showToast({
+						title: '娌℃湁鎵惧埌鎸囧畾璁惧',
+						icon: 'none'
+					})
+				} else if (code == 10003) {
+					uni.showToast({
+						title: '杩炴帴澶辫触',
+						icon: 'none'
+					})
+				} else if (code == 10004) {
+					uni.showToast({
+						title: '娌℃湁鎵惧埌鎸囧畾鏈嶅姟',
+						icon: 'none'
+					})
+				} else if (code == 10005) {
+					uni.showToast({
+						title: '娌℃湁鎵惧埌鎸囧畾鐗瑰緛鍊�',
+						icon: 'none'
+					})
+				} else if (code == 10006) {
+					uni.showToast({
+						title: '褰撳墠杩炴帴宸叉柇寮�',
+						icon: 'none'
+					})
+				} else if (code == 10007) {
+					uni.showToast({
+						title: '褰撳墠鐗瑰緛鍊间笉鏀寔姝ゆ搷浣�',
+						icon: 'none'
+					})
+				} else if (code == 10008) {
+					uni.showToast({
+						title: '鍏朵綑鎵�鏈夌郴缁熶笂鎶ョ殑寮傚父',
+						icon: 'none'
+					})
+				} else if (code == 10009) {
+					uni.showToast({
+						title: 'Android 绯荤粺鐗规湁锛岀郴缁熺増鏈綆浜� 4.3 涓嶆敮鎸� BLE',
+						icon: 'none'
+					})
+				}
+			},
+			//寮�濮嬫悳绱㈣摑鐗�
+			startSearch() {
+				let that = this
+				uni.openBluetoothAdapter({
+					success(res) {
+						uni.getBluetoothAdapterState({
+							success(res2) {
+								console.log('getBluetoothAdapterState:', res2)
+								if (res2.available) {
+									that.isSearching = true;
+									if (res2.discovering) {
+										uni.showToast({
+											title: '姝e湪鎼滅储闄勮繎鎵撳嵃鏈鸿澶�',
+											icon: "none"
+										})
+										return;
+									}
+			
+									//鑾峰彇钃濈墮璁惧淇℃伅
+									that.getBluetoothDevices()
+			
+									// that.checkPemission()
+								} else {
+									uni.showModal({
+										title: '鎻愮ず',
+										content: '鏈満钃濈墮涓嶅彲鐢�',
+									})
+								}
+							}
+						});
+					},
+					fail() {
+						uni.showModal({
+							title: '鎻愮ず',
+							content: '钃濈墮鍒濆鍖栧け璐ワ紝璇锋墦寮�钃濈墮',
+						})
+					}
+				})
+			},
+			stopSearch() {
+				uni.stopBluetoothDevicesDiscovery({
+					success: (res) => {
+						this.isSearching = false;
+						console.log('stop:', res)
+					},
+					fail: (e) => {
+						console.log('stop:', e)
+						this.errorCodeTip(e.errCode);
+					}
+				})
+			},
+			//鏍¢獙鏉冮檺
+			checkPemission() {
+				let that = this
+				let {
+					BLEInformation
+				} = that.Bluetooth;
+				let platform = BLEInformation.platform;
+				that.getBluetoothDevices();
+			},
+			//鑾峰彇钃濈墮璁惧淇℃伅
+			getBluetoothDevices() {
+				let that = this
+				that.list = [];
+				uni.startBluetoothDevicesDiscovery({
+					success(res) {
+						// console.log(res)
+						//钃濈墮璁惧鐩戝惉 uni.onBluetoothDeviceFound
+						plus.bluetooth.onBluetoothDeviceFound((result) => {
+							console.log('onBluetoothDeviceFound:', result)
+							let arr = that.list;
+							let devices = [];
+							let list = result.devices;
+							for (let i = 0; i < list.length; ++i) {
+								if (list[i].name && list[i].name != "鏈煡璁惧") {
+									let arrNew = arr.filter((item) => {
+										return item.deviceId == list[i].deviceId;
+									});
+									// console.log('arrNew:',arrNew.length)
+									if (arrNew.length == 0) {
+										devices.push(list[i]);
+									}
+								}
+							}
+			
+							that.list = arr.concat(devices);
+						});
+						that.time = setTimeout(() => {
+							// uni.getBluetoothDevices
+							plus.bluetooth.getBluetoothDevices({
+								success(res2) {
+									let devices = [];
+									let list = res2.devices;
+									for (let i = 0; i < list.length; ++i) {
+										if (list[i].name && list[i].name != "鏈煡璁惧") {
+											devices.push(list[i]);
+										}
+									}
+			
+									that.list = devices;
+									console.log('getBluetoothDevices:',res2);
+								},
+							})
+			
+							clearTimeout(that.time);
+						}, 3000);
+					}
+				});
+			
+			},
+			//缁戝畾钃濈墮
+			bindViewTap(e) {
+				let that = this;
+				let {
+					title
+				} = e.currentTarget.dataset;
+				let {
+					BLEInformation
+				} = that.Bluetooth;
+				this.stopSearch();
+				
+				that.serviceId = 0;
+				that.writeCharacter = false;
+				that.readCharacter = false;
+				that.notifyCharacter = false;
+				uni.showLoading({
+					title: '姝e湪杩炴帴',
+				})
+				console.log('deviceId:', title)
+				// uni.createBLEConnection
+				plus.bluetooth.createBLEConnection({
+					deviceId: title,
+					success(res) {
+						console.log('createBLEConnection success:', res)
+						BLEInformation.deviceId = title;
+						that.$store.commit('BLEInformationSet', BLEInformation);
+						uni.hideLoading()
+						that.getSeviceId()
+					},
+					fail(e) {
+						that.errorCodeTip(e.errCode);
+						uni.hideLoading()
+					}
+				})
+			},
+			//鑾峰彇钃濈墮璁惧鎵�鏈夋湇鍔�(service)銆�
+			getSeviceId() {
+				let that = this;
+				let {
+					BLEInformation
+				} = that.Bluetooth;
+				console.log('BLEInformation.deviceId:',BLEInformation.deviceId)
+				// uni.getBLEDeviceServices
+				let t=setTimeout(()=>{
+					plus.bluetooth.getBLEDeviceServices({
+						deviceId: BLEInformation.deviceId,
+						success(res) {
+							console.log('getBLEDeviceServices success:',res)
+							that.services = res.services;
+							that.getCharacteristics()
+						},
+						fail: function(e) {
+							that.errorCodeTip(e.code);	
+							console.log('getBLEDeviceServices fail:',e)
+						}
+					})
+					clearTimeout(t);
+				},1500)
+			},
+			getCharacteristics() {
+				var that = this
+				let {
+					services: list,
+					serviceId: num,
+					writeCharacter: write,
+					readCharacter: read,
+					notifyCharacter: notify
+				} = that;
+				let {
+					BLEInformation
+				} = that.Bluetooth;
+				// uni.getBLEDeviceCharacteristics
+				plus.bluetooth.getBLEDeviceCharacteristics({
+					deviceId: BLEInformation.deviceId,
+					serviceId: list[num].uuid,
+					success(res) {
+						// console.log(res)
+						for (var i = 0; i < res.characteristics.length; ++i) {
+							var properties = res.characteristics[i].properties
+							var item = res.characteristics[i].uuid
+							if (!notify) {
+								if (properties.notify) {
+									BLEInformation.notifyCharaterId = item;
+									BLEInformation.notifyServiceId = list[num].uuid;
+									that.$store.commit('BLEInformationSet', BLEInformation);
+									notify = true
+								}
+							}
+							if (!write) {
+								if (properties.write) {
+									BLEInformation.writeCharaterId = item;
+									BLEInformation.writeServiceId = list[num].uuid;
+									that.$store.commit('BLEInformationSet', BLEInformation);
+									write = true
+								}
+							}
+							if (!read) {
+								if (properties.read) {
+									BLEInformation.readCharaterId = item;
+									BLEInformation.readServiceId = list[num].uuid;
+									that.$store.commit('BLEInformationSet', BLEInformation);
+									read = true
+								}
+							}
+						}
+						if (!write || !notify || !read) {
+							num++
+							that.writeCharacter = write;
+							that.readCharacter = read;
+							that.notifyCharacter = notify;
+							that.serviceId = num;
+							if (num == list.length) {
+								uni.showModal({
+									title: '鎻愮ず',
+									content: '鎵句笉鍒拌璇诲啓鐨勭壒寰佸��',
+								})
+							} else {
+								that.getCharacteristics()
+							}
+						} else {
+							that.openControl()
+						}
+					},
+					fail: function(e) {
+						console.log("getBLEDeviceCharacteristics fail锛�",e);
+						that.errorCodeTip(e.errCode);	
+					}
+				})
+			},
+			openControl: function() {
+				uni.navigateTo({
+					url: '/pages/product/outputRegister/printProductOutLabel?data='+ JSON.stringify(this.printData),
+				})
+			},
+		}
+	}
+</script>
+
+<style lang="less">
+	.btn {
+		margin-top: 50rpx;
+		height: 40px;
+		width: 600rpx;
+		line-height: 40px;
+	}
+
+	.item {
+		display: block;
+		font-family: Arial, Helvetica, sans-serif;
+		font-size: 14px;
+		margin: 0 30px;
+		margin-top: 10px;
+		background-color: #FFA850;
+		border-radius: 5px;
+		border-bottom: 2px solid #68BAEA;
+	}
+
+	.block {
+		display: block;
+		color: #ffffff;
+		padding: 5px;
+	}
+</style>

--
Gitblit v1.9.3