diff --git a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515DeviceManager.java b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515DeviceManager.java index cff1caf..b872e5b 100644 --- a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515DeviceManager.java +++ b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515DeviceManager.java @@ -278,7 +278,12 @@ public class ASR5515DeviceManager { @Override public void onWearDetectionResponse(ASR5515Protocol.WearDetectionResponse response) { if (wearDetectionListener != null) { - mainHandler.post(() -> wearDetectionListener.onWearDetectionResponse(response)); + mainHandler.post(() -> { + // 发送佩戴检测接收确认 + onWearDetectionResponseReceived(response.sn); + // 调用佩戴检测监听器 + wearDetectionListener.onWearDetectionResponse(response); + }); } } }); @@ -303,7 +308,17 @@ public class ASR5515DeviceManager { @Override public void onAlgoResultReceived(final ASR5515Protocol.AlgoResultResponse response) { if (algoResultListener != null) { - mainHandler.post(() -> algoResultListener.onAlgoResultReceived(response)); + mainHandler.post(() -> { + // 根据算法结果类型发送接收确认 + if (response.type == ASR5515Protocol.AlgoResultResponse.TYPE_HEART_RATE) { + // 发送心率接收确认 + onHeartRateDataReceived(response.sn); + } else if (response.type == ASR5515Protocol.AlgoResultResponse.TYPE_SPO2) { + // 发送血氧接收确认 + onSpO2DataReceived(response.sn); + } + algoResultListener.onAlgoResultReceived(response); + }); } } }); @@ -535,7 +550,7 @@ public class ASR5515DeviceManager { * 设置采集频率 * @param freq 频率值 */ - public void setCollectFreq(byte freq) { + public void setCollectFreq(byte len, byte freq) { wakeupDevice(); serialPortHelper.setCollectFreq(freq); } @@ -572,6 +587,19 @@ public class ASR5515DeviceManager { serialPortHelper.sendWearDetectionRequest(); } + public void onWearDetectionResponseReceived(short sn) { + if (!isDeviceReady()) { + LogManager.e(TAG, "设备未就绪,无法发送佩戴检测接收确认"); + return; + } + + try { + wakeupDevice(); + serialPortHelper.onWearDetectionResponseReceived(sn); + } catch (Exception e) { + LogManager.e(TAG, "发送佩戴检测接收确认失败: " + e.getMessage()); + } + } /** * 发送GH3220开启测量请求 * @param type 测量类型,参考ASR5515Protocol.GH3220MeasureType @@ -592,43 +620,41 @@ public class ASR5515DeviceManager { /** * 发送心率接收确认 + * * @param sn 序列号 - * @return true 如果发送成功 */ - public boolean onHeartRateDataReceived(short sn) { - if (!isDeviceReady()) { - LogManager.e(TAG, "设备未就绪,无法发送心率接收确认"); - return false; - } + public void onHeartRateDataReceived(short sn) { + if (!isDeviceReady()) { + LogManager.e(TAG, "设备未就绪,无法发送心率接收确认"); + return; + } try { wakeupDevice(); byte[] request = ASR5515Protocol.createHeartRateReceivedRequest(sn); - return safeSendData(request); + safeSendData(request); } catch (Exception e) { LogManager.e(TAG, "发送心率接收确认失败: " + e.getMessage()); - return false; } } /** * 发送血氧接收确认 + * * @param sn 序列号 - * @return true 如果发送成功 */ - public boolean onSpO2DataReceived(short sn) { - if (!isDeviceReady()) { - LogManager.e(TAG, "设备未就绪,无法发送血氧接收确认"); - return false; - } + public void onSpO2DataReceived(short sn) { + if (!isDeviceReady()) { + LogManager.e(TAG, "设备未就绪,无法发送血氧接收确认"); + return; + } try { wakeupDevice(); byte[] request = ASR5515Protocol.createSpO2ReceivedRequest(sn); - return safeSendData(request); + safeSendData(request); } catch (Exception e) { LogManager.e(TAG, "发送血氧接收确认失败: " + e.getMessage()); - return false; } } diff --git a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515Protocol.java b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515Protocol.java index ce010b0..ebfda1a 100644 --- a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515Protocol.java +++ b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/ASR5515Protocol.java @@ -208,7 +208,9 @@ public class ASR5515Protocol { public static final short CMD_DYNAMIC_MEASURE = 0x00CF; // 动态测量 [207 0 0] public static final short CMD_DYNAMIC_MEASURE_RESP = 0x00D0; // 动态测量响应 [208 sn 0] public static final short CMD_WEAR_DETECTION = 0x0105; // 佩戴检测查询 [261 0 0] + public static final short CMD_WEAR_DETECTION_RESP_CHECK = 0x0106; // 佩戴检测响应 [262 sn len 0/1] public static final short CMD_WEAR_DETECTION_RESP = 0x0108; // 佩戴检测响应 [264 sn len 0/1] + public static final short CMD_WEAR_ACK = 0x0109; // 佩戴检测确认 [265 0 0] public static final short CMD_GH3220_MEASURE_START = 0x01F5; // GH3220开启测量 [501 0 len type] public static final short CMD_GH3220_MEASURE_START_RESP = 0x01F6; // GH3220开启测量响应 [502 sn len status] public static final short CMD_GH3220_MEASURE_STOP = 0x01F7; // GH3220停止测量 [503 0 len type] @@ -601,17 +603,29 @@ public class ASR5515Protocol { return createFrame(Commands.CMD_WEAR_DETECTION, (short) 0, new byte[]{}); } + // 创建佩戴检测确认请求帧 [265 0 0] + public static byte[] createWearAckRequest(short sn) { + return createFrame(Commands.CMD_WEAR_ACK, sn, new byte[]{}); + } + // 佩戴检测响应类 public static class WearDetectionResponse { public boolean isWearing; // true表示已佩戴,false表示未佩戴 + public short sn; - public WearDetectionResponse( boolean isWearing) { + public WearDetectionResponse(boolean isWearing, short sn) { this.isWearing = isWearing; + this.sn = sn; + } + + public short getSn() { + return sn; } @Override public String toString() { return "WearDetectionResponse{" + + "sn=" + sn + ", isWearing=" + isWearing + '}'; } @@ -619,11 +633,15 @@ public class ASR5515Protocol { // 解析佩戴检测响应帧 [262 sn len 0/1] public static WearDetectionResponse parseWearDetectionResponse(Frame frame) { - if (frame == null || frame.command != Commands.CMD_WEAR_DETECTION_RESP ) { + if (frame == null || (frame.command != Commands.CMD_WEAR_DETECTION_RESP_CHECK && + frame.command != Commands.CMD_WEAR_DETECTION_RESP)) { + return null; + } + if (frame.data == null || frame.data.length < 1) { return null; } boolean isWearing = frame.data[0] == 1; - return new WearDetectionResponse(isWearing); + return new WearDetectionResponse(isWearing, frame.sn); } // GH3220测量类型常量 @@ -698,12 +716,14 @@ public class ASR5515Protocol { public static final int TYPE_SPO2 = 1; // 血氧类型 public int type; // 算法类型:0-心率,1-血氧 + public short sn; // 序列号 public int value; // 主要值(心率值或血氧值) public int confidence; // 置信度 public int signalQuality; // 信号质量(心率为信噪比,血氧为R值) public int confidenceLevel; // 置信等级(仅用于血氧,范围-2到5) - public AlgoResultResponse(int type, int value, int confidence, int signalQuality, int confidenceLevel) { + public AlgoResultResponse(int type, short sn, int value, int confidence, int signalQuality, int confidenceLevel) { + this.sn = sn; this.type = type; this.value = value; this.confidence = confidence; @@ -736,11 +756,17 @@ public class ASR5515Protocol { return confidence; } + // 获取序列号 + public short getSn() { + return sn; + } + @Override public String toString() { if (type == TYPE_HEART_RATE) { return "AlgoResultResponse{" + "type=HeartRate" + + ", sn=" + sn + ", heartRate=" + value + ", confidence=" + confidence + ", signalNoiseRatio=" + signalQuality + @@ -748,6 +774,7 @@ public class ASR5515Protocol { } else { return "AlgoResultResponse{" + "type=SpO2" + + ", sn=" + sn + ", spo2=" + value + ", confidence=" + confidence + ", rValue=" + signalQuality + @@ -825,7 +852,7 @@ public class ASR5515Protocol { LogManager.d(TAG, "Parse algo result - type: " + (type == AlgoResultResponse.TYPE_HEART_RATE ? "HeartRate" : "SpO2") + ", value: " + value + ", confidence: " + confidence); - return new AlgoResultResponse(type, value, confidence, signalQuality, confidenceLevel); + return new AlgoResultResponse(type, frame.sn, value, confidence, signalQuality, confidenceLevel); } catch (Exception e) { LogManager.e(TAG, "Parse algo result error: " + e.getMessage()); return null; diff --git a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/SerialPortHelper.java b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/SerialPortHelper.java index bfefabb..0cd67af 100644 --- a/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/SerialPortHelper.java +++ b/app/src/main/java/com/ismart/ib86/feature/serial/SerialPort/SerialPortHelper.java @@ -323,6 +323,16 @@ public class SerialPortHelper { sendData(data, null); } + /** + * 发送佩戴检测确认 + * @param sn 序列号 + * @return true 如果发送成功 + */ + public void onWearDetectionResponseReceived(short sn) { + byte[] data = ASR5515Protocol.createWearAckRequest(sn); + sendData(data, null); + } + public boolean open() { try { @@ -649,10 +659,12 @@ public class SerialPortHelper { case ASR5515Protocol.Commands.CMD_WEAR_DETECTION_RESP: ASR5515Protocol.WearDetectionResponse wearDetectionResponse = ASR5515Protocol.parseWearDetectionResponse(frame); - if (wearDetectionResponse != null && wearDetectionCallback != null) { - if (lastWearStatus == null || lastWearStatus != wearDetectionResponse.isWearing) { - wearDetectionCallback.onWearDetectionResponse(wearDetectionResponse); - lastWearStatus = wearDetectionResponse.isWearing; + if (wearDetectionResponse != null) { + if (wearDetectionCallback != null) { + if (lastWearStatus == null || lastWearStatus != wearDetectionResponse.isWearing) { + wearDetectionCallback.onWearDetectionResponse(wearDetectionResponse); + lastWearStatus = wearDetectionResponse.isWearing; + } } } break; @@ -674,13 +686,6 @@ public class SerialPortHelper { case ASR5515Protocol.Commands.CMD_SPO2_RESULT: ASR5515Protocol.AlgoResultResponse algoResultResponse = ASR5515Protocol.parseAlgoResultResponse(frame); if (algoResultResponse != null && algoResultCallback != null) { - // 发送接收确认 - byte[] confirmData; - if(frame.command == ASR5515Protocol.Commands.CMD_HEART_RATE_RESULT) - onHeartRateDataReceived(frame.sn); - else if(frame.command == ASR5515Protocol.Commands.CMD_SPO2_RESULT) - onSpO2DataReceived(frame.sn); - algoResultCallback.onAlgoResultReceived(algoResultResponse); } break;