diff --git a/src/pages/Bluetooth.tsx b/src/pages/Bluetooth.tsx index 7865168..8071b2e 100644 --- a/src/pages/Bluetooth.tsx +++ b/src/pages/Bluetooth.tsx @@ -1,4 +1,4 @@ -import { NavBar, SpinLoading, Toast } from 'antd-mobile'; +import {Button, NavBar, SpinLoading, Toast} from 'antd-mobile'; import './Bluetooth.scss'; import { useEffect, useState } from 'react'; import { useAppDispatch, useAppSelector } from '../utils/hooks'; @@ -7,16 +7,19 @@ import { updateBleLinkStatus, updateBleList } from '../store/features/contextSli export default function Bluetooth() { const dispatch = useAppDispatch(); + const device = useAppSelector((state) => state.context.device); const contextState = useAppSelector((state) => state.context); const [connectingMac, setConnectingMac] = useState('') - + const [scanVisible , setScanVisible] = useState(false); useEffect(() => { - Bridge.scanPeripherals().then((res) => { - if (!res.success) { - Toast.show(res.message); - } - }); + // Bridge.scanPeripherals().then((res) => { + // if (!res.success) { + // Toast.show(res.message); + // }else { + // setScanVisible(true) + // } + // }); return () => { Bridge.stopScanPeripherals().then((res) => {}); }; @@ -30,6 +33,7 @@ export default function Bluetooth() { Toast.show(res.message); } else { dispatch(updateBleLinkStatus({ mac, link: true })); + scanStop() } }); }; @@ -43,8 +47,25 @@ export default function Bluetooth() { } }); }; - function scanning() { - return

扫描中

+ + const scanStart = () => { + Bridge.scanPeripherals().then((res) => { + if (!res.success) { + Toast.show(res.message); + }else { + setScanVisible(true) + } + }); + } + + const scanStop = () => { + Bridge.stopScanPeripherals().then((res) => { + if (!res.success) { + Toast.show(res.message); + }else { + setScanVisible(false) + } + }); } return (
@@ -55,7 +76,9 @@ export default function Bluetooth() {

附近设备

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

{ble.mac}

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