flutter_bluetooth_serial icon indicating copy to clipboard operation
flutter_bluetooth_serial copied to clipboard

The first time startdiscovery is called, some discovery results are lost

Open ncdm-stldr opened this issue 4 years ago • 2 comments

Problem summary

When i use FlutterBluetoothSerial.instance.startDiscovery().listen((bluetoothDiscoveryResult) {//...}); for the first time, I see that the plugin logs some discovered devices, which are not transmitted to the stream i am listening below. If I restart the discovery it is fine then.

Steps to reproduce

To reproduce this issue, simply call FlutterBluetoothSerial.instance.startDiscovery().listen((bluetoothDiscoveryResult) {//...}); and print the results device mac address. Then compare what you see with the plugin logs.

Environment

I use -Flutter Channel stable, 2.5.3 -Android SDK version 31.0.0

Potential cause

I tried swapping this two blocks of code in FlutterBluetoothSerial from the library:

await _methodChannel.invokeMethod('startDiscovery');

subscription = _discoveryChannel.receiveBroadcastStream().listen(
          controller.add,
          onError: controller.addError,
          onDone: controller.close,
        );

like so:

subscription = _discoveryChannel.receiveBroadcastStream().listen(
          controller.add,
          onError: controller.addError,
          onDone: controller.close,
        );

await _methodChannel.invokeMethod('startDiscovery');

It seems to work for my problem but I am unsure if this is correct/safe. I tried this change because receiveBroadcastStream() returns a broacast stream which will loose its data if no one listens to it!

ncdm-stldr avatar Nov 04 '21 14:11 ncdm-stldr

Note that the missing devices problem is even seen with the example application (at least in my case using my phone).

ncdm-stldr avatar Jan 05 '22 17:01 ncdm-stldr

I have the same problem - even in example application there is no discovered devices...

Logos7 avatar Jul 25 '22 13:07 Logos7