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:
- The example runs on an older embedding of android (downgrade it gave me an error. Plugin Binding error, I think);
- I add a couple of permissions to my project to work properly with Bluetooth (Internet,Bluetooth,Bluetooth_Admin, and fine and course location);
- MinSdk is set to API 19;
- I tried to added a future.delayed and waited for it before moving on to my discovery function, same result;
- I disposed my streamSubscription (cancel it) on dispose and naturally I experiment soving thsi by restarting, re-run, debug and uninstall;
- I ran2 differently devices;
- 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.
- I think my app is in dev mode, although it was not set by me and I haven't tried to change that.
- 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!