import 'dart:collection'; import 'package:flutter/material.dart'; import 'dart:async'; import 'package:get/get.dart'; import 'package:sensor/sensor.dart' as sensor; import 'dart:math' as math; void main() { Get.put(MainController()); runApp(const MyApp()); } class MainController extends GetxController { final orientation = sensor.Orientation().obs; final scanResults = [].obs; final connectedResults = [].obs; final hrm = sensor.HeartRateMeasurement().obs; final position = sensor.Position().obs; @override void onInit() { super.onInit(); orientation.bindStream(sensor.Sensor.orientationStream); scanResults.bindStream(sensor.Sensor.sportWearScanResultStream); connectedResults.bindStream(sensor.Sensor.sportWearConnectedStream); hrm.bindStream(sensor.Sensor.sportWearHeartRateMeasurementStream); position.bindStream(sensor.Sensor.locationStream); } void scanStart() async { await sensor.Sensor.sportWearScanStart(); } void scanStop() { sensor.Sensor.api.sportWearScanStop(); } void askEnableBT() async { var r = await sensor.Sensor.api.askEnableBluetooth(); } void connect(sensor.SportWear wear) async { await sensor.Sensor.sportWearConnect(wear); print('点击连接'); } void getLocation()async{ var l = await sensor.Sensor.getCurrentPosition(force: true); print(l); } void locationStart(){ sensor.Sensor.locationStart(1.seconds, 0); } void locationStop(){ sensor.Sensor.api.locationStop(); } void locationService()async{ final r = await sensor.Sensor.api.isLocationServiceOpen(); print('位置服务:$r'); } } class MyApp extends GetView { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text('Plugin example app'), ), body: ListView( children: [ _Orientation(), const Divider(), _OrientationView(), const Divider(), _WearScan(), const Divider(), _Location(), const Divider(), _Location2(), const Divider(), ], ), ), ); } } double _degrees(double radians) { return 180 / math.pi * radians; } class _Orientation extends GetView { @override Widget build(BuildContext context) { return Obx(() { var v = controller.orientation.value; return Column( children: [ const Text('orientation'), Text('x: ${_degrees(v.x)}'), Text('y: ${_degrees(v.y)}'), Text('z: ${_degrees(v.z)}'), ], ); }); } } class _OrientationView extends GetView { @override Widget build(BuildContext context) { return Obx(() { var v = controller.orientation.value; return Stack( alignment: Alignment.center, children: [ Image.asset( 'assets/images/im_compass.png', height: 240, width: 240, ), Image.asset( 'assets/images/im_compass_next_arrow.png', height: 150, width: 150, ), Transform.rotate( angle: -v.z, child: Image.asset( 'assets/images/im_compass_arrow.png', height: 220, width: 220, ), ), ], ); }); } } class _WearScan extends GetView { @override Widget build(BuildContext context) { return Obx(() { var children = [ const Text('扫描'), Row( children: [ OutlinedButton( onPressed: controller.askEnableBT, child: const Text('请求开启')), OutlinedButton( onPressed: controller.scanStart, child: const Text('扫描')), OutlinedButton( onPressed: controller.scanStop, child: const Text('停止')), ], ), ]; var list = controller.scanResults; var connected = controller.connectedResults; var cm = HashMap(); for(var one in connected){ cm[one.address] = true; } var l = list .map((element) { var isConnected = cm[element.address]!= null; return TextButton( onPressed: () => controller.connect(element), child: Text( '${element.name} [${element.address}] rssi: ${element.rssi}' ' ${isConnected?'已连接':''}'));}) .toList(); children.addAll(l); if(connected.isNotEmpty){ final device = connected.first; final hrm = controller.hrm.value; children.add( Text('hr: ${hrm.heartRate} is: ${hrm.contactDetected} rr: ${hrm.rrIntervals.length} ts: ${hrm.timestamp.second}') ); } return Column( children: children, ); }); } } class _Location extends GetView { @override Widget build(BuildContext context) { return OutlinedButton(onPressed: controller.getLocation, child: const Text('定位')); } } class _Location2 extends GetView { @override Widget build(BuildContext context) { return Column( children: [ Row( children: [ OutlinedButton(onPressed: controller.locationStart, child: const Text('定位开')), OutlinedButton(onPressed: controller.locationStop, child: const Text('定位关')), OutlinedButton(onPressed: controller.locationService, child: const Text('判断服务')), ], ), Obx((){ final p = controller.position.value; return Text('la: ${p.latitude} lo: ${p.longitude} al: ${p.altitude.toStringAsFixed(1)}\n' 'bearing: ${p.bearing} speed: ${p.speed} ac: ${p.accuracy}'); }) ], ) ; } }