feat:绑定设备和设备详情

This commit is contained in:
李伊帆
2026-10-10 11:11:44 +08:00
parent 179bd3b66c
commit d4b02745e1
64 changed files with 3526 additions and 221 deletions

View File

@@ -1,29 +1,73 @@
import { onUnmounted, ref } from 'vue'
import { onMounted, onUnmounted, ref } from 'vue'
import {
ensureBluetoothReady,
getBluetoothErrorMessage,
getDiscoveredBluetoothDevices,
isBluetoothApiSupported,
startBluetoothDiscovery,
stopBluetoothDiscovery,
subscribeBluetoothDevicesFound,
} from '@/utils/bluetooth'
import { buildBindDeviceQuery } from '../add-device.types'
import type { ScannedDeviceItem } from './scan.types'
const MOCK_DEVICES: ScannedDeviceItem[] = [
{
id: 'z2-purifier',
name: 'Z2 空气净化器',
serialMasked: '****A3B2',
canAdd: true,
},
{
id: 'unknown',
name: '未知设备',
serialMasked: '****7C91',
canAdd: false,
},
]
import { mapBluetoothDevice } from './scan.utils'
const SCAN_SECONDS = 20
export function useScanDevice() {
const currentStep = 2
const devices = ref<ScannedDeviceItem[]>(MOCK_DEVICES)
const selectedId = ref(MOCK_DEVICES[0]?.id ?? '')
const devices = ref<ScannedDeviceItem[]>([])
const selectedId = ref('')
const secondsLeft = ref(SCAN_SECONDS)
const scanning = ref(true)
const scanError = ref('')
let unsubscribeFound: (() => void) | undefined
function upsertDevice(item: ScannedDeviceItem) {
const idx = devices.value.findIndex((d) => d.id === item.id)
if (idx >= 0) {
devices.value[idx] = item
return
}
devices.value.push(item)
if (!selectedId.value) selectedId.value = item.id
}
function ingestBluetoothDevices(found: UniApp.BluetoothDeviceInfo[]) {
for (const device of found) {
upsertDevice(mapBluetoothDevice(device))
}
}
async function beginDiscovery() {
if (!isBluetoothApiSupported()) {
scanning.value = false
scanError.value = '当前环境不支持蓝牙扫描,请使用 App 或微信小程序'
return
}
try {
await ensureBluetoothReady()
unsubscribeFound = subscribeBluetoothDevicesFound((found) => {
ingestBluetoothDevices(found)
})
const existing = await getDiscoveredBluetoothDevices()
ingestBluetoothDevices(existing)
await startBluetoothDiscovery()
} catch (err) {
scanning.value = false
scanError.value = getBluetoothErrorMessage(err)
uni.showToast({ title: scanError.value, icon: 'none' })
}
}
onMounted(() => {
void beginDiscovery()
})
const timer = setInterval(() => {
if (secondsLeft.value <= 0) {
@@ -33,11 +77,14 @@ export function useScanDevice() {
secondsLeft.value -= 1
if (secondsLeft.value === 0) {
scanning.value = false
void stopBluetoothDiscovery().catch(() => {})
}
}, 1000)
onUnmounted(() => {
clearInterval(timer)
unsubscribeFound?.()
void stopBluetoothDiscovery().catch(() => {})
})
function onSelectDevice(id: string) {
@@ -50,10 +97,15 @@ export function useScanDevice() {
uni.showToast({ title: '暂无法添加该设备', icon: 'none' })
return
}
const q = [
`name=${encodeURIComponent(device.name)}`,
`serial=${encodeURIComponent(device.serialMasked)}`,
].join('&')
const q = buildBindDeviceQuery({
deviceNo: device.deviceNo,
name: device.name,
serialMasked: device.serialMasked,
productId: device.productId,
model: device.model,
imei: device.imei,
mac: device.mac,
})
uni.navigateTo({ url: `/pages/add-device/pair/index?${q}` })
}
@@ -63,6 +115,7 @@ export function useScanDevice() {
selectedId,
secondsLeft,
scanning,
scanError,
onSelectDevice,
onDeviceAction,
}