feat:绑定设备和设备详情
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user