读取数据并单击停止按钮

时间:2019-09-25 14:04:41

标签: javascript react-native

我的代码有问题。

基本上,我从设备读取信息(例如加速度,压力,磁力计)。 每20毫秒就会读取大量数据。

在某个时候,我想中断此读数,因此我单击了停止按钮。 那里有问题。因此,停止按钮无法立即起作用(看起来好像没有被单击)。因此它完全保持静止,几秒钟后,停止按钮开始动作。

您认为此问题是由于什么原因造成的?

async setupNotifications1(device) {
    var timeagm = 0
    var time = 0
    const service = this.serviceGeneral();
    device.monitorCharacteristicForService(service,this.AccGyrMg, (error, characteristic) => {
        if (error)
        {
          this.error(error.message);
          return;
        }
        const buf = Buffer.from(characteristic.value, "base64");
        const [...acc_dx] = [2, 4, 6].map(index => buf.readInt16LE(index));
        this.setState(state => ({acc_dx,array_acc_dx: [...state.array_acc_dx,[timeagm, acc_dx]]
        }));
        // code about the data that I read in the same way of acc (acceleration)
      }
    );

stopConnection() {
        console.log("start stopConnection");
    Actions.registerattivita(
        {
        array_acc_dx: this.state.array_acc_dx,
        array_acc_sx: this.state.array_acc_sx,
        array_gyr_dx: this.state.array_gyr_dx,
        array_gyr_sx: this.state.array_gyr_sx,
        array_mg_dx: this.state.array_mg_dx,
        array_mg_sx: this.state.array_mg_sx,
        array_pressure_sx: this.state.array_pressure_sx,
        array_pressure_dx: this.state.array_pressure_dx,
        id: this.props.id
      }
      )
  }

  render() {
   return (
     <View>
        // ..code about the graphics of the page
      <View style={styles.button}>

      <TouchableOpacity style={[style.button, style.buttonOK]} onPress={() => this.stopConnection()}>
            <Text style={style.buttonTesto}>Stop</Text>
          </TouchableOpacity> 
      </View>
      </View>
    );
  }
}

页面Actions.registerattivita是我传递变量以将信息写入db的页面。 (如果需要,我也可以显示此页面)。但是在到达此页面之前,要经过几秒钟。

如果不清楚,我准备解释任何部分。非常感谢您的关注和帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试吗?

async stopConnection() {
        console.log("start stopConnection");
   await Actions.registerattivita(
        {
        array_acc_dx: this.state.array_acc_dx,
        array_acc_sx: this.state.array_acc_sx,
        array_gyr_dx: this.state.array_gyr_dx,
        array_gyr_sx: this.state.array_gyr_sx,
        array_mg_dx: this.state.array_mg_dx,
        array_mg_sx: this.state.array_mg_sx,
        array_pressure_sx: this.state.array_pressure_sx,
        array_pressure_dx: this.state.array_pressure_dx,
        id: this.props.id
      }
      )
  }