/// <summary> /// Initializes the basic properties for adding a gatt characteristic /// notification trigger /// </summary> private void ToastInit() { _toastName = string.Format("{0}{1}{2}", TOAST_STRING_PREFIX, this._characteristic.Service.Device.DeviceId.GetHashCode(), this.Uuid.GetHashCode()); Toastable = ((_characteristic.CharacteristicProperties & GattCharacteristicProperties.Notify) != 0); ToastRegistered = ApplicationData.Current.LocalSettings.Values.ContainsKey(_toastName); ToastButtonActive = true; if (ToastRegistered) { // We need to add ourselves to the list of active toasts GlobalSettings.AddToast(this); } }
/// <summary> /// Registers the background task for this characteristic. /// /// NOTE: Background tasks must have background access requested. /// In this app, this happens in GlobalSettings.Initialize() on the "BackgroundExecutionManager.RequestAccessAsync();" call. /// </summary> private void RegisterBackgroundTasksForToast() { if (!GlobalSettings.BackgroundAccessRequested) { // UI should never let us reach this, but just to be safe. Utilities.OnException(new InvalidOperationException("Cannot toast when background access missing.")); return; } // Check if task already exists var taskRegistered = false; foreach (var task in BackgroundTaskRegistration.AllTasks) { if (task.Value.Name == _toastName) { taskRegistered = true; ToastRegistered = true; break; } } // If not registered, register it if (taskRegistered == false) { string displayString = string.Format("{0}{1}{2}{3}{4}", ServiceM.DeviceM.Name, BTLE_BackgroundTasksForToasts.ToastBackgroundTask.ToastSplit, ServiceM.Name, BTLE_BackgroundTasksForToasts.ToastBackgroundTask.ToastSplit, this.Name); ApplicationData.Current.LocalSettings.Values[_toastName] = displayString; GlobalSettings.AddToast(this); var builder = new BackgroundTaskBuilder(); var trigger = new GattCharacteristicNotificationTrigger(_characteristic); builder.Name = _toastName; builder.TaskEntryPoint = typeof(ToastBackgroundTask).FullName; builder.SetTrigger(trigger); var taskRegistration = builder.Register(); // hook up completion handlers taskRegistration.Completed += OnTaskRegistrationCompleted; // update state ToastRegistered = true; } }