public async Task <BackgroundTaskRegistrationResult> UpdateBackgroundTaskIfNeededAsync() { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = true, Exception = null }; if (BackgroundTaskManager.CheckIfBackgroundFilterUpdateIsRequired()) { result = await _backgroundTaskManager.UpdateBackgroundTaskAsync(Configuration); } SdkData.BackgroundTaskEnabled = true; return(result); }
/// <summary> /// Registers the timed background task. /// </summary> /// <param name="timerClassName">Classname of the timer background service.</param> /// <returns>The registration result.</returns> public BackgroundTaskRegistrationResult RegisterTimedBackgroundTask(string timerClassName) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = false, Exception = null }; if (BackgroundTaskRegistered(TimerClass)) { // Already registered result.Success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = TimerClass; backgroundTaskBuilder.TaskEntryPoint = timerClassName; TimeTrigger timeTrigger = new TimeTrigger(TimeTriggerIntervalInMinutes, false); backgroundTaskBuilder.SetTrigger(timeTrigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += OnTimedBackgroundTaskCompleted; result.Success = true; } catch (Exception ex) { result.Exception = ex; Logger.Error("BackgroundTaskManager.RegisterTimedBackgroundTask(): Failed to register: ", ex); } } return(result); }
/// <summary> /// Register background tasks, by the given configuration. /// </summary> /// <param name="configuration">Configuration for the new registration.</param> public async Task <BackgroundTaskRegistrationResult> RegisterBackgroundTaskAsync(SdkConfiguration configuration) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = IsBackgroundTaskRegistered, Exception = null }; if (!result.Success) { // Prompt user to accept the request BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) { result = RegisterTimedBackgroundTask(configuration.BackgroundTimerClassName); if (result.Success) { result = await RegisterAdvertisementWatcherBackgroundTaskAsync(configuration); } } if (result.Success) { Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Registration successful"); } } else { Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Already registered"); } return(result); }
/// <summary> /// Registers the BLE advertisement watcher background task. /// </summary> /// <param name="configuration">Configuration for the new registration.</param> /// <returns>The registration result.</returns> private async Task <BackgroundTaskRegistrationResult> RegisterAdvertisementWatcherBackgroundTaskAsync(SdkConfiguration configuration) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = false, Exception = null }; if (BackgroundTaskRegistered(AdvertisementClass)) { // Already registered Logger.Debug("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Already registered"); result.Success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = AdvertisementClass + Guid.NewGuid(); backgroundTaskBuilder.TaskEntryPoint = configuration.BackgroundAdvertisementClassName; BluetoothLEAdvertisementWatcherTrigger advertisementWatcherTrigger = new BluetoothLEAdvertisementWatcherTrigger(); // This filter includes all Sensorberg beacons var pattern = BeaconFactory.UuidToAdvertisementBytePattern(configuration.BackgroundBeaconUuidSpace, configuration.ManufacturerId, configuration.BeaconCode); advertisementWatcherTrigger.AdvertisementFilter.BytePatterns.Add(pattern); ILayoutManager layoutManager = ServiceManager.LayoutManager; AppSettings = await ServiceManager.SettingsManager.GetSettings(); // Using MaxSamplingInterval as SamplingInterval ensures that we get an event only // when entering or exiting from the range of the beacon advertisementWatcherTrigger.SignalStrengthFilter.SamplingInterval = advertisementWatcherTrigger.MaxSamplingInterval; if (AppSettings.RssiEnterThreshold != null && AppSettings.RssiEnterThreshold.Value >= -128 && AppSettings.RssiEnterThreshold.Value <= 127) { advertisementWatcherTrigger.SignalStrengthFilter.InRangeThresholdInDBm = AppSettings.RssiEnterThreshold; } else { advertisementWatcherTrigger.SignalStrengthFilter.InRangeThresholdInDBm = Constants.DefaultBackgroundScannerEnterThreshold; } advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeThresholdInDBm = SignalStrengthFilterOutOfRangeThresholdInDBm; advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(AppSettings.BeaconExitTimeout); IBackgroundTrigger trigger = advertisementWatcherTrigger; backgroundTaskBuilder.SetTrigger(trigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += OnAdvertisementWatcherBackgroundTaskCompleted; backgroundTaskRegistration.Progress += OnAdvertisementWatcherBackgroundTaskProgress; result.Success = true; } catch (Exception ex) { result.Exception = ex; Logger.Error("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Failed to register: ", ex); if (ex.Message.Contains("0x800710DF)")) { await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => await new MessageDialog("Activate Bluetooth for the app").ShowAsync()); } } if (result.Success) { // Check if there was a pending filter update if (SdkData.BackgroundFilterUpdateRequired) { string upToDateHash = LayoutManager.CreateHashOfBeaconId1SInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash) && SdkData.LayoutBeaconId1Hash.Equals(upToDateHash)) { // Background filter updated successfully SdkData.BackgroundFilterUpdateRequired = false; BackgroundFiltersUpdated?.Invoke(this, null); } } else if (string.IsNullOrEmpty(SdkData.LayoutBeaconId1Hash)) { // This is the first time the background task is registered with valid layout => // set the hash string upToDateHash = LayoutManager.CreateHashOfBeaconId1SInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash)) { SdkData.LayoutBeaconId1Hash = upToDateHash; } } } } //Load last events from background await LoadBackgroundActions(); return(result); }
public async Task<BackgroundTaskRegistrationResult> UpdateBackgroundTaskIfNeededAsync() { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { success = true, exception = null }; if (BackgroundTaskManager.CheckIfBackgroundFilterUpdateIsRequired()) { result = await _backgroundTaskManager.UpdateBackgroundTaskAsync(ManufacturerId, BeaconCode); } SDKData.Instance.BackgroundTaskEnabled = true; return result; }
/// <summary> /// Registers the timed background task. /// </summary> /// <param name="timerClassName">Classname of the timer background service.</param> /// <returns>The registration result.</returns> public BackgroundTaskRegistrationResult RegisterTimedBackgroundTask(string timerClassName) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = false, Exception = null }; if (BackgroundTaskRegistered(TimerClass)) { // Already registered result.Success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = TimerClass; backgroundTaskBuilder.TaskEntryPoint = timerClassName; TimeTrigger timeTrigger = new TimeTrigger(TimeTriggerIntervalInMinutes, false); backgroundTaskBuilder.SetTrigger(timeTrigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += OnTimedBackgroundTaskCompleted; result.Success = true; } catch (Exception ex) { result.Exception = ex; Logger.Error("BackgroundTaskManager.RegisterTimedBackgroundTask(): Failed to register: " , ex); } } return result; }
/// <summary> /// Registers the BLE advertisement watcher background task. /// </summary> /// <param name="configuration">Configuration for the new registration.</param> /// <returns>The registration result.</returns> private async Task<BackgroundTaskRegistrationResult> RegisterAdvertisementWatcherBackgroundTaskAsync(SdkConfiguration configuration) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = false, Exception = null }; if (BackgroundTaskRegistered(AdvertisementClass)) { // Already registered Logger.Debug("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Already registered"); result.Success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = AdvertisementClass + Guid.NewGuid(); backgroundTaskBuilder.TaskEntryPoint = configuration.BackgroundAdvertisementClassName; BluetoothLEAdvertisementWatcherTrigger advertisementWatcherTrigger = new BluetoothLEAdvertisementWatcherTrigger(); // This filter includes all Sensorberg beacons var pattern = BeaconFactory.UuidToAdvertisementBytePattern(configuration.BackgroundBeaconUuidSpace, configuration.ManufacturerId, configuration.BeaconCode); advertisementWatcherTrigger.AdvertisementFilter.BytePatterns.Add(pattern); ILayoutManager layoutManager = ServiceManager.LayoutManager; AppSettings = await ServiceManager.SettingsManager.GetSettings(); // Using MaxSamplingInterval as SamplingInterval ensures that we get an event only // when entering or exiting from the range of the beacon advertisementWatcherTrigger.SignalStrengthFilter.SamplingInterval = advertisementWatcherTrigger.MaxSamplingInterval; if (AppSettings.RssiEnterThreshold != null && AppSettings.RssiEnterThreshold.Value >= -128 && AppSettings.RssiEnterThreshold.Value <= 127) { advertisementWatcherTrigger.SignalStrengthFilter.InRangeThresholdInDBm = AppSettings.RssiEnterThreshold; } else { advertisementWatcherTrigger.SignalStrengthFilter.InRangeThresholdInDBm = Constants.DefaultBackgroundScannerEnterThreshold; } advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeThresholdInDBm = SignalStrengthFilterOutOfRangeThresholdInDBm; advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(AppSettings.BeaconExitTimeout); IBackgroundTrigger trigger = advertisementWatcherTrigger; backgroundTaskBuilder.SetTrigger(trigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += OnAdvertisementWatcherBackgroundTaskCompleted; backgroundTaskRegistration.Progress += OnAdvertisementWatcherBackgroundTaskProgress; result.Success = true; } catch (Exception ex) { result.Exception = ex; Logger.Error("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Failed to register: ", ex); if (ex.Message.Contains("0x800710DF)")) { await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => await new MessageDialog("Activate Bluetooth for the app").ShowAsync()); } } if (result.Success) { // Check if there was a pending filter update if (SdkData.BackgroundFilterUpdateRequired) { string upToDateHash = LayoutManager.CreateHashOfBeaconId1SInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash) && SdkData.LayoutBeaconId1Hash.Equals(upToDateHash)) { // Background filter updated successfully SdkData.BackgroundFilterUpdateRequired = false; BackgroundFiltersUpdated?.Invoke(this, null); } } else if (string.IsNullOrEmpty(SdkData.LayoutBeaconId1Hash)) { // This is the first time the background task is registered with valid layout => // set the hash string upToDateHash = LayoutManager.CreateHashOfBeaconId1SInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash)) { SdkData.LayoutBeaconId1Hash = upToDateHash; } } } } //Load last events from background await LoadBackgroundActions(); return result; }
/// <summary> /// Register background tasks, by the given configuration. /// </summary> /// <param name="configuration">Configuration for the new registration.</param> public async Task<BackgroundTaskRegistrationResult> RegisterBackgroundTaskAsync(SdkConfiguration configuration) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = IsBackgroundTaskRegistered, Exception = null }; if (!result.Success) { // Prompt user to accept the request BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) { result = RegisterTimedBackgroundTask(configuration.BackgroundTimerClassName); if (result.Success) { result = await RegisterAdvertisementWatcherBackgroundTaskAsync(configuration); } } if (result.Success) { Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Registration successful"); } } else { Logger.Debug("BackgroundTaskManager.RegisterBackgroundTask(): Already registered"); } return result; }
public async Task<BackgroundTaskRegistrationResult> UpdateBackgroundTaskIfNeededAsync() { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { Success = true, Exception = null }; if (BackgroundTaskManager.CheckIfBackgroundFilterUpdateIsRequired()) { result = await _backgroundTaskManager.UpdateBackgroundTaskAsync(Configuration); } SdkData.BackgroundTaskEnabled = true; return result; }
internal async Task<BackgroundTaskRegistrationResult> InternalRegisterBackgroundTaskAsync(UInt16 manufacturerId, UInt16 beaconCode) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { success = IsBackgroundTaskRegistered, exception = null }; if (!result.success) { // Prompt user to accept the request BackgroundAccessStatus backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync(); if (backgroundAccessStatus == BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity || backgroundAccessStatus == BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity) { result = RegisterTimedBackgroundTask(); if (result.success) { result = await RegisterAdvertisementWatcherBackgroundTaskAsync(manufacturerId, beaconCode); } } if (result.success) { System.Diagnostics.Debug.WriteLine("BackgroundTaskManager.RegisterBackgroundTask(): Registration successful"); } } else { System.Diagnostics.Debug.WriteLine("BackgroundTaskManager.RegisterBackgroundTask(): Already registered"); } return result; }
/// <summary> /// Registers the timed background task. /// </summary> /// <returns>The registration result.</returns> public BackgroundTaskRegistrationResult RegisterTimedBackgroundTask() { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { success = false, exception = null }; if (BackgroundTaskRegistered(_timedBackgroundTaskName)) { // Already registered result.success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = _timedBackgroundTaskName; backgroundTaskBuilder.TaskEntryPoint = TimedBackgroundTaskEntryPoint; TimeTrigger timeTrigger = new TimeTrigger(TimeTriggerIntervalInMinutes, false); backgroundTaskBuilder.SetTrigger(timeTrigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnTimedBackgroundTaskCompleted); result.success = true; } catch (Exception ex) { result.exception = ex; System.Diagnostics.Debug.WriteLine("BackgroundTaskManager.RegisterTimedBackgroundTask(): Failed to register: " + ex); } } return result; }
/// <summary> /// Registers the BLE advertisement watcher background task. /// </summary> /// <param name="manufacturerId">The manufacturer ID of beacons to watch.</param> /// <param name="beaconCode">The beacon code of beacons to watch.</param> /// <returns>The registration result.</returns> private async Task<BackgroundTaskRegistrationResult> RegisterAdvertisementWatcherBackgroundTaskAsync( UInt16 manufacturerId, UInt16 beaconCode) { BackgroundTaskRegistrationResult result = new BackgroundTaskRegistrationResult() { success = false, exception = null }; if (BackgroundTaskRegistered(_advertisementWatcherBackgroundTaskName)) { // Already registered System.Diagnostics.Debug.WriteLine("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Already registered"); result.success = true; } else { BackgroundTaskBuilder backgroundTaskBuilder = new BackgroundTaskBuilder(); backgroundTaskBuilder.Name = _advertisementWatcherBackgroundTaskName; backgroundTaskBuilder.TaskEntryPoint = AdvertisementWatcherBackgroundTaskEntryPoint; IBackgroundTrigger trigger = null; BluetoothLEAdvertisementWatcherTrigger advertisementWatcherTrigger = new BluetoothLEAdvertisementWatcherTrigger(); //BluetoothLEManufacturerData manufacturerData = BeaconFactory.DefaultBeaconManufacturerData(); //advertisementWatcherTrigger.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); // This filter includes all Sensorberg beacons var pattern = BeaconFactory.UUIDToAdvertisementBytePattern(Constants.SensorbergUuidSpace, manufacturerId, beaconCode); advertisementWatcherTrigger.AdvertisementFilter.BytePatterns.Add(pattern); LayoutManager layoutManager = LayoutManager.Instance; #if FILTER_SUPPORTS_MORE_UUIDS // Only UUIDs that are registered to the app will be added into filter if (await layoutManager.VerifyLayoutAsync(false) && layoutManager.Layout.ContainsOtherThanSensorbergBeaconId1s()) { int counter = 0; foreach (string beaconId1 in LayoutManager.Instance.Layout.AccountBeaconId1s) { if (beaconId1.Length == Constants.BeaconId1LengthWithoutDashes && counter < MaxBeaconId1FilterCount) { if (!beaconId1.StartsWith(Constants.SensorbergUuidSpace, StringComparison.CurrentCultureIgnoreCase)) { pattern = BeaconFactory.UUIDToAdvertisementBytePattern(beaconId1); advertisementWatcherTrigger.AdvertisementFilter.BytePatterns.Add(pattern); counter++; } } } } #endif // Using MaxSamplingInterval as SamplingInterval ensures that we get an event only // when entering or exiting from the range of the beacon advertisementWatcherTrigger.SignalStrengthFilter.SamplingInterval = advertisementWatcherTrigger.MaxSamplingInterval; advertisementWatcherTrigger.SignalStrengthFilter.InRangeThresholdInDBm = SignalStrengthFilterInRangeThresholdInDBm; advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeThresholdInDBm = SignalStrengthFilterOutOfRangeThresholdInDBm; advertisementWatcherTrigger.SignalStrengthFilter.OutOfRangeTimeout = TimeSpan.FromMilliseconds(Constants.BeaconExitDelayInMilliseconds); trigger = advertisementWatcherTrigger; backgroundTaskBuilder.SetTrigger(trigger); try { BackgroundTaskRegistration backgroundTaskRegistration = backgroundTaskBuilder.Register(); backgroundTaskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnAdvertisementWatcherBackgroundTaskCompleted); result.success = true; } catch (Exception ex) { result.exception = ex; System.Diagnostics.Debug.WriteLine("BackgroundTaskManager.RegisterAdvertisementWatcherBackgroundTask(): Failed to register: " + ex); } if (result.success) { SDKData sdkData = SDKData.Instance; // Check if there was a pending filter update if (sdkData.BackgroundFilterUpdateRequired) { string upToDateHash = LayoutManager.CreateHashOfBeaconId1sInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash) && sdkData.LayoutBeaconId1Hash.Equals(upToDateHash)) { // Background filter updated successfully sdkData.BackgroundFilterUpdateRequired = false; if (BackgroundFiltersUpdated != null) { BackgroundFiltersUpdated(this, null); } } } else if (string.IsNullOrEmpty(sdkData.LayoutBeaconId1Hash)) { // This is the first time the background task is registered with valid layout => // set the hash string upToDateHash = LayoutManager.CreateHashOfBeaconId1sInLayout(layoutManager.Layout); if (!string.IsNullOrEmpty(upToDateHash)) { sdkData.LayoutBeaconId1Hash = upToDateHash; } } } } return result; }