/// <summary> /// Invoked when this page is about to be displayed in a Frame. /// </summary> /// <param name="e">Event data that describes how this page was reached. /// This parameter is typically used to configure the page.</param> protected override async void OnNavigatedTo(NavigationEventArgs e) { // Request the right to have background tasks run in the future. This need only be done once // after the app is installed, but it is harmless to do it every time the app is launched. if (await BackgroundExecutiondManager.RequestAccessAsync() == BackgroundAccessStatus.Denied) { // TODO: What? } // Acquire the set of background tasks that we already have registered. Store them into a dictionary, keyed // by task name. (For each LE device, we will use a task name that is derived from its Bluetooth address). Dictionary <string, BackgroundTaskRegistration> taskRegistrations = new Dictionary <string, BackgroundTaskRegistration>(); foreach (BackgroundTaskRegistration reg in BackgroundTaskRegistration.AllTasks.Values) { taskRegistrations[reg.Name] = reg; } // Get the list of paired Bluetooth LE devicdes, and add them to our 'devices' list. Associate each device with // its pre-existing registration if any, and remove that registration from our dictionary. Devices.Clear(); foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector())) { BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id); Racer device = new Racer(bleDevice); if (taskRegistrations.ContainsKey(device.TaskName)) { device.TaskRegistration = taskRegistrations[device.TaskName]; taskRegistrations.Remove(device.TaskName); } Devices.Add(device); } // Unregister any remaining background tasks that remain in our dictionary. These are tasks that we registered // for Bluetooth LE devices that have since been unpaired. foreach (BackgroundTaskRegistration reg in taskRegistrations.Values) { reg.Unregister(false); } }
/// <summary> /// Get all available BT devices. /// </summary> /// <exception cref="UnauthorizedAccessException">You have not set correct capabilities for your platform.</exception> /// <returns>Returns all available BT devices.</returns> public async override Task <List <DeviceInformation> > GetAllDevicesAsync() { var selector = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort); DeviceInformationCollection platformDevices; try { platformDevices = await PlatformDeviceInformation.FindAllAsync(selector); } catch (UnauthorizedAccessException) { if (Debugger.IsAttached) { // This exception likely occured becaused you have not added this to Package.appxmanifest: /* * <m2:DeviceCapability Name="bluetooth.rfcomm"> * <m2:Device Id="any"> * <m2:Function Type="name:serialPort" /> * </m2:Device> * </m2:DeviceCapability> */ Debugger.Break(); } throw; } var devices = new List <DeviceInformation>(); foreach (var platformDevice in platformDevices) { var device = new DeviceInformation(); device.DisplayName = platformDevice.Name; device.PlatformDeviceObject = platformDevice; devices.Add(device); } return(devices); }
private async void MainWindow_Loaded(object sender, RoutedEventArgs ea) { setLanguageList(); bool hasCamera = await setCameraListAsync(); if (hasCamera) { await InitializeMediaCaptureAsync(); setBrightnessControl(); initializeTimer(); } return; #region local functions in MainWindow_Loaded void setLanguageList() { IReadOnlyList <UwpLanguage> langList = UwpOcrEngine.AvailableRecognizerLanguages; this.LangComboBox.ItemsSource = langList; this.LangComboBox.DisplayMemberPath = nameof(UwpLanguage.DisplayName); this.LangComboBox.SelectedValuePath = nameof(UwpLanguage.LanguageTag); var ocrEngine = UwpOcrEngine.TryCreateFromUserProfileLanguages(); this.LangComboBox.SelectedValue = ocrEngine.RecognizerLanguage.LanguageTag; } async Task <bool> setCameraListAsync() { var devices = await UwpDeviceInformation.FindAllAsync(UwpDeviceClass.VideoCapture); if (devices.Count == 0) { hideCameraUI(); return(false); } setupCameraComboBox(devices); return(true); void hideCameraUI() { this.CameraLabel.Visibility = Visibility.Collapsed; this.CameraComboBox.Visibility = Visibility.Collapsed; this.MonitorCameraButton.Visibility = Visibility.Collapsed; this.CameraControlGrid.Visibility = Visibility.Collapsed; this.OcrCameraButtan.Visibility = Visibility.Collapsed; } void setupCameraComboBox(IReadOnlyList <UwpDeviceInformation> deviceList) { this.CameraComboBox.ItemsSource = deviceList; this.CameraComboBox.DisplayMemberPath = nameof(UwpDeviceInformation.Name); this.CameraComboBox.SelectedValuePath = nameof(UwpDeviceInformation.Id); this.CameraComboBox.SelectedIndex = 0; } } void setBrightnessControl() { // [明るさ] スライダーを設定 var brightCtl = _mediaCapture.BrightnessControl; this.BrightSlider.Minimum = brightCtl.Capabilities.Min; this.BrightSlider.Maximum = brightCtl.Capabilities.Max; if (brightCtl.TryGetValue(out double bright)) { this.BrightSlider.Value = bright; } // [コントラスト] スライダーを設定 var contrastCtl = _mediaCapture.ContrastControl; this.ContrastSlider.Minimum = contrastCtl.Capabilities.Min; this.ContrastSlider.Maximum = contrastCtl.Capabilities.Max; if (contrastCtl.TryGetValue(out double contrast)) { this.ContrastSlider.Value = contrast; } } void initializeTimer() { // CapturePhotoToStreamAsync() が、どうも毎秒1回程度しかキャプチャさせてくれないようだ。 // タイマー動作にする必要はなかったかもしれない。 _dispatcherTimer = new DispatcherTimer(DispatcherPriority.Normal) { Interval = TimeSpan.FromMilliseconds(333), }; _dispatcherTimer.Tick += new EventHandler(OnTimerTickAsync); _dispatcherTimer.Start(); } #endregion } // END MainWindow_Loaded