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:
I am developing for android with flutter. Any help would be appreciated!
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.