/// <summary> /// Event handler for arrival of devices /// </summary> /// <param name="sender"></param> /// <param name="devInterface">The device interface which was added</param> private void OnAdded(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation devInterface) { // search the device list for a device with a matching interface ID DeviceListEntry match = FindInList(devInterface.Id); // If we found a match then mark it as verified and return if (match != null) { if (match.Matched == false) { DeviceAdded(this, new UsbDeviceInfo(match)); } match.Matched = true; return; } // Create a new element for this device interface, and queue up the query of its // device information match = new DeviceListEntry(devInterface); // Add the new element to the end of the list of devices this.devices.Add(match); DeviceAdded(this, new UsbDeviceInfo(match)); }
/// <summary> /// Invoked when the device watcher finds a proximity sensor /// </summary> /// <param name="sender">The device watcher</param> /// <param name="device">Device information for the proximity sensor that was found</param> private void OnProximitySensorAdded(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation device) { if (_sensor == null && Windows.Devices.Sensors.ProximitySensor.FromId(device.Id) is Windows.Devices.Sensors.ProximitySensor foundSensor) { _sensor = foundSensor; } }
static public void SearchDevice() { // Additional properties we would like about the device. // Property strings are documented here https://msdn.microsoft.com/en-us/library/windows/desktop/ff521659(v=vs.85).aspx string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable", "System.Devices.Aep.AepId", "System.Devices.Aep.Category" }; // BT_Code: Example showing paired and non-paired in a single query. string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")"; deviceWatcher = DeviceInformation.CreateWatcher( aqsAllBluetoothLEDevices, requestedProperties, DeviceInformationKind.AssociationEndpoint); // Register event handlers before starting the watcher. deviceWatcher.Added += DeviceWatcher_Added; deviceWatcher.Updated += DeviceWatcher_Updated; deviceWatcher.Removed += DeviceWatcher_Removed; deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; deviceWatcher.Stopped += DeviceWatcher_Stopped; // Start the watcher. deviceWatcher.Start(); }
/// <summary> /// Event handler for arrival of Fx2 devices /// </summary> /// <param name="sender"></param> /// <param name="devInterface">The device interface which was added</param> private async void OnFx2Added(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation devInterface) { await MainPage.Current.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { MainPage.Current.NotifyUser(String.Format("OnFx2Added: {0}", devInterface.Id), NotifyType.StatusMessage); // search the device list for a device with a matching interface ID DeviceListEntry match = FindInList(devInterface.Id); // If we found a match then mark it as verified and return if (match != null) { match.Matched = true; return; } // Create a new element for this device interface, and queue up the query of its // device information match = new DeviceListEntry(devInterface); // Add the new element to the end of the list of devices m_Fx2Devices.Add(match); } ); }
/// <summary> /// Event handler for the end of an enumeration/reenumeration started /// by calling Start on the device watcher. This culls out any entries /// in the list which are no longer matched. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnEnumerationComplete(Windows.Devices.Enumeration.DeviceWatcher sender, object args) { DeviceListEntry removedDevice; while ((removedDevice = this.devices.FirstOrDefault(e => e.Matched == false)) != null) { this.devices.Remove(removedDevice); } }
private void InitDeviceWatcher() { // Create a device watcher to look for instances of the device interface this.watcher = Windows.Devices.Enumeration.DeviceInformation.CreateWatcher( this.deviceSelector, new string[] { "System.Devices.DeviceInstanceId" } ); this.watcher.Added += this.OnAdded; this.watcher.Removed += this.OnRemoved; this.watcher.EnumerationCompleted += this.OnEnumerationComplete; }
private async void ShadowDriverDeviceWatcher_Added(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformation args) { ShadowDriverDevice = await CustomDevice.FromIdAsync(args.Id, DeviceAccessMode.ReadWrite, DeviceSharingMode.Exclusive); if (ShadowDriverDevice != null) { await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ViewModel.DeviceConnectStatus = "Connected"; }); } }
/// <summary> /// Event handler for the removal of an device /// </summary> /// <param name="sender"></param> /// <param name="devInformation">The device interface that was removed</param> private void OnRemoved(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformationUpdate devInformation) { var deviceId = devInformation.Id; // Search the list of devices for one with a matching ID var match = FindInList(deviceId); if (match != null) { // Remove the matched item this.devices.Remove(match); DeviceRemoved(this, new UsbDeviceInfo(match)); } }
static public void Stop() { /// <summary> /// Stops watching for all nearby Bluetooth devices. /// </summary> if (deviceWatcher != null) { // Unregister the event handlers. deviceWatcher.Added -= DeviceWatcher_Added; // Stop the watcher. deviceWatcher.Stop(); deviceWatcher = null; } }
void InitDeviceWatcher() { // Define the selector to enumerate all of the fx2 device interface class instances var selector = CustomDevice.GetDeviceSelector(Fx2Driver.DeviceInterfaceGuid); // Create a device watcher to look for instances of the fx2 device interface m_Fx2Watcher = Windows.Devices.Enumeration.DeviceInformation.CreateWatcher( selector, new string[] { "System.Devices.DeviceInstanceId" } ); m_Fx2Watcher.Added += OnFx2Added; m_Fx2Watcher.Removed += OnFx2Removed; m_Fx2Watcher.EnumerationCompleted += OnFx2EnumerationComplete; }
/// <summary> /// Event handler for the end of an enumeration/reenumeration started /// by calling Start on the device watcher. This culls out any entries /// in the list which are no longer matched. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private async void OnFx2EnumerationComplete(Windows.Devices.Enumeration.DeviceWatcher sender, object args) { await MainPage.Current.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { MainPage.Current.NotifyUser(String.Format("OnFx2EnumerationComplete"), NotifyType.StatusMessage); DeviceListEntry removedDevice; while ((removedDevice = m_Fx2Devices.FirstOrDefault(e => e.Matched == false)) != null) { MainPage.Current.NotifyUser("OnFx2EnumerationComplete: Removing missing device " + removedDevice.Id, NotifyType.StatusMessage); m_Fx2Devices.Remove(removedDevice); } } ); }
/// <summary> /// Event handler for the removal of an Fx2 device /// </summary> /// <param name="sender"></param> /// <param name="devInformation">The device interface that was removed</param> private async void OnFx2Removed(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformationUpdate devInformation) { await MainPage.Current.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { var deviceId = devInformation.Id; MainPage.Current.NotifyUser(String.Format("OnFx2Removed: {0}", deviceId), NotifyType.StatusMessage); // Search the list of devices for one with a matching ID var match = FindInList(deviceId); if (match != null) { // Remove the matched item MainPage.Current.NotifyUser(String.Format("OnFx2Removed: {0} removed", deviceId), NotifyType.StatusMessage); m_Fx2Devices.Remove(match); } } ); }
/// <summary> /// This method Unregister the Device Watcher . /// </summary> public bool UnregisterDeviceWatcher() { if (deviceWatcher != null) { deviceWatcher.Stop(); deviceWatcher.Added -= DeviceAdded; deviceWatcher.Removed -= DeviceRemoved; deviceWatcher = null; return true; } return false; }
private void ShadowDriverDeviceWatcher_Removed(Windows.Devices.Enumeration.DeviceWatcher sender, Windows.Devices.Enumeration.DeviceInformationUpdate args) { ShadowDriverDevice = null; ViewModel.DeviceConnectStatus = "Disconnected"; }
/// <summary> /// This method Register the Device Watcher . /// </summary> public bool RegisterDeviceWatcher() { deviceWatcher = Windows.Devices.Enumeration.DeviceInformation.CreateWatcher(Windows.Devices.Enumeration.DeviceClass.PortableStorageDevice); if (deviceWatcher != null) { deviceWatcher.Added += DeviceAdded; deviceWatcher.Removed += DeviceRemoved; deviceWatcher.Start(); return true; } return false; }