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) => {