From 7c1d0344db78b37ff02a6b14ad28dd29dece3537 Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Thu, 3 Apr 2025 17:44:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=93=9D=E7=89=99mock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Bluetooth.tsx | 76 ++++++++++++++++++++++++++++++++++--------------- src/pages/Measure.tsx | 17 ----------- src/utils/bridge.ts | 63 +++++++++++++++++++++++++++++++++------- 3 files changed, 105 insertions(+), 51 deletions(-) diff --git a/src/pages/Bluetooth.tsx b/src/pages/Bluetooth.tsx index d91b855..e212ff7 100644 --- a/src/pages/Bluetooth.tsx +++ b/src/pages/Bluetooth.tsx @@ -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 (
@@ -9,33 +39,33 @@ export default function Bluetooth() {
-
-

已配对设备

-
-
-
- 设备名 - 已连接 -
-
- 设备名 - -
-
-
- -

附近设备

-
- 设备1 -
-
- 设备2 -
+ {contextState.bleList.map((ble) => ( +
bl.linked) + ? undefined + : () => onConnect(ble.mac) + } + > + {ble.name} + {ble.linked ? '已连接' : ''} + {ble.linked && ( + + )} +
+ ))}
diff --git a/src/pages/Measure.tsx b/src/pages/Measure.tsx index d8143a2..f752c5e 100644 --- a/src/pages/Measure.tsx +++ b/src/pages/Measure.tsx @@ -17,23 +17,6 @@ import { selectLabeledKtjOrgs } from '../store/features/baseData'; import { updateOrg } from '../store/features/contextSlice'; import { textsOfKeys } from '../utils/helper'; -// declare global { -// interface Window { -// ReactNativeWebView: { postMessage: (arg: string) => void }; -// funcMap: Record; -// bridgeCall: (func: string, args: any[]) => void; -// } -// } -// window.funcMap = { -// add: (a: number, b: number) => a + b, -// sub: (a: number, b: number) => a - b, -// }; - -// window.bridgeCall = (func, ...args) => { -// console.log(func, args); -// const res = window.funcMap[func].apply(null, args); -// console.log('res:', res); -// }; export default function Measure() { const navigate = useNavigate(); diff --git a/src/utils/bridge.ts b/src/utils/bridge.ts index e2d675d..710e2a0 100644 --- a/src/utils/bridge.ts +++ b/src/utils/bridge.ts @@ -89,17 +89,15 @@ export function emitBridgeEvent(event: MobileDatagram) { } export function registerBridgeFunc() { -// window.bridgeFunc = { -// peripheralStatus: (param: string) => { -// bridgeSub.next({ func: 'peripheralStatus', data: JSON.parse(param) }); -// }, -// }; - -// window.bridgeCall = (func, param) => { -// const res = window.bridgeFunc[func].call(null, param); -// console.log('res:', res); -// }; - + // window.bridgeFunc = { + // peripheralStatus: (param: string) => { + // bridgeSub.next({ func: 'peripheralStatus', data: JSON.parse(param) }); + // }, + // }; + // window.bridgeCall = (func, param) => { + // const res = window.bridgeFunc[func].call(null, param); + // console.log('res:', res); + // }; // const jsFuncs = ['funcInJs']; // jsFuncs.forEach((funcName) => { // window.WebViewJavascriptBridge.registerHandler(funcName, (data, callback) => { @@ -216,4 +214,47 @@ export default class Bridge { }); } } + + static scanPeripherals() { + return httpRequest({ + url: '/api/ble/list/start', + method: 'POST', + params: {}, + }); + } + static stopScanPeripherals() { + return httpRequest({ + url: '/api/ble/list/stop', + method: 'POST', + params: {}, + }); + } + static connectPeripheral(params: { id: string }) { + return httpRequest({ + url: '/api/ble/connect', + method: 'POST', + params, + }); + } + static disconnectPeripheral() { + return httpRequest({ + url: '/api/ble/disconnect', + method: 'POST', + params: {}, + }); + } + static getConfig() { + return httpRequest>({ + url: '/api/system/config', + method: 'POST', + params: {}, + }); + } + static saveConfig(params: { server: string }) { + return httpRequest>({ + url: '/api/system/config/save', + method: 'POST', + params, + }); + } }