| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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 = <sensor.SportWear>[].obs;
- final connectedResults = <sensor.SportWear>[].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<MainController> {
- 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<MainController> {
- @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<MainController> {
- @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<MainController> {
- @override
- Widget build(BuildContext context) {
- return Obx(() {
- var children = <Widget>[
- 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<String, bool>();
- 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<MainController> {
- @override
- Widget build(BuildContext context) {
- return OutlinedButton(onPressed: controller.getLocation, child: const Text('定位'));
- }
- }
- class _Location2 extends GetView<MainController> {
- @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}');
- })
- ],
- ) ;
- }
- }
|