hiflex 0.0.11 copy "hiflex: ^0.0.11" to clipboard
hiflex: ^0.0.11 copied to clipboard

unlisted

HiFLEX Sensor Plugin

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:hiflex/hiflex_device.dart';
import 'package:hiflex/hiflex_manager.dart';
import 'package:hiflex/hiflex_sampling.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  double _sensorValue = 0.0;
  double _deltaTime = 0;
  Vector3 _acc = new Vector3(x: 0, y: 0, z: 0);
  Vector3 _mag = new Vector3(x: 0, y: 0, z: 0);
  Vector3 _gyr = new Vector3(x: 0, y: 0, z: 0);
  Quaternion _quaternion = Quaternion.identity;
  EulurAngle _angle = new EulurAngle(x: 0, y: 0, z: 0);
  int _batteryLevel = 0;

  HiflexManager _manager = new HiflexManager();
  HiflexDevice _device;
  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      platformVersion = await HiflexManager.platformVersion;
      print("$platformVersion");
      if (await _manager.init()) {
        print('HiflexManager Init Success');
      }
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Row(
                children: [
                  RaisedButton(
                    child: Text('Scan', style: TextStyle(fontSize: 24)),
                    onPressed: () async {
                      (await _manager.scanDevice()).listen((device) async {
                        if (await device.connect()) {
                          device.samplingStream()?.listen((event) {
                            setState(() {
                              _deltaTime = event.deltaTime;
                              _sensorValue = event.stretch;
                              _acc = event.acceleometer;
                              _gyr = event.gyroscope;
                              _mag = event.magnetometer;
                              _quaternion = event.quaternion;
                              _angle = event.eulurAngle;
                              _batteryLevel = device.batteryLevel;
                            });
                          });
                          device.connectionState.listen((event) {
                            print('connect state $event');
                            if (event == HiflexDeviceState.disconnected) {
                              device = null;
                            }
                          });
                        }
                      });
                    },
                    color: Colors.green,
                    textColor: Colors.white,
                  ),
                  RaisedButton(
                    child: Text('Connect', style: TextStyle(fontSize: 24)),
                    onPressed: () async {
                      _device = await _manager.findDevice();
                      if (_device != null) {
                        _device.samplingStream()?.listen((event) {
                          setState(() {
                            _deltaTime = event.deltaTime;
                            _sensorValue = event.stretch;
                            _acc = event.acceleometer;
                            _gyr = event.gyroscope;
                            _mag = event.magnetometer;
                            _quaternion = event.quaternion;
                            _angle = event.eulurAngle;
                            _batteryLevel = _device.batteryLevel;
                          });
                        });
                      }
                      _device.connectionState.listen((event) {
                        print('connect state $event');
                        if (event == HiflexDeviceState.disconnected) {
                          _device = null;
                        }
                      });
                    },
                    color: Colors.green,
                    textColor: Colors.white,
                  ),
                  RaisedButton(
                    child: Text('Disconnect', style: TextStyle(fontSize: 24)),
                    onPressed: () async {
                      if (_device != null) {
                        _device.disconnect();
                      }
                    },
                    color: Colors.green,
                    textColor: Colors.white,
                  ),
                ],
              ),
              Text('Running on: $_platformVersion'),
              Text('Sensor: ${_sensorValue.toInt()}'),
              Text('DeltaTime: $_deltaTime ms'),
              Text(
                  'Acc: ${_acc.x.toInt()}, ${_acc.y.toInt()}, ${_acc.z.toInt()}'),
              Text(
                  'Gyr: ${_gyr.x.toInt()}, ${_gyr.y.toInt()}, ${_gyr.z.toInt()}'),
              Text(
                  'Mag: ${_mag.x.toInt()}, ${_mag.y.toInt()}, ${_mag.z.toInt()}'),
              Text(
                  'Quaternion: ${_quaternion.w}, ${_quaternion.x}, ${_quaternion.y}, ${_quaternion.z}'),
              Text(
                  'EulerAngle: ${_angle.x.toInt()}, ${_angle.y.toInt()}, ${_angle.z.toInt()}'),
              Text('Battery Level: $_batteryLevel'),
            ],
          ),
        ),
      ),
    );
  }
}
0
likes
35
pub points
0%
popularity

Publisher

unverified uploader

HiFLEX Sensor Plugin

Homepage

License

LGPL-3.0 (LICENSE)

Dependencies

flutter, flutter_ble_lib, permission_handler

More

Packages that depend on hiflex