|
|
@ -1,7 +1,37 @@ |
|
|
|
import { NavBar, SpinLoading } from 'antd-mobile'; |
|
|
|
import { NavBar, SpinLoading, Toast } from 'antd-mobile'; |
|
|
|
import './Bluetooth.scss'; |
|
|
|
import { useEffect } from 'react'; |
|
|
|
import { useAppSelector } from '../utils/hooks'; |
|
|
|
import Bridge from '../utils/bridge'; |
|
|
|
|
|
|
|
export default function Bluetooth() { |
|
|
|
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 = (id: string) => { |
|
|
|
Bridge.connectPeripheral({ id }).then((res) => { |
|
|
|
if (!res.success) { |
|
|
|
Toast.show(res.message); |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
const onDisconnect = (id: string) => { |
|
|
|
Bridge.disconnectPeripheral().then((res) => { |
|
|
|
if (!res.success) { |
|
|
|
Toast.show(res.message); |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
return ( |
|
|
|
<div> |
|
|
|
<NavBar className="bg-white" back={null}> |
|
|
@ -9,33 +39,33 @@ export default function Bluetooth() { |
|
|
|
</NavBar> |
|
|
|
<div className="home-page-content overflow-x-hidden overflow-y-auto"> |
|
|
|
<section> |
|
|
|
<div className="h-[42px] px-5"> |
|
|
|
<h1 className="h-[42px] leading-[42px] text-base text-text font-medium">已配对设备</h1> |
|
|
|
</div> |
|
|
|
<div className="bg-white px-5 text-sm text-text"> |
|
|
|
<div className="h-12 flex justify-between items-center border-b border-[#eee]"> |
|
|
|
<span>设备名</span> |
|
|
|
<span>已连接</span> |
|
|
|
</div> |
|
|
|
<div className="h-12 flex justify-between items-center border-b border-[#eee]"> |
|
|
|
<span>设备名</span> |
|
|
|
<span></span> |
|
|
|
</div> |
|
|
|
</div> |
|
|
|
</section> |
|
|
|
|
|
|
|
<section> |
|
|
|
<div className="h-[42px] px-5 flex items-center justify-between"> |
|
|
|
<h1 className="h-[42px] leading-[42px] text-base text-text font-medium">附近设备</h1> |
|
|
|
<SpinLoading /> |
|
|
|
</div> |
|
|
|
<div className="bg-white px-5 text-sm text-text"> |
|
|
|
<div className="h-12 flex justify-between items-center border-b border-[#eee]"> |
|
|
|
<span>设备1</span> |
|
|
|
</div> |
|
|
|
<div className="h-12 flex justify-between items-center border-b border-[#eee]"> |
|
|
|
<span>设备2</span> |
|
|
|
{contextState.bleList.map((ble) => ( |
|
|
|
<div |
|
|
|
key={ble.mac} |
|
|
|
className="h-12 flex items-center border-b border-[#eee]" |
|
|
|
onClick={ |
|
|
|
contextState.bleList.some((bl) => bl.linked) |
|
|
|
? undefined |
|
|
|
: () => onConnect(ble.mac) |
|
|
|
} |
|
|
|
> |
|
|
|
<span>{ble.name}</span> |
|
|
|
<span className="text-xs text-title ml-2">{ble.linked ? '已连接' : ''}</span> |
|
|
|
{ble.linked && ( |
|
|
|
<button |
|
|
|
className="btn-contained px-2 py-1 rounded ml-auto" |
|
|
|
onClick={() => onDisconnect(ble.mac)} |
|
|
|
> |
|
|
|
断开 |
|
|
|
</button> |
|
|
|
)} |
|
|
|
</div> |
|
|
|
))} |
|
|
|
</div> |
|
|
|
</section> |
|
|
|
</div> |
|
|
|