diff --git a/src/core/store/modules/bluetoothScale.js b/src/core/store/modules/bluetoothScale.js index f3200aa..2c515bb 100644 --- a/src/core/store/modules/bluetoothScale.js +++ b/src/core/store/modules/bluetoothScale.js @@ -37,6 +37,30 @@ function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } +// ---------------- 历史连接设备持久化 ---------------- +const HISTORY_KEY = 'WMS_BT_SCALE_HISTORY'; + +function loadHistoryDevices() { + try { + const list = uni.getStorageSync(HISTORY_KEY); + return Array.isArray(list) ? list : []; + } catch (e) { + return []; + } +} + +function saveHistoryDevice(device) { + if (!device || !device.deviceId) return; + try { + const list = loadHistoryDevices().filter(d => d && d.deviceId !== device.deviceId); + list.unshift({ deviceId: device.deviceId, name: device.name || '' }); + if (list.length > 20) list.length = 20; + uni.setStorageSync(HISTORY_KEY, list); + } catch (e) { + // ignore + } +} + function normalizeBluetoothError(error) { const raw = error && (error.message || error.errMsg || String(error)); const text = String(raw || '').trim(); @@ -92,6 +116,8 @@ let reconnectTimer = null; let disconnectDebounceTimer = null; let scanStopTimer = null; let manualDisconnect = false; +// 连接互斥锁:避免手动连接与自动重连并发执行 +let connectLock = false; const SCAN_TIMEOUT_MS = 12000; @@ -267,6 +293,27 @@ function parseIncoming(commit, state, res) { } } +// 发现可订阅(notify/indicate)的重量特征值,返回 { service, characteristic, error } +async function discoverNotifyCharacteristic(deviceId) { + const [srvErr, srvRes] = await uni.getBLEDeviceServices({ deviceId }); + if (srvErr || !srvRes || !srvRes.services || !srvRes.services.length) { + return { service: null, characteristic: null, error: new Error('未发现设备服务') }; + } + for (const service of srvRes.services) { + const [chErr, chRes] = await uni.getBLEDeviceCharacteristics({ + deviceId, + serviceId: service.uuid + }); + if (chErr || !chRes || !chRes.characteristics) continue; + for (const ch of chRes.characteristics) { + if (ch.properties && (ch.properties.notify || ch.properties.indicate)) { + return { service, characteristic: ch, error: null }; + } + } + } + return { service: null, characteristic: null, error: new Error('未找到可订阅的重量数据特征') }; +} + const actions = { /** * 初始化蓝牙适配器并注册全局监听 @@ -365,6 +412,11 @@ const actions = { */ async startScan({ commit, dispatch }) { commit('RESET_DEVICES'); + // 已配对/已连接设备不会出现在 discovery 结果中,先并入列表; + // 并加载历史连接过的设备,避免切换设备后扫描找不到之前连接过的设备名称 + const bonded = await dispatch('getBondedDevices'); + (Array.isArray(bonded) ? bonded : []).forEach(d => commit('ADD_DEVICE', d)); + loadHistoryDevices().forEach(d => commit('ADD_DEVICE', d)); const [err] = await uni.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false }); commit('DISCOVERING', true); if (err) { @@ -428,7 +480,7 @@ const actions = { commit('SET_CONNECTION_STATUS', 'scanning'); await dispatch('startScan'); - const timeout = 15000; + const timeout = SCAN_TIMEOUT_MS; const startAt = Date.now(); try { while (Date.now() - startAt < timeout) { @@ -458,6 +510,12 @@ const actions = { return; } + if (connectLock) { + commit('ADD_LOG', { type: 'info', message: '正在连接设备,跳过本次自动重连' }); + return; + } + connectLock = true; + commit('SET_RECONNECTING', true); commit('SET_RECONNECT_MESSAGE', '正在恢复蓝牙连接...'); commit('SET_CONNECTION_STATUS', 'connecting'); @@ -485,15 +543,27 @@ const actions = { throw connErr; } - if (state.currentDevice.serviceId && state.currentDevice.characteristicId) { - const [notifyErr] = await uni.notifyBLECharacteristicValueChange({ - deviceId: state.currentDevice.id, - serviceId: state.currentDevice.serviceId, - characteristicId: state.currentDevice.characteristicId, - state: true - }); - if (notifyErr) throw notifyErr; - } + // 连接建立后部分安卓机型服务可用存在延时,短暂等待再重新发现服务 + await delay(400); + + // 重连后重新发现服务/特征,避免直接复用旧特征实例导致 notify 失败 + const discovered = await discoverNotifyCharacteristic(state.currentDevice.id); + if (discovered.error) throw discovered.error; + + const [notifyErr] = await uni.notifyBLECharacteristicValueChange({ + deviceId: state.currentDevice.id, + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid, + state: true + }); + if (notifyErr) throw notifyErr; + + commit('SET_CURRENT_DEVICE', { + id: state.currentDevice.id, + name: state.currentDevice.name || '未知设备', + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid + }); clearReconnectTimer(); commit('SET_RECONNECT_ATTEMPTS', 0); @@ -516,6 +586,8 @@ const actions = { }); } throw e; + } finally { + connectLock = false; } }, @@ -525,14 +597,30 @@ const actions = { commit('ADD_LOG', { type: 'info', message: `设备已连接,继续读取重量: ${device.name || device.deviceId}` }); return true; } - if (manualDisconnect) { - commit('ADD_LOG', { type: 'info', message: `已手动断开,忽略重复连接: ${device.name || device.deviceId}` }); - return false; + if (connectLock) { + commit('ADD_LOG', { type: 'info', message: '正在连接其他设备,请稍候' }); + throw new Error('正在连接中,请稍候'); } + connectLock = true; clearReconnectTimer(); clearDisconnectDebounceTimer(); manualDisconnect = false; + + // 单连接:切换设备前先显式断开旧设备,避免旧设备残留连接; + // 断开期间置 manualDisconnect=true,阻止全局监听对旧设备触发自动重连 + const prev = state.currentDevice; + const prevId = prev && prev.id; + if (prevId && prevId !== device.deviceId) { + manualDisconnect = true; + try { + await uni.closeBLEConnection({ deviceId: prevId }); + } catch (e) { + // ignore + } + commit('ADD_LOG', { type: 'info', message: `已断开旧设备: ${prev.name || prevId}` }); + } + commit('SET_CONNECTION_STATUS', 'connecting'); commit('ADD_LOG', { type: 'info', message: `正在连接: ${device.name || device.deviceId}` }); @@ -541,61 +629,48 @@ const actions = { deviceId: device.deviceId, timeout: 10000 }); - if (connErr) { - const errText = String(connErr.message || connErr.errMsg || ''); - if (/already connect|已连接|already connected/i.test(errText)) { - if (!state.currentDevice || state.currentDevice.id !== device.deviceId) { - commit('SET_CURRENT_DEVICE', { - id: device.deviceId, - name: device.name || '未知设备', - serviceId: (state.currentDevice && state.currentDevice.serviceId) || '', - characteristicId: (state.currentDevice && state.currentDevice.characteristicId) || '' - }); - } - commit('SET_RECONNECTING', false); - commit('SET_RECONNECT_MESSAGE', ''); - commit('SET_CONNECTION_STATUS', 'connected'); - commit('SET_RECONNECT_ATTEMPTS', 0); - dispatch('stopScan'); - commit('ADD_LOG', { type: 'warning', message: `设备已处于连接状态,继续读取重量: ${device.name || device.deviceId}` }); - return true; - } - throw connErr; - } - const [srvErr, srvRes] = await uni.getBLEDeviceServices({ deviceId: device.deviceId }); - if (srvErr || !srvRes || !srvRes.services || !srvRes.services.length) { - throw new Error('未发现设备服务'); - } + // already-connect 兜底:设备已处于系统连接状态,重新发现服务并重新订阅通知, + // 避免复用旧特征实例导致收不到重量数据 + if (connErr && /already connect|已连接|already connected/i.test(String(connErr.message || connErr.errMsg || ''))) { + // 连接已建立,短暂等待服务可用后重新发现服务/特征 + await delay(400); + const discovered = await discoverNotifyCharacteristic(device.deviceId); + if (discovered.error) throw discovered.error; - // 遍历服务,找到具备 notify/indicate 的特征值(电子秤重量数据通常通过通知推送) - let notifyChar = null; - let notifyService = null; - for (const service of srvRes.services) { - const [chErr, chRes] = await uni.getBLEDeviceCharacteristics({ + const [notifyErr] = await uni.notifyBLECharacteristicValueChange({ deviceId: device.deviceId, - serviceId: service.uuid + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid, + state: true }); - if (chErr || !chRes || !chRes.characteristics) continue; + if (notifyErr) throw notifyErr; - for (const ch of chRes.characteristics) { - if (ch.properties && (ch.properties.notify || ch.properties.indicate)) { - notifyChar = ch; - notifyService = service; - break; - } - } - if (notifyChar) break; + commit('SET_CURRENT_DEVICE', { + id: device.deviceId, + name: device.name || '未知设备', + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid + }); + saveHistoryDevice({ deviceId: device.deviceId, name: device.name || '' }); + manualDisconnect = false; + commit('SET_RECONNECTING', false); + commit('SET_RECONNECT_MESSAGE', ''); + commit('SET_CONNECTION_STATUS', 'connected'); + commit('SET_RECONNECT_ATTEMPTS', 0); + dispatch('stopScan'); + commit('ADD_LOG', { type: 'warning', message: `设备已处于连接状态,已重新订阅重量数据: ${device.name || device.deviceId}` }); + return true; } + if (connErr) throw connErr; - if (!notifyChar || !notifyService) { - throw new Error('未找到可订阅的重量数据特征'); - } + const discovered = await discoverNotifyCharacteristic(device.deviceId); + if (discovered.error) throw discovered.error; const [notifyErr] = await uni.notifyBLECharacteristicValueChange({ deviceId: device.deviceId, - serviceId: notifyService.uuid, - characteristicId: notifyChar.uuid, + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid, state: true }); if (notifyErr) throw notifyErr; @@ -603,9 +678,10 @@ const actions = { commit('SET_CURRENT_DEVICE', { id: device.deviceId, name: device.name || '未知设备', - serviceId: notifyService.uuid, - characteristicId: notifyChar.uuid + serviceId: discovered.service.uuid, + characteristicId: discovered.characteristic.uuid }); + saveHistoryDevice({ deviceId: device.deviceId, name: device.name || '' }); clearReconnectTimer(); manualDisconnect = false; commit('SET_RECONNECT_ATTEMPTS', 0); @@ -616,6 +692,7 @@ const actions = { commit('ADD_LOG', { type: 'success', message: `连接成功: ${device.name || device.deviceId}` }); return true; } catch (e) { + manualDisconnect = false; commit('SET_RECONNECTING', false); commit('SET_RECONNECT_MESSAGE', '连接失败,可手动重连'); commit('SET_CONNECTION_STATUS', 'error'); @@ -625,6 +702,8 @@ const actions = { message: `连接失败: ${e.message || e.errMsg || '未知错误'}` }); throw e; + } finally { + connectLock = false; } }, diff --git a/src/pages/work/order/order-review-comps/ReviewDetail.vue b/src/pages/work/order/order-review-comps/ReviewDetail.vue index 5335dbe..f4d4f24 100644 --- a/src/pages/work/order/order-review-comps/ReviewDetail.vue +++ b/src/pages/work/order/order-review-comps/ReviewDetail.vue @@ -1695,7 +1695,10 @@ import voice from '@/core/utils/voice'; .no-barcode-popup__actions { display: flex; align-items: center; - gap: 20rpx; + } + + .no-barcode-popup__btn + .no-barcode-popup__btn { + margin-left: 20rpx; } .no-barcode-popup__btn { diff --git a/src/pages/work/order/order-review.vue b/src/pages/work/order/order-review.vue index 37a8492..969c59b 100644 --- a/src/pages/work/order/order-review.vue +++ b/src/pages/work/order/order-review.vue @@ -8,6 +8,13 @@