diff --git a/BrickController2/BrickController2/UI/Pages/DevicePage.xaml b/BrickController2/BrickController2/UI/Pages/DevicePage.xaml
index 8faff9f5..e9133929 100644
--- a/BrickController2/BrickController2/UI/Pages/DevicePage.xaml
+++ b/BrickController2/BrickController2/UI/Pages/DevicePage.xaml
@@ -129,6 +129,8 @@
+
+
diff --git a/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs b/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs
index 32140b7d..da50a211 100644
--- a/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs
+++ b/BrickController2/BrickController2/UI/ViewModels/DevicePageViewModel.cs
@@ -46,15 +46,21 @@ public DevicePageViewModel(
RenameCommand = new SafeCommand(async () => await RenameDeviceAsync());
BuWizzOutputLevelChangedCommand = new SafeCommand(outputLevel => SetBuWizzOutputLevel(outputLevel));
BuWizz2OutputLevelChangedCommand = new SafeCommand(outputLevel => SetBuWizzOutputLevel(outputLevel));
+ ScanCommand = new SafeCommand(ScanAsync, () => CanExecuteScan);
}
public Device Device { get; }
public bool IsBuWizzDevice => Device.DeviceType == DeviceType.BuWizz;
public bool IsBuWizz2Device => Device.DeviceType == DeviceType.BuWizz2;
+ public bool CanBePowerSource => Device.CanBePowerSource;
+ public bool CanExecuteScan => Device.CanBePowerSource &&
+ Device.DeviceState == DeviceState.Connected &&
+ !_deviceManager.IsScanning;
public ICommand RenameCommand { get; }
public ICommand BuWizzOutputLevelChangedCommand { get; }
public ICommand BuWizz2OutputLevelChangedCommand { get; }
+ public ICommand ScanCommand { get; }
public int BuWizzOutputLevel { get; set; } = 1;
public int BuWizz2OutputLevel { get; set; } = 1;
@@ -214,6 +220,66 @@ await _dialogService.ShowMessageBoxAsync(
}
}
+ private async Task ScanAsync()
+ {
+ if (!_deviceManager.IsBluetoothOn)
+ {
+ await _dialogService.ShowMessageBoxAsync(
+ Translate("Warning"),
+ Translate("BluetoothIsTurnedOff"),
+ Translate("Ok"),
+ _disappearingTokenSource.Token);
+ }
+
+ var percent = 0;
+ var scanResult = true;
+ await _dialogService.ShowProgressDialogAsync(
+ true,
+ async (progressDialog, token) =>
+ {
+ if (!_isDisappearing)
+ {
+ using (var cts = new CancellationTokenSource())
+ using (_disappearingTokenSource.Token.Register(() => cts.Cancel()))
+ {
+ Task scanTask = null;
+ try
+ {
+ scanTask = _deviceManager.ScanAsync(cts.Token);
+
+ while (!token.IsCancellationRequested && percent <= 100 && !scanTask.IsCompleted)
+ {
+ progressDialog.Percent = percent;
+ await Task.Delay(100, token);
+ percent += 1;
+ }
+ }
+ catch (Exception)
+ { }
+
+ cts.Cancel();
+
+ if (scanTask != null)
+ {
+ scanResult = await scanTask;
+ }
+ }
+ }
+ },
+ Translate("Scanning"),
+ Translate("SearchingForDevices"),
+ Translate("Cancel"));
+
+ if (!scanResult && !_isDisappearing)
+ {
+ await _dialogService.ShowMessageBoxAsync(
+ Translate("Warning"),
+ Translate("ErrorDuringScanning"),
+ Translate("Ok"),
+ CancellationToken.None);
+ }
+ }
+
private void OnDeviceDisconnected(Device device)
{
}