310 lines
12 KiB
Dart
310 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
|
import 'package:gps_map_flowpoint/class/devicesSaved.dart';
|
|
import 'package:gps_map_flowpoint/deviceAdvertizinScan.dart';
|
|
import 'package:gps_map_flowpoint/deviceFlowMap.dart';
|
|
|
|
class DeviceSelection extends StatefulWidget {
|
|
const DeviceSelection({super.key, required this.title});
|
|
|
|
final String title;
|
|
|
|
@override
|
|
State<DeviceSelection> createState() => _DeviceSelectionState();
|
|
}
|
|
|
|
class _DeviceSelectionState extends State<DeviceSelection> {
|
|
List<ScanResult> scanResults = [];
|
|
TextEditingController tecSearch = TextEditingController();
|
|
bool isScanning = false;
|
|
|
|
List<DevicesSaved> devicesSaved = [];
|
|
|
|
// Initialize Bluetooth scanning and subscription in initState
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
restoreLocal().then((value) {
|
|
print(value.length);
|
|
setState(() {
|
|
devicesSaved = value;
|
|
});
|
|
});
|
|
FlutterBluePlus.isSupported.then((isSupported) {
|
|
if (!isSupported) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text("Bluetooth non activé"),
|
|
content: Text(
|
|
"Le Bluetooth n'est pas activé sur cet appareil. Veillez l'activer pour continuer."),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text("OK"),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
});
|
|
|
|
FlutterBluePlus.onScanResults.listen(
|
|
(results) {
|
|
setState(() {
|
|
scanResults = results;
|
|
});
|
|
},
|
|
onError: (e) => print(e),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Filtered list of devices
|
|
List<ScanResult> filteredDevices = scanResults
|
|
.where((result) => result.device.platformName
|
|
.toLowerCase()
|
|
.contains(tecSearch.text.toLowerCase()))
|
|
.toList();
|
|
|
|
//Saved devices list is connected ?
|
|
devicesSaved.forEach((element) {
|
|
element.connected = scanResults
|
|
.where((result) => result.device.remoteId.str == element.remoteId)
|
|
.length >
|
|
0;
|
|
});
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
backgroundColor: Colors.blue,
|
|
title: Text("Eagle Tr@cker", style: TextStyle(color: Colors.white)),
|
|
),
|
|
body: Column(
|
|
children: [
|
|
//List of devices saved in local storage by card
|
|
devicesSaved.length > 0
|
|
? Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text('Appareils sauvegardés',
|
|
style: TextStyle(
|
|
fontSize: 18.0, fontWeight: FontWeight.bold)),
|
|
),
|
|
ListView.builder(
|
|
shrinkWrap: true,
|
|
itemCount: devicesSaved.length,
|
|
itemBuilder: (context, index) {
|
|
DevicesSaved device = devicesSaved[index];
|
|
return Card(
|
|
child: ListTile(
|
|
title: Row(children: [
|
|
Icon(Icons.circle,
|
|
color: devicesSaved[index].connected
|
|
? Colors.green
|
|
: Colors.orange,
|
|
size: 10.0),
|
|
Text(device.deviceName)
|
|
]),
|
|
subtitle: Text(device.deviceAddress),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () async {
|
|
devicesSaved.removeAt(index);
|
|
await saveLocal(devicesSaved);
|
|
setState(() {
|
|
devicesSaved = devicesSaved;
|
|
});
|
|
},
|
|
child: Icon(Icons.delete)),
|
|
GestureDetector(
|
|
onTap: () {
|
|
//Stop scanning when a device is selected
|
|
FlutterBluePlus.stopScan();
|
|
setState(() {
|
|
isScanning = false;
|
|
});
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
DeviceAdvertizinScan(
|
|
deviceName: device.deviceName,
|
|
deviceAddress:
|
|
device.deviceAddress,
|
|
remoteId: device.remoteId,
|
|
)),
|
|
);
|
|
},
|
|
child: Icon(Icons.info_outline),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
//Stop scanning when a device is selected
|
|
FlutterBluePlus.stopScan();
|
|
setState(() {
|
|
isScanning = false;
|
|
});
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => DeviceFlowMap(
|
|
title: 'GPS Map Flowpoint',
|
|
deviceName: device.deviceName,
|
|
deviceAddress: device.deviceAddress,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Icon(Icons.map_outlined),
|
|
),
|
|
],
|
|
),
|
|
));
|
|
},
|
|
),
|
|
],
|
|
)
|
|
: Container(),
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text('Recherche d\' appareils BLE',
|
|
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: TextFormField(
|
|
controller: tecSearch,
|
|
decoration: InputDecoration(
|
|
hintText: 'Rechercher un appareil par nom',
|
|
prefixIcon: Icon(Icons.search),
|
|
),
|
|
onChanged: (value) {
|
|
setState(() {});
|
|
},
|
|
),
|
|
),
|
|
SizedBox(height: 16.0),
|
|
Expanded(
|
|
child: ListView.builder(
|
|
itemCount: filteredDevices.length,
|
|
itemBuilder: (context, index) {
|
|
ScanResult result = filteredDevices[index];
|
|
return ListTile(
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () async {
|
|
if (devicesSaved
|
|
.where((element) =>
|
|
element.deviceAddress ==
|
|
result.device.remoteId.str)
|
|
.length >
|
|
0) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content:
|
|
Text('Cet appareil est déjà sauvegardé'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
devicesSaved.add(DevicesSaved(
|
|
deviceName: result.device.platformName,
|
|
deviceAddress: result.device.remoteId.str,
|
|
remoteId: result.device.remoteId.str,
|
|
));
|
|
await saveLocal(devicesSaved);
|
|
setState(() {
|
|
devicesSaved = devicesSaved;
|
|
});
|
|
;
|
|
},
|
|
child: Icon(Icons.add),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
//Stop scanning when a device is selected
|
|
FlutterBluePlus.stopScan();
|
|
setState(() {
|
|
isScanning = false;
|
|
});
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => DeviceAdvertizinScan(
|
|
deviceName: result.device.platformName,
|
|
deviceAddress: result.device.remoteId.str,
|
|
remoteId: result.device.remoteId.str,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Icon(Icons.info_outline),
|
|
),
|
|
GestureDetector(
|
|
onTap: () {
|
|
//Stop scanning when a device is selected
|
|
FlutterBluePlus.stopScan();
|
|
setState(() {
|
|
isScanning = false;
|
|
});
|
|
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => DeviceFlowMap(
|
|
title: 'GPS Map Flowpoint',
|
|
deviceName: result.device.platformName,
|
|
deviceAddress: result.device.remoteId.str,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
child: Icon(Icons.map_outlined),
|
|
),
|
|
],
|
|
),
|
|
title: Row(children: [
|
|
Icon(Icons.circle, color: Colors.green, size: 10.0),
|
|
Text(result.device.platformName)
|
|
]),
|
|
subtitle: Text(result.device.remoteId.str),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
backgroundColor: Colors.blue,
|
|
onPressed: () {
|
|
print('Scanning');
|
|
|
|
setState(() {
|
|
isScanning = !isScanning;
|
|
if (isScanning) {
|
|
FlutterBluePlus.startScan();
|
|
} else {
|
|
FlutterBluePlus.stopScan();
|
|
}
|
|
});
|
|
},
|
|
child: Icon(isScanning ? Icons.stop : Icons.play_arrow,
|
|
color: Colors.white),
|
|
),
|
|
);
|
|
}
|
|
}
|