Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting rssi continously for scanResults? #23

Closed
watsmyname opened this issue Mar 10, 2022 · 3 comments
Closed

Getting rssi continously for scanResults? #23

watsmyname opened this issue Mar 10, 2022 · 3 comments

Comments

@watsmyname
Copy link

Actually this is more like a question than an issue.

What i need to do is scan for bluetooth devices, And I periodically need to update the list with new device, and for existing device, updating their corresponding rssi in the list. For this I use Timer to execute findBluetoothPeripherial(), every 40 seconds.
My Question, is there a better way of achieving this?

FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
List scanResults = <ScanResult>[];
Timer? timer;


@override
void initState(){
	super.initState();
	findBluetoothPeripherial();

	timer = Timer.periodic(Duration(seconds: 40), (Timer t) => findBluetoothPeripherial());
}


findBluetoothPeripherial(){
    //check and handle permissions
	flutterBlue.startScan(timeout: Duration(seconds: 20));

	var subscription = flutterBlue.scanResults.listen((results) {
    
	    for (ScanResult r in results) {
	        print('${r.device.name} found! rssi: ${r.rssi}');
	        int index = scanResults.indexWhere((item) => item.device.id == r.device.id);

	        if(index > 0){
	        	setState((){
		        	scanResults[index].rssi = r.rssi;
	        	});
	        }else {
	        	setState(() {
	        		scanResults.add(r);
	        	});
	        }
	    }
	});

	// Stop scanning
	flutterBlue.stopScan();
}

Another question

What is the difference between -

flutterBlue.scanResults.listen((results){
	//code here
});

And

flutterBlue.scan(timeout: Duration(seconds: 20))
        .distinct()
        .asBroadcastStream()
        .listen((results){
        		//code here
        });
@Mr-xzq
Copy link

Mr-xzq commented Apr 20, 2022

I also had this problem that the scan couldn't keep up with the latest devices around me.
My temporary solution is the same as yours, which is to restart the scan periodically.

@SaikCaskey
Copy link

SaikCaskey commented Aug 17, 2022

you could try to set allowDuplicates to true when you start the scan, it's not documented (i don't think), but allows duplicate scan results to be reported (i.e., > 1 for same device, and then continuous updates)

@chipweinberger
Copy link
Owner

yes that sounds good to me^

Please try 1.7.6+. reopen a new issue if you still have problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants