eagletracker/lib/deviceAdvertizinScan.dart
2024-07-08 23:03:37 +02:00

146 lines
4.8 KiB
Dart

import 'package:eagletracker/config.dart';
import 'package:eagletracker/function.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:eagletracker/class/BLEProvider.dart';
import 'package:provider/provider.dart';
/*
* Class for the device advertisement scan information screen
*/
class DeviceAdvertizinScan extends StatefulWidget {
const DeviceAdvertizinScan(
{super.key,
required this.deviceName,
required this.deviceAddress,
required this.remoteId});
final String deviceName;
final String deviceAddress;
final String remoteId;
@override
State<DeviceAdvertizinScan> createState() => _DeviceAdvertizinScanState();
}
class _DeviceAdvertizinScanState extends State<DeviceAdvertizinScan> {
Config config = Config();
List<ScanResult> scanResults = [];
double latitude = 0;
double longitude = 0;
double altitude = 0;
double speed = 0;
String value = 'Aucune valeur reçue';
TextEditingController tecTakingPoint = TextEditingController();
@override
Widget build(BuildContext context) {
//Get the BLE provider and the scan results
final bleProvider = Provider.of<BLEProvider>(context);
scanResults = bleProvider.scanResults;
//Check if the device is in the list of scanned devices, make sure the manufacturer data is not empty and the remoteId is the same as the one selected
if (scanResults
.where((element) =>
element.advertisementData.manufacturerData.isNotEmpty &&
element.device.remoteId.toString() == widget.remoteId)
.isNotEmpty) {
value = scanResults
.where((element) {
if (element.device.remoteId.toString() == widget.remoteId) {
if (element.advertisementData.manufacturerData[49406] != null) {
List<int> data =
element.advertisementData.manufacturerData[49406]!;
//Convert the data to hexadecimal
List<String> hexList =
data.map((int value) => value.toRadixString(16)).toList();
if (config.debug) {
print(data);
print(hexList);
}
//Get the values from the advertisement data
List<double> values = getValueFromAdvData(data);
latitude = values[0];
longitude = values[1];
altitude = values[2];
speed = values[3];
setState(() {});
}
}
return element.device.remoteId.toString() == widget.remoteId;
})
.map((e) => e.toString())
.first;
}
return Scaffold(
appBar: AppBar(
title: Text(config.projectName, style: TextStyle(color: Colors.white)),
backgroundColor: Colors.blue,
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.pop(context);
},
),
),
body: Column(
children: [
const Padding(
padding: EdgeInsets.all(16.0),
child: Text('Lecture de la trame publicitaire',
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
),
Container(
alignment: Alignment.topCenter,
child: Column(
children: [
Text(widget.deviceName),
Text(widget.deviceAddress),
Text(widget.remoteId),
],
)),
Container(
margin: const EdgeInsets.all(10),
child:
//Text centré
const Center(
child: Text(
"La trame publicitaire est en cours de réception et actualisée en temps réel.",
style: TextStyle(
color: Colors.green,
),
textAlign: TextAlign.center),
)),
Container(
margin: const EdgeInsets.all(10),
child: const CircularProgressIndicator()),
Container(
margin: const EdgeInsets.all(10),
alignment: Alignment.center,
child: Text(value,
style: const TextStyle(
color: Colors.black, fontWeight: FontWeight.bold))),
Container(
margin: const EdgeInsets.all(10),
alignment: Alignment.center,
child: Text(
"Valeurs de la longitude ($longitude), latitude ($latitude), altitude ($altitude) et vitesse ($speed)",
style: const TextStyle(
color: Colors.black, fontWeight: FontWeight.bold)))
],
),
);
}
}