|
|
@ -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 <div className='flex items-center gap-x-3'><p>扫描中</p><SpinLoading /></div> |
|
|
|
|
|
|
|
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 ( |
|
|
|
<div> |
|
|
@ -55,7 +76,9 @@ export default function Bluetooth() { |
|
|
|
<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> |
|
|
|
{contextState.bleList.some((bl) => bl.linked) || connectingMac ? undefined : scanning()} |
|
|
|
{scanVisible ? |
|
|
|
<div className='flex items-center gap-x-3'><SpinLoading /><Button size='small' color='primary' onClick={scanStop}>停止扫描</Button></div> |
|
|
|
: <div className='flex items-center gap-x-3'><Button size='small' color='primary' onClick={scanStart}>扫描</Button></div>} |
|
|
|
</div> |
|
|
|
<div className="bg-white px-5 text-sm text-text"> |
|
|
|
{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() { |
|
|
|
<p className='text-[10px] text-title'>{ble.mac}</p> |
|
|
|
</div> |
|
|
|
|
|
|
|
<span className="text-xs text-title ml-3">{ble.linked ? '已连接' : connectingMac === ble.mac ? '正在连接' : ''}</span> |
|
|
|
<span className="text-xs text-title ml-3">{device.connected && ble.linked ? '已连接' : connectingMac === ble.mac ? '正在连接' : ''}</span> |
|
|
|
{connectingMac === ble.mac && <div className='ml-auto'><SpinLoading /></div>} |
|
|
|
{ble.linked && ( |
|
|
|
{device.connected && ble.linked && ( |
|
|
|
<button |
|
|
|
className="btn-contained px-2 py-1 rounded ml-auto" |
|
|
|
onClick={() => onDisconnect(ble.mac)} |
|
|
|