startDiscovery method
Implementation
@override
Future<Stream<List<BluetoothDevice>>?> startDiscovery({final Duration? duration})async{
if(!await requestBluetoothPermissions())return null;
methodChannel.invokeMethod("startDiscovery");
if( duration != null && ! await isAllServicesDisposed() ) {
Future.delayed(duration).then((_)async{
if( await isAllServicesDisposed())return;
cancelDiscovery();
});
}
if(_stream != null ) {
return _stream!.stream;
}
_stream = StreamController();
try {
devicesEventChannel.receiveBroadcastStream().listen((data){
if(data is! List)return;
lastSeenDevices.clear();
for(final dynamic data in data){
try {
final BluetoothDevice device = BluetoothDevice.fromParsed(data);
lastSeenDevices.add(device);
}catch(_){}
continue;
}
_stream!.sink.add(lastSeenDevices);
});
return _stream!.stream;
}
catch(e){
return null;
}
}