From d7abb64debd9cb199a7fd6650f6d8f391ded286b Mon Sep 17 00:00:00 2001 From: lvwang2002 Date: Sat, 26 Oct 2024 07:04:32 +0800 Subject: [PATCH] =?UTF-8?q?feature():=E6=A0=B9=E6=8D=AE=E5=89=8D=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E9=97=B4=E9=9A=94=E7=9A=84=E5=AE=9E=E7=8E=B0=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rn-dy-glucose-driver.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/rn-dy-glucose-driver.js b/src/rn-dy-glucose-driver.js index 334c9a3..bbbdf90 100755 --- a/src/rn-dy-glucose-driver.js +++ b/src/rn-dy-glucose-driver.js @@ -1,6 +1,6 @@ 'use strict'; import React, { Component } from 'react'; -import { NativeModules, DeviceEventEmitter, Platform } from 'react-native'; +import { NativeModules, DeviceEventEmitter, Platform,AppState, } from 'react-native'; @@ -74,9 +74,11 @@ export default class RNDYGlucoseDriver { this._timeoutPromise = this._timeoutPromise.bind(this); this.connectDevice = this.connectDevice.bind(this); this._mixTimeoutPromise = this._mixTimeoutPromise.bind(this); + this._blockSleep = this._blockSleep.bind(this); this._executeEvent = this._executeEvent.bind(this); this._registerEvent = this._registerEvent.bind(this); this._commadTimeInterval = this._commadTimeInterval.bind(this); + this._commadTimeIntervalWithTimer = this._commadTimeIntervalWithTimer.bind(this); this.createAuthChannel = this.createAuthChannel.bind(this); this.disconnecAllDevicestWithout = this.disconnecAllDevicestWithout.bind(this); this.getAllDataFromIndex = this.getAllDataFromIndex.bind(this); @@ -335,7 +337,7 @@ export default class RNDYGlucoseDriver { this._eventMap[DYG_ON_GET_BATTERY] = (deviceInfo) => { console.log(TAG, 'resolve ' + DYG_ON_GET_BATTERY); const { value } = deviceInfo; - resolve(percent); + resolve(value); } }); return this._mixTimeoutPromise(devicePromise); @@ -637,7 +639,7 @@ export default class RNDYGlucoseDriver { }); } - _commadTimeInterval(interval) { + _commadTimeIntervalWithTimer(interval) { return new Promise((resolve, reject) => { setTimeout(() => { resolve('command await time out'); @@ -645,6 +647,30 @@ export default class RNDYGlucoseDriver { }); } + // 阻塞方法,模拟 sleep 功能 + _blockSleep(milliseconds) { + return new Promise(resolve => { + const start = new Date().getTime(); + let current = start; + while (current - start < milliseconds) { + current = new Date().getTime(); + } + resolve(); + }); + } + + _commadTimeInterval(interval) { + if (Platform.OS === 'android' && AppState.currentState !== 'active') { + // 如果在后台运行,使用阻塞方法 + console.log(TAG,'start blocker'); + return this._blockSleep(interval); + } else { + // 如果在前台运行,使用 setTimeout + console.log(TAG,'start timer'); + return this._commadTimeIntervalWithTimer(interval); + } + } + _registerEvent(eventName) { this._eventMap[eventName] = (deviceInfo) => { @@ -670,6 +696,8 @@ export default class RNDYGlucoseDriver { this._eventMap[eventName] = null; } + + /** 监控底层驱动发送的消息 */ startMonitorDriverEvent() { this._createChannel = (deviceInfo) => {