Search code examples
flutterbluetoothandroid-bluetooth

Flutter Bluetooth Serial Discovery - canceling discovery (stream closed)


So I'm trying replicate the example on https://github.com/edufolly/flutter_bluetooth_serial/tree/master/example on my main app. but strangely, it behaves differently when integrated in my. I thought it could be a very easy and immediate thing but it turns out that it is giving much more trouble.

Basically the discovery doesn't found any device and prints the following:

D/FlutterBluePlugin(26807): Starting discovery
D/BluetoothAdapter(26807): startDiscovery
D/FlutterBluePlugin(26807): Canceling discovery (stream closed)
D/BluetoothAdapter(26807): cancelDiscovery
D/BluetoothAdapter(26807): cancelDiscovery = false

The code responsible for it and that behaves differently is :

//...

  StreamSubscription<BluetoothDiscoveryResult> _streamSubscription;

//...

  void _startDiscovery() {
    print(FlutterBluetoothSerial.instance.startDiscovery());

    _streamSubscription =
        FlutterBluetoothSerial.instance.startDiscovery().listen((r) {
      Provider.of<BluetoothProvider>(context, listen: false)
          .addFoundedDevice(r);
      print(r.device.name);
      print(r);
    });

    _streamSubscription.onDone(() {
      Provider.of<BluetoothProvider>(context, listen: false).isDiscovering =
          false;
    });
  }

//...

Debugging it showed me that it doesn't enter the listen function.

Printting FlutterBluetoothSerial.instance.startDiscovery() give us the following:

I/flutter (26807): Instance of '_ControllerStream<BluetoothDiscoveryResult>

I have been around this for a couple of days now. Things I notice/did:

  1. The example runs on an older embedding of android (downgrade it gave me an error. Plugin Binding error, I think);
  2. I add a couple of permissions to my project to work properly with Bluetooth (Internet,Bluetooth,Bluetooth_Admin, and fine and course location);
  3. MinSdk is set to API 19;
  4. I tried to added a future.delayed and waited for it before moving on to my discovery function, same result;
  5. I disposed my streamSubscription (cancel it) on dispose and naturally I experiment soving thsi by restarting, re-run, debug and uninstall;
  6. I ran2 differently devices;
  7. I had this problem months ago and I think the same code worked for 2 different people (devs, different pc and phone), but failed on my 2 phones and with my friend (also, different pc and phone). I know how I did it that time, same I don't know why it is not working now.
  8. I think my app is in dev mode, although it was not set by me and I haven't tried to change that.
  9. I might have done more things, but that is what I can remember now.

I am developing for android with flutter. Any help would be appreciated!


Solution

  • I have solved using this line before getting the current state:

    await FlutterBluetoothSerial.instance.cancelDiscovery();
    
    // Get current state
    FlutterBluetoothSerial.instance.state.then((state) { ...
    

    I don't know why, but after connecting the first time, if you don't disconnect, it doesn't allow to discover new devices the next time.