Bluetooth e mapa a funcionar
This commit is contained in:
@@ -16,7 +16,7 @@ class BluetoothConnectionScreen extends StatefulWidget {
|
||||
class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
List<ScanResult> _scanResults = [];
|
||||
bool _isScanning = false;
|
||||
BluetoothDevice? _connectedDevice; // Track connected device
|
||||
BluetoothDevice? _connectedDevice;
|
||||
late StreamSubscription<List<ScanResult>> _scanResultsSubscription;
|
||||
late StreamSubscription<bool> _isScanningSubscription;
|
||||
|
||||
@@ -27,7 +27,7 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
_scanResultsSubscription = FlutterBluePlus.scanResults.listen((results) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
// FILTRO: Mantém apenas dispositivos que possuem um nome identificado
|
||||
// Filtra os dispositivos para mostrar apenas aqueles que possuem um nome identificado
|
||||
_scanResults = results.where((r) => r.device.platformName.isNotEmpty).toList();
|
||||
});
|
||||
}
|
||||
@@ -54,16 +54,30 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
Map<Permission, PermissionStatus> statuses = await [
|
||||
Permission.bluetoothScan,
|
||||
Permission.bluetoothConnect,
|
||||
Permission.bluetoothAdvertise,
|
||||
Permission.location,
|
||||
].request();
|
||||
|
||||
if (statuses[Permission.bluetoothScan]!.isGranted &&
|
||||
statuses[Permission.bluetoothConnect]!.isGranted) {
|
||||
bool allGranted = true;
|
||||
if (statuses[Permission.bluetoothScan]?.isDenied ?? true) allGranted = false;
|
||||
if (statuses[Permission.bluetoothConnect]?.isDenied ?? true) allGranted = false;
|
||||
|
||||
final bool scanPermanentlyDenied = statuses[Permission.bluetoothScan]?.isPermanentlyDenied ?? false;
|
||||
final bool connectPermanentlyDenied = statuses[Permission.bluetoothConnect]?.isPermanentlyDenied ?? false;
|
||||
|
||||
if (scanPermanentlyDenied || connectPermanentlyDenied) {
|
||||
if (mounted) {
|
||||
_showSettingsDialog();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (allGranted) {
|
||||
_startScan();
|
||||
} else {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
const SnackBar(
|
||||
content: Text(AppStrings.permissionsDenied),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
@@ -75,12 +89,35 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
void _showSettingsDialog() {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text("Permissões Necessárias"),
|
||||
content: const Text("As permissões de Bluetooth foram negadas permanentemente. Por favor, habilite-as nas configurações do sistema para continuar."),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text("CANCELAR"),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
openAppSettings();
|
||||
Navigator.pop(context);
|
||||
},
|
||||
child: const Text("CONFIGURAÇÕES"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _startScan() async {
|
||||
try {
|
||||
if (await FlutterBluePlus.adapterState.first != BluetoothAdapterState.on) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
const SnackBar(
|
||||
content: Text(AppStrings.turnOnBluetooth),
|
||||
behavior: SnackBarBehavior.floating,
|
||||
),
|
||||
@@ -89,7 +126,9 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
return;
|
||||
}
|
||||
|
||||
await FlutterBluePlus.startScan(timeout: const Duration(seconds: 15));
|
||||
await FlutterBluePlus.startScan(
|
||||
timeout: const Duration(seconds: 15),
|
||||
);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -135,7 +174,7 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
});
|
||||
FlutterBluePlus.stopScan();
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
const SnackBar(
|
||||
content: Text(AppStrings.connectedSuccess),
|
||||
backgroundColor: AppColors.success,
|
||||
behavior: SnackBarBehavior.floating,
|
||||
@@ -190,7 +229,6 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
Column(
|
||||
children: [
|
||||
const SizedBox(height: 20),
|
||||
// Header Card
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 25),
|
||||
child: Container(
|
||||
@@ -282,7 +320,6 @@ class _BluetoothConnectionScreenState extends State<BluetoothConnectionScreen> {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 30),
|
||||
// Device List
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: _connectedDevice != null ? 1 : _scanResults.length,
|
||||
|
||||
Reference in New Issue
Block a user