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.

80 lines
2.5 KiB

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