From c5eabd8328bcfa94df82f82ac5d69ff384e41196 Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Mon, 14 Apr 2025 10:29:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=93=9D=E7=89=99=E8=BF=9E=E6=8E=A5=E5=92=8C?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E6=97=B6=E7=9A=84UI=E5=8F=8D=E9=A6=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Bluetooth.tsx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/pages/Bluetooth.tsx b/src/pages/Bluetooth.tsx index 2f92c78..7865168 100644 --- a/src/pages/Bluetooth.tsx +++ b/src/pages/Bluetooth.tsx @@ -1,14 +1,16 @@ import { NavBar, SpinLoading, Toast } from 'antd-mobile'; import './Bluetooth.scss'; -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; import { useAppDispatch, useAppSelector } from '../utils/hooks'; import Bridge from '../utils/bridge'; -import { updateBleLinkStatus } from '../store/features/contextSlice'; +import { updateBleLinkStatus, updateBleList } from '../store/features/contextSlice'; export default function Bluetooth() { const dispatch = useAppDispatch(); const contextState = useAppSelector((state) => state.context); + const [connectingMac, setConnectingMac] = useState('') + useEffect(() => { Bridge.scanPeripherals().then((res) => { if (!res.success) { @@ -21,7 +23,9 @@ export default function Bluetooth() { }, []); const onConnect = (mac: string) => { + setConnectingMac(mac); Bridge.connectPeripheral({ mac }).then((res) => { + setConnectingMac(''); if (!res.success) { Toast.show(res.message); } else { @@ -34,7 +38,8 @@ export default function Bluetooth() { if (!res.success) { Toast.show(res.message); } else { - dispatch(updateBleLinkStatus({ mac, link: false })); + dispatch(updateBleList([])); // 清空列表 + // dispatch(updateBleLinkStatus({ mac, link: false })); } }); }; @@ -50,7 +55,7 @@ export default function Bluetooth() {

附近设备

- {contextState.bleList.some((bl) => bl.linked) ? undefined : scanning()} + {contextState.bleList.some((bl) => bl.linked) || connectingMac ? undefined : scanning()}
{contextState.bleList.map((ble) => ( @@ -58,7 +63,7 @@ export default function Bluetooth() { key={ble.mac} className="py-2 flex items-center border-b border-[#eee]" onClick={ - contextState.bleList.some((bl) => bl.linked) + contextState.bleList.some((bl) => bl.linked) || connectingMac ? undefined : () => onConnect(ble.mac) } @@ -68,7 +73,8 @@ export default function Bluetooth() {

{ble.mac}

- {ble.linked ? '已连接' : ''} + {ble.linked ? '已连接' : connectingMac === ble.mac ? '正在连接' : ''} + {connectingMac === ble.mac &&
} {ble.linked && (