Code Monkey home page Code Monkey logo

Comments (10)

remonh87 avatar remonh87 commented on May 29, 2024 1

ah ok I see what is the issue now. You execute a stream subscription at the on build which I would try to avoid. It looks like a race condition on the Android layer. I would suggest to look to our example app. We execute the startscan on onTap but maintaining the subscription is done outside the build method. A simple solution can already be:

class Screen extends StatefulWidget {
  Screen();

  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {
  FlutterReactiveBle _ble = FlutterReactiveBle();

void startScan() {
   _ble.scanForDevices(withServices: [], scanMode: ScanMode.lowPower, requireLocationServicesEnabled: true).listen((event) {
     print(event.id);
     }).onError((handleError) {
       print(handleError);
    });
}
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              onPressed: startScan,
              child: Text("Scan"),
            ),
          ],
        ),
      ),
    );
  }
}

Also check our example app for a even cleaner approach

from flutter_reactive_ble.

Errichamonda avatar Errichamonda commented on May 29, 2024 1

Hi, I've tried the code that you sent me and I'm still getting the same error.
I've noticed that if I move from compileSDK 30 to 28 it works.

from flutter_reactive_ble.

remonh87 avatar remonh87 commented on May 29, 2024 1

That is intended. Before scanning you need to make sure that the status of your ble is set to Ready. See ble_status_monitor in the example app for a simple implementation. We do not handle automatic asking for permissions since it is up to the user to decide how they want to handle it. For example https://pub.dev/packages/permission_handler provides a good way of handling this.

from flutter_reactive_ble.

remonh87 avatar remonh87 commented on May 29, 2024

@Errichamonda thanks for reporting the issue. Can you supply me the code that you are using for calling the scanForDevices? Make sure that the parameter withServices has a value as documented in the code.

from flutter_reactive_ble.

Errichamonda avatar Errichamonda commented on May 29, 2024

It's a simple Stateful Widget.
Permission are granted to localization to always.

I've also tried to give the withServices parameter a List with one Uuid inside but I still got the same error.

class Screen extends StatefulWidget {
  Screen();

  @override
  _ScreenState createState() => _ScreenState();
}

class _ScreenState extends State<Screen> {
  FlutterReactiveBle _ble = FlutterReactiveBle();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.max,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RaisedButton(
              onPressed: () {
                _ble.scanForDevices(withServices: [], scanMode: ScanMode.lowPower, requireLocationServicesEnabled: true).listen((event) {
                  print(event.id);
                }).onError((handleError) {
                  print(handleError);
                });
              },
              child: Text("Scan"),
            ),
          ],
        ),
      ),
    );
  }
}

from flutter_reactive_ble.

remonh87 avatar remonh87 commented on May 29, 2024

That is very interesting... I updated the example app to compileSDK 30 and I have no issues. I use the exact same code as what I explained above and it works without raising an issue. I tested it both on Samsung and a google pixel as wel a galaxy tab. What I am thinking is it could be a device specific issue. Are you in the position of testing it on another android device? Also which version of Flutter are you using? I am using , v1.17.3

from flutter_reactive_ble.

Errichamonda avatar Errichamonda commented on May 29, 2024
Flutter (Channel beta, 1.19.0-4.3.pre, on Microsoft Windows [Versione 10.0.19041.329], locale it-IT)
    • Flutter version 1.19.0-4.3.pre at C:\flutter
    • Framework revision 8fe7655ed2 (12 days ago), 2020-07-01 14:31:18 -0700
    • Engine revision 9a28c3bcf4
    • Dart version 2.9.0 (build 2.9.0-14.1.beta)

I've tested on a OnePlus 6 with compileSDK 30 and it seems to work. Maybe it's something about Xiaomi software that causes the error. If I still encounter this error I'll reopen this issue. Thanks for the help.

Edit: it seems to be working properly now on Xiaomi device with compileSDK 30. I don't know what caused the issue.

from flutter_reactive_ble.

omarkamal avatar omarkamal commented on May 29, 2024

Hi, I'm having pretty much the same issue here. I was using a provider/get_it/mvvm architecture but I simplified it to the same code snipped shared above. I'm on Flutter 1.17.5 (the current stable).

I've tried this on 3 different Android devices, but no luck.

Here are my observations :

  1. If I leave the permission off (using the exact same code as above), the first button press says no location, the second button press says scanning parameters are not set
  2. If explicitly go to the app info and give it location permission, it works like a charm
  3. Even the example app compiled on my device throws the error in the image attached :

IMAGE 2020-07-30 21:18:49

Help?

from flutter_reactive_ble.

remonh87 avatar remonh87 commented on May 29, 2024

@omarkamal we require location permissions since this is the safest way of scanning. You can avoid this requirement by:

_ble.scanForDevices(withServices: serviceIds, requireLocationServicesEnabled: false) but this is at your own risk since Nexus devices need location services for scanning. I will reopen the issue because I am curious what happens on the second tap

from flutter_reactive_ble.

omarkamal avatar omarkamal commented on May 29, 2024

Hey, thanks for your response!

But that's the odd thing I noticed - even when I didn't explicitly set that flag, it never asked me for the location permission.

It was denied by default and I had to open app info and allow permissions there. Even setting the Bluetooth permissions on the project's own androidManifest didn't throw up a location prompt.

Tried this on a pixel 2 xl and an mi a2 both on Android 10.

I'll dig into the double button press a bit more to make sure it's nothing silly I'm doing on my end!

from flutter_reactive_ble.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.