Browse Source

蓝牙连接后更新状态

master
zhangjiming 4 months ago
parent
commit
ff407255c9
  1. 8
      src/pages/Bluetooth.tsx
  2. 19
      src/store/features/contextSlice.ts

8
src/pages/Bluetooth.tsx

@ -1,10 +1,12 @@
import { NavBar, SpinLoading, Toast } from 'antd-mobile';
import './Bluetooth.scss';
import { useEffect } from 'react';
import { useAppSelector } from '../utils/hooks';
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(() => {
@ -22,6 +24,8 @@ export default function Bluetooth() {
Bridge.connectPeripheral({ id }).then((res) => {
if (!res.success) {
Toast.show(res.message);
} else {
dispatch(updateBleLinkStatus({id, link: true}))
}
});
};
@ -29,6 +33,8 @@ export default function Bluetooth() {
Bridge.disconnectPeripheral().then((res) => {
if (!res.success) {
Toast.show(res.message);
} else {
dispatch(updateBleLinkStatus({id, link: false}))
}
});
};

19
src/store/features/contextSlice.ts

@ -100,6 +100,15 @@ export const contextSlice = createSlice({
state.bleList = action.payload;
},
updateBleLinkStatus: (state, action: PayloadAction<{ id: string; link: boolean }>) => {
state.bleList = state.bleList.map((ble) => {
if (ble.mac === action.payload.id) {
ble.linked = action.payload.link;
}
return ble;
});
},
updateSyncProgress: (state, action: PayloadAction<SyncProgress['data']>) => {
state.syncProgress = action.payload;
},
@ -118,6 +127,12 @@ export const contextSlice = createSlice({
},
});
export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } =
contextSlice.actions;
export const {
updateOrg,
updateDevice,
updateRailTypeId,
updateBleList,
updateBleLinkStatus,
updateSyncProgress,
} = contextSlice.actions;
export default contextSlice.reducer;
Loading…
Cancel
Save