public async Task FindControllers() { _deviceInformations.Clear(); _isLeftConnected = false; _isRightConnected = false; var task = new TaskCompletionSource <bool>(); var leftSelector = BluetoothDevice.GetDeviceSelectorFromDeviceName(LeftJoyConName); var rightSelector = BluetoothDevice.GetDeviceSelectorFromDeviceName(RightJoyConName); var selector = $"({leftSelector}) OR ({rightSelector})"; _deviceWatcher = DeviceInformation.CreateWatcher(selector, null, DeviceInformationKind.AssociationEndpoint); _deviceWatcher.Added += AddedDevice; _deviceWatcher.Updated += async(watcher, info) => { var device = _deviceInformations.FirstOrDefault(dev => dev.Id.Equals(info.Id)); if (device != null) { device.Update(info); await TryPair(device); } }; _deviceWatcher.EnumerationCompleted += (watcher, info) => task.SetResult(true); _deviceWatcher.Stopped += (watcher, info) => task.SetResult(true); _deviceWatcher.Start(); await task.Task; }
/// <summary> /// Obsługuje zdarzenie kliknięcia w przycisk 'Połącz'. Obsługa polega na sprawdzeniu załączenia modułu Bluetooth. /// W przypadku, gdy wynik sprawdzenia jest pozytywny wyświetlane jest menu wyboru urządzenia o nazwie "RobotPajak". /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void ButtonConnect_Click(object sender, RoutedEventArgs e) { if (labelStartInstruction.Visibility == Visibility.Visible) { labelStartInstruction.Visibility = Visibility.Collapsed; } var isBtActive = await Bluetooth.IsEnabled(); if (isBtActive) { var picker = new DevicePicker(); picker.Filter.SupportedDeviceSelectors.Add(BluetoothDevice.GetDeviceSelectorFromDeviceName("RobotPajak")); picker.Show(new Rect(10, 10, 300, 300)); picker.DeviceSelected += (pckr, args) => { _selectedDeviceHandler(pckr, args); }; } else { PopUp.Show(StringConsts.BtNotActiveError, StringConsts.Error); } }
public void When_GetSelector() { string testSelector; testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelector()); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromPairingState(true)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsPaired:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromPairingState(false)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#True OR " + _deviceSelectorIssueInquiry + "#False)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Connected)); testSelector = _deviceSelectorPrefix + "(System.Devices.Aep.IsConnected:=System.StructuredQueryType.Boolean#False OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromConnectionStatus(BluetoothConnectionStatus.Disconnected)); string deviceName = "TESTNAME"; testSelector = _deviceSelectorPrefix + "(System.ItemNameDisplay:=\"" + deviceName + "\" OR " + _deviceSelectorIssueInquiry + "#True)"; Assert.AreEqual(testSelector, BluetoothDevice.GetDeviceSelectorFromDeviceName(deviceName)); }