private async void StartGattServiceWatcher(BluetoothLEDeviceDisplay deviceInfoDisp) { //Get the Bluetooth address for filtering the watcher BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceInfoDisp.Id); string selector = "(" + GattDeviceService.GetDeviceSelectorFromUuid(IoServiceUuid) + ")" + " AND (System.DeviceInterface.Bluetooth.DeviceAddress:=\"" + bleDevice.BluetoothAddress.ToString("X") + "\")"; gattServiceWatcher = DeviceInformation.CreateWatcher(selector); // Hook up handlers for the watcher events before starting the watcher gattServiceWatcher.Added += new TypedEventHandler <DeviceWatcher, DeviceInformation>(async(watcher, deviceInfo) => { // If the selected device is a WeDo device, enable the controls await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { weDoIoService = await GattDeviceService.FromIdAsync(deviceInfo.Id); outputCommandCharacteristic = weDoIoService.GetCharacteristics(OutputCommandCharacteristicGuid)[0]; InitializeGattStreamServiceCharateristics(deviceInfoDisp); btnStop.IsEnabled = true; }); }); gattServiceWatcher.Updated += new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { //Do nothing }); }); gattServiceWatcher.EnumerationCompleted += new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { //Do nothing }); }); gattServiceWatcher.Removed += new TypedEventHandler <DeviceWatcher, DeviceInformationUpdate>(async(watcher, deviceInfoUpdate) => { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { //Do nothing }); }); gattServiceWatcher.Stopped += new TypedEventHandler <DeviceWatcher, Object>(async(watcher, obj) => { await this.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { //Do nothing }); }); gattServiceWatcher.Start(); }
private void UpdateButtons(BluetoothLEDeviceDisplay deviceInfoDisp) { btnStop.IsEnabled = false; //Stop any existing service watcher if (gattServiceWatcher != null) { StopGattServiceWatcher(); } //If there is a paired device selected, look for the WeDo service and enable controls if found if (deviceInfoDisp != null) { if (deviceInfoDisp.IsPaired == true) { StartGattServiceWatcher(deviceInfoDisp); } } }
private async void ResultsListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { resultsListView.IsEnabled = false; string val = resultsListView.SelectedValue.ToString(); deviceInfoDisp = ResultCollection.Where(r => r.Id == val).FirstOrDefault(); this.NotifyUser("Unpairing started. Please wait...", NotifyType.StatusMessage); DevicePairingKinds ceremoniesSelected = DevicePairingKinds.ConfirmOnly; // ProtectionLevelSelectorInfo protectionLevelInfo = (ProtectionLevelSelectorInfo)protectionLevelComboBox.SelectedItem; DevicePairingProtectionLevel protectionLevel = DevicePairingProtectionLevel.Default; DeviceInformationCustomPairing customPairing = deviceInfoDisp.DeviceInformation.Pairing.Custom; Debug.WriteLine("Is Paired -- " + deviceInfoDisp.IsPaired); customPairing.PairingRequested += PairingRequestedHandler; DevicePairingResult result = await customPairing.PairAsync(ceremoniesSelected, protectionLevel); customPairing.PairingRequested -= PairingRequestedHandler; if (result.Status == DevicePairingResultStatus.Paired || result.Status == DevicePairingResultStatus.AlreadyPaired) { Debug.WriteLine("Paired.."); } deviceInfoDisp = ResultCollection.Where(r => r.Id == val).FirstOrDefault(); Debug.WriteLine("Is Paired -- " + deviceInfoDisp.IsPaired); //this.NotifyUser( // "Unpairing result = " + result.Status.ToString(), // result.Status == DeviceUnpairingResultStatus.Unpaired ? NotifyType.StatusMessage : NotifyType.ErrorMessage); UpdateButtons(deviceInfoDisp); resultsListView.IsEnabled = true; }