You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.1 KiB

4 months ago
4 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
  1. import { NavBar, SpinLoading, Toast } from 'antd-mobile';
  2. import './Bluetooth.scss';
  3. import { useEffect, useState } from 'react';
  4. import { useAppDispatch, useAppSelector } from '../utils/hooks';
  5. import Bridge from '../utils/bridge';
  6. import { updateBleLinkStatus, updateBleList } from '../store/features/contextSlice';
  7. export default function Bluetooth() {
  8. const dispatch = useAppDispatch();
  9. const contextState = useAppSelector((state) => state.context);
  10. const [connectingMac, setConnectingMac] = useState('')
  11. useEffect(() => {
  12. Bridge.scanPeripherals().then((res) => {
  13. if (!res.success) {
  14. Toast.show(res.message);
  15. }
  16. });
  17. return () => {
  18. Bridge.stopScanPeripherals().then((res) => {});
  19. };
  20. }, []);
  21. const onConnect = (mac: string) => {
  22. setConnectingMac(mac);
  23. Bridge.connectPeripheral({ mac }).then((res) => {
  24. setConnectingMac('');
  25. if (!res.success) {
  26. Toast.show(res.message);
  27. } else {
  28. dispatch(updateBleLinkStatus({ mac, link: true }));
  29. }
  30. });
  31. };
  32. const onDisconnect = (mac: string) => {
  33. Bridge.disconnectPeripheral().then((res) => {
  34. if (!res.success) {
  35. Toast.show(res.message);
  36. } else {
  37. dispatch(updateBleList([])); // 清空列表
  38. // dispatch(updateBleLinkStatus({ mac, link: false }));
  39. }
  40. });
  41. };
  42. function scanning() {
  43. return <div className='flex items-center gap-x-3'><p></p><SpinLoading /></div>
  44. }
  45. return (
  46. <div>
  47. <NavBar className="bg-white" back={null}>
  48. </NavBar>
  49. <div className="home-page-content bluetooth-list overflow-x-hidden overflow-y-auto">
  50. <section>
  51. <div className="h-[42px] px-5 flex items-center justify-between">
  52. <h1 className="h-[42px] leading-[42px] text-base text-text font-medium"></h1>
  53. {contextState.bleList.some((bl) => bl.linked) || connectingMac ? undefined : scanning()}
  54. </div>
  55. <div className="bg-white px-5 text-sm text-text">
  56. {contextState.bleList.map((ble) => (
  57. <div
  58. key={ble.mac}
  59. className="py-2 flex items-center border-b border-[#eee]"
  60. onClick={
  61. contextState.bleList.some((bl) => bl.linked) || connectingMac
  62. ? undefined
  63. : () => onConnect(ble.mac)
  64. }
  65. >
  66. <div>
  67. <p>{ble.name}</p>
  68. <p className='text-[10px] text-title'>{ble.mac}</p>
  69. </div>
  70. <span className="text-xs text-title ml-3">{ble.linked ? '已连接' : connectingMac === ble.mac ? '正在连接' : ''}</span>
  71. {connectingMac === ble.mac && <div className='ml-auto'><SpinLoading /></div>}
  72. {ble.linked && (
  73. <button
  74. className="btn-contained px-2 py-1 rounded ml-auto"
  75. onClick={() => onDisconnect(ble.mac)}
  76. >
  77. </button>
  78. )}
  79. </div>
  80. ))}
  81. </div>
  82. </section>
  83. </div>
  84. </div>
  85. );
  86. }