The first time startdiscovery is called, some discovery results are lost
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!
Note that the missing devices problem is even seen with the example application (at least in my case using my phone).
I have the same problem - even in example application there is no discovered devices...