private void IBleStack_DiscoveryComplete(IBleStack sender, BleDiscoveryStatus status) { ExecuteOnMainThread.BeginInvoke(() => { string discovery_status = "Discovery status : "; switch (status) { case BleDiscoveryStatus.BDS_COMPLETE: { discovery_status += "Complete!"; break; } case BleDiscoveryStatus.BDS_CANCELED: { discovery_status += "Canceled!"; break; } case BleDiscoveryStatus.BDS_FAILED: { discovery_status += "Failed!"; break; } default: { discovery_status += "Unknown..."; break; } } lbStatus.Text = discovery_status; }); }
/// <summary> /// Called when the discovery process is complete /// </summary> /// <param name="sender">Device watcher which did the discovery process</param> /// <param name="e">not used, always null</param> private void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, object e) { // Update discovery status _discovery_status = BleDiscoveryStatus.BDS_COMPLETE; _device_watcher.Stop(); // Notify end of discovery DiscoveryComplete?.Invoke(this, BleDiscoveryStatus.BDS_COMPLETE); }
/// <summary> /// Called when the discovery process has been stopped /// </summary> /// <param name="sender">Device watcher which did the discovery process</param> /// <param name="e">not used, always null</param> private void DeviceWatcher_Stopped(DeviceWatcher sender, object e) { // Check if the discovery process is not already complete if (_discovery_status != BleDiscoveryStatus.BDS_COMPLETE) { // Update discovery status _discovery_status = BleDiscoveryStatus.BDS_CANCELED; // Notify end of discovery DiscoveryComplete?.Invoke(this, BleDiscoveryStatus.BDS_CANCELED); } }
/// <summary> /// Start the discovery of BLE devices /// </summary> /// <returns>true if the discovery has started, fals otherwise</returns> public bool StartDeviceDiscovery() { bool ret = false; // Check if the discovery process is stopped if (_device_watcher.Status != DeviceWatcherStatus.Started) { // Start the discovery process _device_watcher.Start(); _discovery_status = BleDiscoveryStatus.BDS_IN_PROGRESS; ret = true; } return(ret); }
/// <summary> /// Constructor /// </summary> public UwpBleStack() { // Create a device watcher to discover BLE devices only with their associated properties string[] requested_properties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" }; string aqs_ble_devices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")"; _device_watcher = DeviceInformation.CreateWatcher(aqs_ble_devices, requested_properties, DeviceInformationKind.AssociationEndpoint); // Register to watcher events _device_watcher.Added += DeviceWatcher_Added; _device_watcher.Updated += DeviceWatcher_Updated; _device_watcher.Removed += DeviceWatcher_Removed; _device_watcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted; _device_watcher.Stopped += DeviceWatcher_Stopped; // Intialize discovery status _discovery_status = BleDiscoveryStatus.BDS_STOPPED; }