0

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();
 }
5
  • 1
    Please share the full code Commented 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. Commented 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. Commented 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(); } Commented 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. Commented Feb 6, 2025 at 16:29

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.