1 line
13 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{"version":3,"sources":["pages/index/index.uvue","App.uvue"],"sourcesContent":["<template>\n <view class=\"container\">\n <view class=\"title-bar\">\n <text class=\"title\">连接设备</text>\n <button \n class=\"scan-btn\" \n :disabled=\"isScanning\" \n @click=\"toggleScan\"\n >\n {{ isScanning ? '停止扫描' : '开始扫描' }}\n </button>\n </view>\n <view class=\"status-tip\" v-if=\"isScanning\">\n <text>正在扫描设备...</text>\n <view class=\"loading\"></view>\n </view>\n <view class=\"status-tip\" v-else-if=\"foundDevices.length === 0\">\n <text>未发现设备,请点击\"开始扫描\"</text>\n </view>\n <view class=\"device-list\">\n <view \n class=\"device-item\" \n v-for=\"(device, index) in foundDevices\" \n :key=\"device.deviceId\"\n @click=\"connectDevice(device)\"\n >\n <!-- 设备名称 -->\n <view class=\"device-name\">\n <text>{{ device.name || '未知设备' }}</text>\r\n\t\t <view class=\"is_bj\" v-if=\"device.is_bj\" >吧唧</view>\n <text class=\"device-id\">{{ device.deviceId }}</text>\r\n\t\t \n </view>\n \n <!-- 信号强度 -->\n <view class=\"device-rssi\">\n <text>信号: {{ device.rssi }} dBm</text>\n <view class=\"rssi-bar\" :style=\"{ width: getRssiWidth(device.rssi) }\"></view>\n </view>\n \n <!-- 连接状态 -->\n <view class=\"device-status\" v-if=\"device.connected\">\n <text class=\"connected\">已连接</text>\n </view>\n </view>\n </view>\n </view>\n</template>\n\n<script>\nexport default {\n data() {\n return {\n foundDevices: [],\n isScanning: false,\n bluetoothEnabled: false, \n connectedDeviceId: ''\n };\n },\n\n onLoad() {\n // 页面加载时初始化蓝牙\n this.initBluetooth();\n },\n onUnload() {\n // 页面卸载时清理资源\n this.stopScan();\n uni.closeBluetoothAdapter();\n // 移除事件监听\n uni.offBluetoothDeviceFound(this.onDeviceFound);\n },\n\n methods: {\n // 初始化蓝牙适配器\n initBluetooth() {\n uni.openBluetoothAdapter({\n success: () => {\n this.bluetoothEnabled = true;\n // uni.showToast({ title: '蓝牙初始化成功', icon: 'none' });\n },\n fail: (err) => {\n this.bluetoothEnabled = false;\n uni.showModal({\n title: '蓝牙开启失败',\n content: '请检查设备蓝牙是否开启',\n showCancel: false\n });\n console.error('蓝牙初始化失败:', err);\n }\n });\n },\n\n // 切换扫描状态(开始/停止)\n toggleScan() {\n if (!this.bluetoothEnabled) {\n this.initBluetooth();\n return;\n }\n\n if (this.isScanning) {\n this.stopScan();\n } else {\n this.startScan();\n }\n },\n\n // 开始扫描设备\n startScan() {\n this.isScanning = true;\n this.foundDevices = []; // 清空历史列表\n\n // 开始扫描空services表示扫描所有设备\n uni.startBluetoothDevicesDiscovery({\n services: [],\n allowDuplicatesKey: false, // 不允许重复上报\n success: () => {\n uni.showToast({ title: '开始扫描', icon: 'none' });\n // 注册设备发现事件\n uni.onBluetoothDeviceFound(this.onDeviceFound);\n },\n fail: (err) => {\n this.isScanning = false;\n uni.showToast({ title: '扫描失败', icon: 'none' });\n console.error('扫描失败:', err);\n }\n });\n\n // 10秒后自动停止扫描避免耗电\n setTimeout(() => {\n if (this.isScanning) this.stopScan();\n }, 5000);\n },\n\n // 停止扫描\n stopScan() {\n if (!this.isScanning) return;\n\n uni.stopBluetoothDevicesDiscovery({\n success: () => {\n this.isScanning = false;\r\n\t\t if(this.foundDevices.length == 0){\r\n\t\t\t uni.showToast({\r\n\t\t\t title: `暂未扫描到任何设备`, \r\n\t\t\t icon: 'none' \r\n\t\t\t });\r\n\t\t\t return;\r\n\t\t }\n uni.showToast({ \n title: `扫描完成,发现${this.foundDevices.length}台设备`, \n icon: 'none' \n });\n },\n fail: (err) => {\n console.error('停止扫描失败:', err);\n }\n });\n },\n\n // 设备发现回调(处理去重和数据格式化)\n onDeviceFound(res) {\n\t\tconst devices = res.devices || [];\n\t\tdevices.forEach(device => {\r\n\t\t\tvar that = this\n\t\t\tif (!device.deviceId) return;\n\t\t\tconst isExist = this.foundDevices.some(\n\t\t\t\td => d.deviceId === device.deviceId\n\t\t\t);\n\t\t\tif (isExist) return;\r\n\t\t\tvar is_bj = false;\r\n\t\t\tconst advData = new Uint8Array(device.advertisData);\r\n\t\t\t// const companyidData = (advData[0]<<8)| advData[1];\r\n\t\t\tconst devicenameData = advData.slice(2, 6);\r\n\t\t\tconst devicename = String.fromCharCode(...devicenameData)\r\n\t\t\tif(devicename == 'dzbj') is_bj = true;\n\t\t\tthis.foundDevices.push({\r\n\t\t\t\tis_bj: is_bj,\n\t\t\t\tname: device.name || '未知设备',\n\t\t\t\tdeviceId: device.deviceId,\n\t\t\t\trssi: device.RSSI,\n\t\t\t\tconnected: device.deviceId === this.connectedDeviceId\n\t\t\t});\n\t\t});\n },\n\n // 计算信号强度条宽度(-100dBm为0%-30dBm为100%\n getRssiWidth(rssi) {\n const minRssi = -100;\n const maxRssi = -30;\n let ratio = (rssi - minRssi) / (maxRssi - minRssi);\n ratio = Math.max(0, Math.min(1, ratio)); // 限制在0-1之间\n return `${ratio * 100}%`;\n },\n\n connectDevice(device) {\r\n\t\tvar that = this\r\n\t\tthis.stopScan();\n\t\tuni.showLoading({ title: '连接中...' });\n\t\tuni.createBLEConnection({\r\n\t\t\tdeviceId:device.deviceId,\r\n\t\t\tsuccess(res) {\r\n\t\t\t\tthat.foundDevices = that.foundDevices.map(d => ({\r\n\t\t\t\t\t...d,\r\n\t\t\t\t\tconnected: d.deviceId === device.deviceId\r\n\t\t\t\t}));\r\n\t\t\t\tuni.hideLoading();\r\n\t\t\t\tuni.showToast({ title: `已连接${device.name}`, icon: 'none' });\r\n\t\t\t\tuni.navigateTo({\r\n\t\t\t\t\turl:'../connect/connect?deviceId='+device.deviceId,\r\n\t\t\t\t\tfail: (err) => {\r\n\t\t\t\t\t uni.showToast({ title: `连接失败,请稍后重试`, icon: 'error' });\r\n\t\t\t\t\t\tuni.closeBLEConnection({\r\n\t\t\t\t\t\t\tdeviceId:device.deviceId,\r\n\t\t\t\t\t\t\tsuccess() {\r\n\t\t\t\t\t\t\t\tconsole('断开连接成功')\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t})\r\n\t\t\t\t\t}\r\n\t\t\t\t})\r\n\t\t\t},\r\n\t\t\tfail() {\r\n\t\t\t\tuni.hideLoading();\r\n\t\t\t\tuni.showToast({ title: `连接失败`, icon: 'error' });\r\n\t\t\t}\r\n\t\t})\n }\n }\n};\n</script>\n\n<style scoped>\n.container {\n padding: 16rpx;\n background-color: #f5f5f5;\n /* min-height: 100vh; */\n}\n\n/* 标题栏 */\n.title-bar {\n display: flex;\n justify-content: space-between;\n align-items: center;\n padding: 20rpx 0;\n border-bottom: 1px solid #eee;\n margin-bottom: 20rpx;\r\n margin-top: 80rpx;\r\n \n}\n\n.title {\n\tfont-size: 36rpx;\n\tfont-weight: bold;\n\tcolor: #333;\n}\n\n.scan-btn {\n\tbackground-color: #00c900;\n\tcolor: white;\n\tpadding: 0rpx 24rpx;\n\tborder-radius: 8rpx;\n\tfont-size: 28rpx;\r\n\tmargin-right: 5%;\n}\n\n.scan-btn:disabled {\n background-color: #ccc;\n}\n\n/* 状态提示 */\n.status-tip {\n text-align: center;\n padding: 40rpx 0;\n color: #666;\n font-size: 28rpx;\n}\n\n.loading {\n width: 30rpx;\n height: 30rpx;\n border: 3rpx solid #007aff;\n border-top-color: transparent;\n border-radius: 50%;\n margin: 20rpx auto;\n animation: spin 1s linear infinite;\n}\n\n@keyframes spin {\n to { transform: rotate(360deg); }\n}\n\n/* 设备列表 */\n.device-list {\n display: flex;\n flex-direction: column;\n gap: 16rpx;\n}\n\n.device-item {\n background-color: white;\n border-radius: 12rpx;\n padding: 24rpx;\n box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);\n cursor: pointer;\n}\n\n.device-name {\n display: flex;\n justify-content: space-between;\n margin-bottom: 16rpx;\n}\n\n.device-name text:first-child {\n font-size: 32rpx;\n color: #333;\n}\n\r\n.is_bj{\r\n\tpadding: 3px 8px;\r\n\tborder-radius: 5px;\r\n\tcolor: white;\r\n\tbackground-color: rgba(30, 228, 156, 0.4);\r\n\tposition: relative;\r\n\tright: 30px;\r\n}\r\n\n.device-id {\n font-size: 24rpx;\n color: #999;\n}\n\n.device-rssi {\n display: flex;\n flex-direction: column;\n gap: 8rpx;\n}\n\n.device-rssi text {\n font-size: 26rpx;\n color: #666;\n}\n\n.rssi-bar {\n height: 8rpx;\n background-color: #eee;\n border-radius: 4rpx;\n overflow: hidden;\n}\n\n.rssi-bar::after {\n content: '';\n display: block;\n height: 100%;\n background: linear-gradient(to right, #ff4d4f, #faad14, #52c41a);\n}\n\n.device-status {\n margin-top: 16rpx;\n text-align: right;\n}\n\n.connected {\n font-size: 26rpx;\n color: #07c160;\n background-color: #f0fff4;\n padding: 4rpx 16rpx;\n border-radius: 12rpx;\n}\n</style>",null],"names":[],"mappings":";;;;;;;;;;;;+BAkNU;+BA/IF;+BAkIF;+BAOE;+BAEA;+BAzIA;+BAiDM;+BA3CJ;+BAyHJ;+BAlHQ;+BAjBV;+BA+CM;+BAyBA;AAvFL;;eAUH,sBAAM;YAEJ,IAAI,CAAC,aAAa;QACpB;;iBACA,MAAQ;YAEN,IAAI,CAAC,QAAQ;YACb;YAEA,4BAA4B,IAAI,CAAC,aAAa;QAChD;;;;;;;eArEA,IA6CO,QAAA,IA7CD,WAAM,cAAW;YACrB,IASO,QAAA,IATD,WAAM,cAAW;gBACrB,IAA+B,QAAA,IAAzB,WAAM,UAAQ;gBACpB,IAMS,UAAA,IALP,WAAM,YACL,cAAU,KAAA,UAAU,EACpB,aAAO,KAAA,UAAU,OAEf,IAAA,KAAA,UAAU;oBAAA;;oBAAA;;gBAAA,GAAA,CAAA,EAAA;oBAAA;oBAAA;iBAAA;;uBAGc,KAAA,UAAU;gBAAzC,IAGO,QAAA,gBAHD,WAAM;oBACV,IAAsB,QAAA,IAAA,EAAhB;oBACN,IAA6B,QAAA,IAAvB,WAAM;;;gBAEsB,IAAA,KA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