import { NavBar, SpinLoading, Toast } from 'antd-mobile'; import './Bluetooth.scss'; import { useEffect } from 'react'; import { useAppDispatch, useAppSelector } from '../utils/hooks'; import Bridge from '../utils/bridge'; import { updateBleLinkStatus } from '../store/features/contextSlice'; export default function Bluetooth() { const dispatch = useAppDispatch(); const contextState = useAppSelector((state) => state.context); useEffect(() => { Bridge.scanPeripherals().then((res) => { if (!res.success) { Toast.show(res.message); } }); return () => { Bridge.stopScanPeripherals().then((res) => {}); }; }, []); const onConnect = (mac: string) => { Bridge.connectPeripheral({ mac }).then((res) => { if (!res.success) { Toast.show(res.message); } else { dispatch(updateBleLinkStatus({mac, link: true})) } }); }; const onDisconnect = (mac: string) => { Bridge.disconnectPeripheral().then((res) => { if (!res.success) { Toast.show(res.message); } else { dispatch(updateBleLinkStatus({mac, link: false})) } }); }; return (
蓝牙

附近设备

{contextState.bleList.map((ble) => (
bl.linked) ? undefined : () => onConnect(ble.mac) } > {ble.name} {ble.linked ? '已连接' : ''} {ble.linked && ( )}
))}
); }