edufolly / edufolly/flutter_bluetooth_serial

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

Open
#151 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
517
Forks
662
PR merge metrics
No merged PRs in 30d

Description

### 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!

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in the FlutterBluetoothSerial startDiscovery implementation, focusing on the ordering of receiveBroadcastStream().listen and invokeMethod('startDiscovery'). Reproduce the first-discovery case from the issue and compare stream results with plugin logs; done when the initial discovery delivers all logged devices without breaking later discoveries.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.