main.dart 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import 'dart:collection';
  2. import 'package:flutter/material.dart';
  3. import 'dart:async';
  4. import 'package:get/get.dart';
  5. import 'package:sensor/sensor.dart' as sensor;
  6. import 'dart:math' as math;
  7. void main() {
  8. Get.put(MainController());
  9. runApp(const MyApp());
  10. }
  11. class MainController extends GetxController {
  12. final orientation = sensor.Orientation().obs;
  13. final scanResults = <sensor.SportWear>[].obs;
  14. final connectedResults = <sensor.SportWear>[].obs;
  15. final hrm = sensor.HeartRateMeasurement().obs;
  16. final position = sensor.Position().obs;
  17. @override
  18. void onInit() {
  19. super.onInit();
  20. orientation.bindStream(sensor.Sensor.orientationStream);
  21. scanResults.bindStream(sensor.Sensor.sportWearScanResultStream);
  22. connectedResults.bindStream(sensor.Sensor.sportWearConnectedStream);
  23. hrm.bindStream(sensor.Sensor.sportWearHeartRateMeasurementStream);
  24. position.bindStream(sensor.Sensor.locationStream);
  25. }
  26. void scanStart() async {
  27. await sensor.Sensor.sportWearScanStart();
  28. }
  29. void scanStop() {
  30. sensor.Sensor.api.sportWearScanStop();
  31. }
  32. void askEnableBT() async {
  33. var r = await sensor.Sensor.api.askEnableBluetooth();
  34. }
  35. void connect(sensor.SportWear wear) async {
  36. await sensor.Sensor.sportWearConnect(wear);
  37. print('点击连接');
  38. }
  39. void getLocation()async{
  40. var l = await sensor.Sensor.getCurrentPosition(force: true);
  41. print(l);
  42. }
  43. void locationStart(){
  44. sensor.Sensor.locationStart(1.seconds, 0);
  45. }
  46. void locationStop(){
  47. sensor.Sensor.api.locationStop();
  48. }
  49. void locationService()async{
  50. final r = await sensor.Sensor.api.isLocationServiceOpen();
  51. print('位置服务:$r');
  52. }
  53. }
  54. class MyApp extends GetView<MainController> {
  55. const MyApp({super.key});
  56. @override
  57. Widget build(BuildContext context) {
  58. return MaterialApp(
  59. home: Scaffold(
  60. appBar: AppBar(
  61. title: const Text('Plugin example app'),
  62. ),
  63. body: ListView(
  64. children: [
  65. _Orientation(),
  66. const Divider(),
  67. _OrientationView(),
  68. const Divider(),
  69. _WearScan(),
  70. const Divider(),
  71. _Location(),
  72. const Divider(),
  73. _Location2(),
  74. const Divider(),
  75. ],
  76. ),
  77. ),
  78. );
  79. }
  80. }
  81. double _degrees(double radians) {
  82. return 180 / math.pi * radians;
  83. }
  84. class _Orientation extends GetView<MainController> {
  85. @override
  86. Widget build(BuildContext context) {
  87. return Obx(() {
  88. var v = controller.orientation.value;
  89. return Column(
  90. children: [
  91. const Text('orientation'),
  92. Text('x: ${_degrees(v.x)}'),
  93. Text('y: ${_degrees(v.y)}'),
  94. Text('z: ${_degrees(v.z)}'),
  95. ],
  96. );
  97. });
  98. }
  99. }
  100. class _OrientationView extends GetView<MainController> {
  101. @override
  102. Widget build(BuildContext context) {
  103. return Obx(() {
  104. var v = controller.orientation.value;
  105. return Stack(
  106. alignment: Alignment.center,
  107. children: [
  108. Image.asset(
  109. 'assets/images/im_compass.png',
  110. height: 240,
  111. width: 240,
  112. ),
  113. Image.asset(
  114. 'assets/images/im_compass_next_arrow.png',
  115. height: 150,
  116. width: 150,
  117. ),
  118. Transform.rotate(
  119. angle: -v.z,
  120. child:
  121. Image.asset(
  122. 'assets/images/im_compass_arrow.png',
  123. height: 220,
  124. width: 220,
  125. ),
  126. ),
  127. ],
  128. );
  129. });
  130. }
  131. }
  132. class _WearScan extends GetView<MainController> {
  133. @override
  134. Widget build(BuildContext context) {
  135. return Obx(() {
  136. var children = <Widget>[
  137. const Text('扫描'),
  138. Row(
  139. children: [
  140. OutlinedButton(
  141. onPressed: controller.askEnableBT, child: const Text('请求开启')),
  142. OutlinedButton(
  143. onPressed: controller.scanStart, child: const Text('扫描')),
  144. OutlinedButton(
  145. onPressed: controller.scanStop, child: const Text('停止')),
  146. ],
  147. ),
  148. ];
  149. var list = controller.scanResults;
  150. var connected = controller.connectedResults;
  151. var cm = HashMap<String, bool>();
  152. for(var one in connected){
  153. cm[one.address] = true;
  154. }
  155. var l = list
  156. .map((element) {
  157. var isConnected = cm[element.address]!= null;
  158. return TextButton(
  159. onPressed: () => controller.connect(element),
  160. child: Text(
  161. '${element.name} [${element.address}] rssi: ${element.rssi}'
  162. ' ${isConnected?'已连接':''}'));})
  163. .toList();
  164. children.addAll(l);
  165. if(connected.isNotEmpty){
  166. final device = connected.first;
  167. final hrm = controller.hrm.value;
  168. children.add(
  169. Text('hr: ${hrm.heartRate} is: ${hrm.contactDetected} rr: ${hrm.rrIntervals.length} ts: ${hrm.timestamp.second}')
  170. );
  171. }
  172. return Column(
  173. children: children,
  174. );
  175. });
  176. }
  177. }
  178. class _Location extends GetView<MainController> {
  179. @override
  180. Widget build(BuildContext context) {
  181. return OutlinedButton(onPressed: controller.getLocation, child: const Text('定位'));
  182. }
  183. }
  184. class _Location2 extends GetView<MainController> {
  185. @override
  186. Widget build(BuildContext context) {
  187. return Column(
  188. children: [
  189. Row(
  190. children: [
  191. OutlinedButton(onPressed: controller.locationStart, child: const Text('定位开')),
  192. OutlinedButton(onPressed: controller.locationStop, child: const Text('定位关')),
  193. OutlinedButton(onPressed: controller.locationService, child: const Text('判断服务')),
  194. ],
  195. ),
  196. Obx((){
  197. final p = controller.position.value;
  198. return Text('la: ${p.latitude} lo: ${p.longitude} al: ${p.altitude.toStringAsFixed(1)}\n'
  199. 'bearing: ${p.bearing} speed: ${p.speed} ac: ${p.accuracy}');
  200. })
  201. ],
  202. ) ;
  203. }
  204. }