I have Bluetooth classic device and I have used bluetooth_classic package and succeeded on getting the device in scan.
but I am not able to stop the subscription which I started during scanning process.
and getting this error when I stop the scan and then again starting the scan second time: Stream has already been listened to.
late StreamSubscription<Device> _scanResultsSubscription;
_scanResultsSubscription = _bluetoothClassicPlugin.onDeviceDiscovered().listen(
(event) {
_discoveredDevices = [..._discoveredDevices, event];
},
);
@override
void dispose() {
_scanResultsSubscription.cancel();
super.dispose();
}
-
1Please share the full codePratik Lakhani– Pratik Lakhani2025年02月04日 15:25:07 +00:00Commented Feb 4, 2025 at 15:25
-
If you are just restarting the scan, it looks like the Bluetooth plugin is reusing the same stream so you don't need to cancel the stream subscription.Abion47– Abion472025年02月04日 18:10:52 +00:00Commented Feb 4, 2025 at 18:10
-
@Abion47 Yes in that case I will not need but if I am navigating back (stop the scan ,dispose will call) and again opening the same screen that time this subscription will start again, this time facing the error.Anjali Jagtap– Anjali Jagtap2025年02月06日 05:12:38 +00:00Commented Feb 6, 2025 at 5:12
-
@PratikLakhani Future<void> startScan() async { discoveredDevices.clear(); await bluetoothClassicPlugin.startScan(); } Future<void> stopScan() async { discoveredDevices.clear(); await bluetoothClassicPlugin.stopScan(); }Anjali Jagtap– Anjali Jagtap2025年02月06日 05:17:29 +00:00Commented Feb 6, 2025 at 5:17
-
Then you shouldn't be putting the Bluetooth service inside a widget. Put it in an external service class that you can call from any widget.Abion47– Abion472025年02月06日 16:29:20 +00:00Commented Feb 6, 2025 at 16:29
lang-dart