如何在颤动中每秒获得 100 个加速度计值

how to get 100 accelerometer values per sec in flutter

我是 flutter 的新手,我正在编写一个应用程序,它使用 UDP 将加速度计的值从我的手机流式传输到 pc,它正在工作,但我每秒获得大约 5 到 10 个值我想要至少 100 个加速度计值我服务器上的秒是我写的代码 https://github.com/USAMAWIZARD/send-acc-with-udp-flutter 我应该在我的代码中优化什么来获取值。

import 'dart:convert';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:sensors/sensors.dart';
import 'package:easy_udp/easy_udp.dart';
import 'package:wifi/wifi.dart';

void main() {
  runApp(MaterialApp(home: Home()));
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextEditingController ipaddress = new TextEditingController();
  TextEditingController portno = new TextEditingController();
  String srcip;
  int desport;
  String desip;
  var socket;
  int i = 0;

  void initializer() async {
    print("values initialized");
    desip = ipaddress.text;
    srcip = await Wifi.ip;
    desport = int.parse(portno.text);
    socket = await EasyUDPSocket.bind(srcip, 8000);
  }

  void init() async {
    await initializer();
    accelerometerEvents.listen((AccelerometerEvent event) async {
      await socket.send(
          ascii.encode(event.toString() + i.toString()), desip, desport);
      // await socket.send(ascii.encode(i.toString()), desip, desport);

      print(i);
      i += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('send udp data'),
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TextField(
            controller: ipaddress,
            textAlign: TextAlign.left,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: 'IP Address',
              hintStyle: TextStyle(color: Colors.grey),
            ),
          ),
          TextField(
            controller: portno,
            textAlign: TextAlign.left,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: 'Port NO',
              hintStyle: TextStyle(color: Colors.grey),
            ),
          ),
          TextButton(
              child: Text("start sending"),
              onPressed: () async => {
                    init(),
                  })
        ],
      ),
    );
  }
}

我最终使用了一个不同的库,它提供了一个名为 interval 的参数,我将该参数设置为 Sensors.SENSOR_DELAY_FASTEST,它每秒给我大约 100 个值

import 'dart:convert';
import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_sensors/flutter_sensors.dart';
import 'package:easy_udp/easy_udp.dart';
import 'package:wifi/wifi.dart';

void main() {
  runApp(MaterialApp(home: Home()));
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  TextEditingController ipaddress = new TextEditingController();
  TextEditingController portno = new TextEditingController();
  String srcip;
  int desport;
  String desip;
  var socket;
  int i = 0;
  var stream;
  void initializer() async {
    print("values initialized");
    desip = ipaddress.text;
    srcip = await Wifi.ip;
    desport = int.parse(portno.text);
    socket = await EasyUDPSocket.bind(srcip, 8000);

    stream = await SensorManager().sensorUpdates(
      sensorId: Sensors.ACCELEROMETER,
      interval: Sensors.SENSOR_DELAY_FASTEST,
    );
  }

  void init() async {
    await initializer();
    stream.listen((sensorEvent) async {
      await socket.send(
          ascii.encode(sensorEvent.datax.toString() + i.toString()),
          desip,
          desport);
      // await socket.send(ascii.encode(i.toString()), desip, desport);

      print(i);
      i += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('send udp data'),
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          TextField(
            controller: ipaddress,
            textAlign: TextAlign.left,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: 'IP Address',
              hintStyle: TextStyle(color: Colors.grey),
            ),
          ),
          TextField(
            controller: portno,
            textAlign: TextAlign.left,
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: 'Port NO',
              hintStyle: TextStyle(color: Colors.grey),
            ),
          ),
          TextButton(
              child: Text("start sending"),
              onPressed: () async => {
                    init(),
                  })
        ],
      ),
    );
  }
}