veepoo_sdk 0.0.1
veepoo_sdk: ^0.0.1 copied to clipboard

Veepoo SDK for flutter

example/lib/main.dart

import 'dart:async';
import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:veepoo_sdk/model/ecg_data.dart';
import 'package:veepoo_sdk/model/hr_data.dart';
import 'package:veepoo_sdk/model/origin_v3_data.dart';
import 'package:veepoo_sdk/model/search_result.dart';
import 'package:veepoo_sdk/model/sleep_data.dart';
import 'package:veepoo_sdk/model/spo2h_data.dart';
import 'package:veepoo_sdk/model/spo2h_origin_data.dart';
import 'package:veepoo_sdk/veepoo_sdk.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  List<SearchResult> listDevice = new List<SearchResult>();
  bool isScanning = false;
  int connectStatus = 0;

  String _hrValue = "HR: N/a";
  String _spo2Value = "SPO2: N/a";
  String _process = "Loading: --";

  @override
  void initState() {
    super.initState();

    VeepooSdk.eventChannel()
        .receiveBroadcastStream()
        .listen(_onEvent, onError: _onError);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(
        title: const Text('Veepoo SDK example'),
      ),
      body: Column(
        children: <Widget>[
          buildConnectStatus(context),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('ReScan'),
                onPressed: _onRefresh,
              ),
              RaisedButton(
                child: Text('QConnect'),
                onPressed: _quickConnect,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('Disconnect'),
                onPressed: _disconnect,
              ),
              RaisedButton(
                child: Text('Auth'),
                onPressed: _bind,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('HR start'),
                onPressed: _startDetectHeart,
              ),
              RaisedButton(
                child: Text('HR stop'),
                onPressed: _stopDetectHeart,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('SPO2 start'),
                onPressed: _startDetectSPO2H,
              ),
              RaisedButton(
                child: Text('SPO2 stop'),
                onPressed: _stopDetectSPO2H,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('Set info'),
                onPressed: _syncPersonInfo,
              ),
              RaisedButton(
                child: Text('TEST'),
                onPressed: _sdkTest,
              ),
              RaisedButton(
                child: Text('readOriginData'),
                onPressed: _readOriginData,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('readSleepData'),
                onPressed: _readSleepData,
              ),
              RaisedButton(
                child: Text('readSpo2hOrigin'),
                onPressed: _readSpo2hOrigin,
              ),
            ],
          ),
          Row(
            children: <Widget>[
              RaisedButton(
                child: Text('t'),
                onPressed: _dummyTest,
              ),
            ],
          ),
          Text('Health data'),
          Text(_hrValue),
          Text(_spo2Value),
          Text(_process),
          Text('Device list'),
          new Expanded(
            child: createListView(context),
          )
        ],
      ),
    ));
  }

  Widget buildConnectStatus(BuildContext context) {
    if (connectStatus == 0) {
      return Text("Disconnected");
    } else if (connectStatus == 1) {
      return Text("Connected");
    } else {
      return Text("Connecting");
    }
  }

  Widget createListView(BuildContext context) {
    if (isScanning) {
      return ListTile(title: Text('Scanning device...'));
    }

    if (listDevice.length < 1) {
      return ListTile(title: Text('No device found'));
    }

    return new ListView.builder(
      itemCount: listDevice.length,
      itemBuilder: (BuildContext context, int index) {
        return new Column(
          children: <Widget>[
            new ListTile(
              title: new Text(
                  "${listDevice[index].name} - ${listDevice[index].mac} - ${listDevice[index].rssi}"),
              onTap: () => onTapped(index),
            ),
            new Divider(
              height: 2.0,
            ),
          ],
        );
      },
    );
  }

  Future<List<SearchResult>> startScanDevice() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      List<SearchResult> result = await VeepooSdk.scanDevice();

      setState(() {
        listDevice.addAll(result);
      });
    } on PlatformException {}

    setState(() {
      isScanning = false;
    });

    return listDevice;
  }

  void _onRefresh() {
    if (isScanning) {
      return;
    }

    setState(() {
      isScanning = true;
      listDevice.clear();
    });

    startScanDevice();
  }

  void onTapped(index) async {
    setState(() {
      connectStatus = 2;
    });

    bool isConnected = await VeepooSdk.connect(listDevice[index].mac);

    if (!isConnected) {
      setState(() {
        connectStatus = 0;
      });

      return;
    }

    await _bind();

    setState(() {
      connectStatus = 1;
    });
  }

  void _quickConnect() async {
//    bool status = await VeepooSdk.connect('F4:D1:24:B8:C5:48');
    bool status = await VeepooSdk.connect('F8:58:E7:56:7A:97');

    if (status) {
      _bind();
    }
  }

  void _disconnect() async {
    try {
      await VeepooSdk.disconnect();
    } catch (e) {
      print(e);
    }

    setState(() {
      connectStatus = 0;
    });
  }

  void _bind() {
    VeepooSdk.bind('0000', true);
  }

  void _startDetectHeart() async {
    await VeepooSdk.startDetectHeart();
  }

  void _stopDetectHeart() {
    VeepooSdk.stopDetectHeart();
  }

  void _startDetectSPO2H() async {
    await VeepooSdk.startDetectSPO2H();
  }

  void _stopDetectSPO2H() {
    VeepooSdk.stopDetectSPO2H();
  }

  void _syncPersonInfo() {
    VeepooSdk.syncPersonInfo(true, 170, 60, 24, 5000);
  }

  void _readOriginData() async {
    OriginV3Data originV3Data = await VeepooSdk.readOrigin3Data();
    print('xxxxxxxxxxxxxxxxxxxxxxxxxxx');
    print(originV3Data.toJson());
    print(originV3Data.originData3s);
    print(originV3Data.hrvOriginData);
    print(originV3Data.spo2hOriginData);
    inspect(originV3Data);
//    List<OriginData> data = await VeepooSdk.readOriginData();
//
//    print(data.length);
//
//    data.forEach((element) {
//      print(element.toJson());
//    });
  }

  void _readSpo2hOrigin() async {
    List<Spo2hOriginData> data = await VeepooSdk.readSpo2hOrigin();

    data.forEach((element) {
      print(element.toJson());
    });
  }

  void _readSleepData() async {
    List<SleepData> sleepData = await VeepooSdk.readSleepData();

    print(sleepData);
    inspect(sleepData);
  }

  void _sdkTest() {
    VeepooSdk.sdkTest();
  }

  void _onEvent(Object event) {
    Map<String, dynamic> e = json.decode(event);

    String action = e["action"];

    if (action == "onSpO2HADataChange") {
      String rawJson = e["payload"];
      Spo2hData spo2hData = Spo2hData.fromJson(json.decode(rawJson));

      setState(() {
        _spo2Value = "SPO2: ${spo2hData.value}";
      });
    }
    if (action == "onHrDataChange") {
      String rawJson = e["payload"];
      HrData hrData = HrData.fromJson(json.decode(rawJson));

      setState(() {
        _hrValue = "HR: ${hrData.data}";
      });
    }
    if (action == "onReadOriginProgress") {
      String raw = e["payload"];

      setState(() {
        _process = "Process: ${raw}";
      });
    }
  }

  void _onError(Object error) {
    print(error);
  }

  void _dummyTest() async {
    List<EcgDetectResult> list = await VeepooSdk.readECGData();
    print(list);
    inspect(list);
//    VeepooSdk.dummyTest();
  }
}
0
likes
70
pub points
0%
popularity

Veepoo SDK for flutter

Homepage (insecure)

Documentation

API reference

License

unknown (LICENSE)

Dependencies

flutter, json_annotation

More

Packages that depend on veepoo_sdk