1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Asynchronus calls to get Bluetooth & Location Permission

Discussão em 'Mobile' iniciado por Stack, Outubro 17, 2024 às 03:03.

  1. Stack

    Stack Membro Participativo

    I'm a newbie to Flutter & am trying to get a simple functionality in my app where I want to check if the users Bluetooth & location are enabled before beginning any ops however I'm getting inconsistent results and black screens in the process, can someone explain in what I'm missing here -


    void findBleState() async {
    var locationStatus = await Permission.location.status;
    if (locationStatus.isDenied) {
    await Permission.location.request();
    }
    var locationServiceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!locationServiceEnabled) {
    await Geolocator.openLocationSettings();
    }

    FlutterBluePlus.adapterState.listen((BluetoothAdapterState state) async {
    if (state == BluetoothAdapterState.on && locationStatus.isGranted && locationServiceEnabled) {
    print('Bluetooth is active');
    print('Scanning Status - ${FlutterBluePlus.isScanningNow}');
    startScan();
    scanedDevices();
    } else if (state == BluetoothAdapterState.off) {
    await _handleBluetoothOff(locationServiceEnabled);
    }
    });
    }

    Future<void> _handleBluetoothOff(bool locationServiceEnabled) async {
    print('Bluetooth & location is not active');
    if (Platform.isAndroid) {
    try {
    await FlutterBluePlus.turnOn();
    print('Bluetooth & location turned on');
    } catch (e) {
    print('Error turning on Bluetooth: $e');
    }
    print('Bluetooth is now turned on!');
    }
    }


    When both Bluetooth & Location are disabled it tends to behave in an unpredictable manner where it at times prompts bluetooth permission and gets stuck or gets stuck on a blackscreen without any prompts.

    Thanks in advance.

    Continue reading...

Compartilhe esta Página