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 && (