示例#1
0
        // Delete one or all messages.
        // The ID of the message to delete is passed as a parameter. An ID of MaxValue
        // specifies that all messages should be deleted.
        private async Task DoDeleteAsync(uint messageId)
        {
            // If this is the first request, get the default SMS device. If this
            // is the first SMS device access, the user will be prompted to grant
            // access permission for this application.
            if (device == null)
            {
                try
                {
                    rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage);
                    device = await SmsDevice.GetDefaultAsync();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            try
            {
                // Delete one or all messages.
                if (messageId < uint.MaxValue)
                {
                    // Verify ID is within range (1 to message store capacity). Note that a SIM
                    // can have gaps in its message array, so all valid IDs do not necessarily map
                    // to messages.
                    if (messageId >= 1 && messageId <= device.MessageStore.MaxMessages)
                    {
                        // Delete the selected message asynchronously.
                        rootPage.NotifyUser("Deleting message ...", NotifyType.StatusMessage);
                        await device.MessageStore.DeleteMessageAsync(messageId);

                        rootPage.NotifyUser("Message " + messageId + " deleted", NotifyType.StatusMessage);
                    }
                    else
                    {
                        rootPage.NotifyUser("Message ID entered is out of range", NotifyType.ErrorMessage);
                    }
                }
                else
                {
                    // Delete all messages asynchronously.
                    rootPage.NotifyUser("Deleting all messages ...", NotifyType.StatusMessage);
                    await device.MessageStore.DeleteMessagesAsync(SmsMessageFilter.All);

                    rootPage.NotifyUser("All messages deleted", NotifyType.StatusMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);

                // On failure, release the device. If the user revoked access or the device
                // is removed, a new device object must be obtained.
                device = null;
            }
        }
示例#2
0
        // Handle request to register the background task
        private async void RegisterBackgroundTask_Click(object sender, RoutedEventArgs e)
        {
            // SMS is a sensitive capability and the user may be prompted for consent. If the app
            // does not obtain permission for the package to have access to SMS before the background
            // work item is run (perhaps after the app is suspended or terminated), the background
            // work item will not have access to SMS and will have no way to prompt the user for consent
            // without an active window. Here, any available SMS device is activated in order to ensure
            // consent. Your app will likely do something with the SMS device as part of its features.
            if (!hasDeviceAccess)
            {
                try
                {
                    SmsDevice smsDevice = (SmsDevice)await SmsDevice.GetDefaultAsync();

                    rootPage.NotifyUser("Successfully connnected to SMS device with account number: " + smsDevice.AccountPhoneNumber, NotifyType.StatusMessage);
                    hasDeviceAccess = true;
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            try
            {
                // Create a new background task builder.
                BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();

                // Create a new SmsReceived trigger.
                SystemTrigger trigger = new SystemTrigger(SystemTriggerType.SmsReceived, false);

                // Associate the SmsReceived trigger with the background task builder.
                taskBuilder.SetTrigger(trigger);

                // Specify the background task to run when the trigger fires.
                taskBuilder.TaskEntryPoint = BackgroundTaskEntryPoint;

                // Name the background task.
                taskBuilder.Name = BackgroundTaskName;

                // Register the background task.
                BackgroundTaskRegistration taskRegistration = taskBuilder.Register();

                // Associate completed event handler with the new background task.
                taskRegistration.Completed += new BackgroundTaskCompletedEventHandler(OnCompleted);

                UpdateBackgroundTaskUIState(true);
                rootPage.NotifyUser("Registered SMS background task", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage);
            }
        }
        // Clean-up when scenario page is left. This is called when the
        // user navigates away from the scenario page.
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Detach event handler
            if (listening)
            {
                device.SmsMessageReceived -= device_SmsMessageReceived;
            }

            // Release the device
            device = null;
        }
示例#4
0
        /// <summary>
        /// Initialize SIM device and update UI Labels with result
        /// </summary>
        private async void InitializeSIM()
        {
            SmsDevice device;

            try
            {
                device = await SmsDevice.GetDefaultAsync();

                ResultLbl.Text = LocRM.GetString("SIMFound");
            }
            catch (Exception e)
            {
                ResultLbl.Text = LocRM.GetString("SIMNotFound");
                Log.LogError(e.ToString());
            }
        }
        // Handle a request to send a text message.
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            // If this is the first request, get the default SMS device. If this
            // is the first SMS device access, the user will be prompted to grant
            // access permission for this application.
            if (device == null)
            {
                try
                {
                    rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage);
                    device = await SmsDevice.GetDefaultAsync();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            try
            {
                // Convert the entered message from hex to a byte array.
                byte[] data;
                ParseHexString(PduMessageText.Text, out data);

                // Create a binary message and set the data.
                SmsBinaryMessage msg = new SmsBinaryMessage();
                msg.SetData(data);

                // Set format based on the SMS device cellular type (GSM or CDMA)
                msg.Format = (device.CellularClass == CellularClass.Gsm) ? SmsDataFormat.GsmSubmit : SmsDataFormat.CdmaSubmit;

                // Send message asynchronously.
                rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                await device.SendMessageAsync(msg);

                rootPage.NotifyUser("Sent message sent in PDU format", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);

                // On failure, release the device. If the user revoked access or the device
                // is removed, a new device object must be obtained.
                device = null;
            }
        }
示例#6
0
        // Handle a request to send a text message.
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            // If this is the first request, get the default SMS device. If this
            // is the first SMS device access, the user will be prompted to grant
            // access permission for this application.
            if (device == null)
            {
                try
                {
                    rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage);
                    device = await SmsDevice.GetDefaultAsync();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            try
            {
                // Create a text message - set the entered destination number and message text.
                SmsTextMessage msg = new SmsTextMessage();
                msg.To   = SendToText.Text;
                msg.Body = SendMessageText.Text;

                // Send the message asynchronously
                rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                await device.SendMessageAsync(msg);

                rootPage.NotifyUser("Text message sent", NotifyType.StatusMessage);
            }
            catch (Exception err)
            {
                rootPage.NotifyUser(err.Message, NotifyType.ErrorMessage);

                // On failure, release the device. If the user revoked access or the device
                // is removed, a new device object must be obtained.
                device = null;
            }
        }
示例#7
0
        async Task DisplayToastAsync(IBackgroundTaskInstance taskInstance)
        {
            SmsReceivedEventDetails smsDetails = (SmsReceivedEventDetails)taskInstance.TriggerDetails;

            SmsDevice smsDevice = (SmsDevice)await SmsDevice.FromIdAsync(smsDetails.DeviceId);

            SmsBinaryMessage smsEncodedmsg = (SmsBinaryMessage)await smsDevice.MessageStore.GetMessageAsync(smsDetails.MessageIndex);

            SmsTextMessage smsTextMessage = Windows.Devices.Sms.SmsTextMessage.FromBinaryMessage(smsEncodedmsg);

            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);

            XmlNodeList stringElements = toastXml.GetElementsByTagName("text");

            stringElements.Item(0).AppendChild(toastXml.CreateTextNode(smsTextMessage.From));

            stringElements.Item(1).AppendChild(toastXml.CreateTextNode(smsTextMessage.Body));

            ToastNotification notification = new ToastNotification(toastXml);

            ToastNotificationManager.CreateToastNotifier().Show(notification);
        }
        // Handle a request to listen for received messages.
        private async void Receive_Click(object sender, RoutedEventArgs e)
        {
            // If this is the first request, get the default SMS device. If this
            // is the first SMS device access, the user will be prompted to grant
            // access permission for this application.
            if (device == null)
            {
                try
                {
                    rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage);
                    device = await SmsDevice.GetDefaultAsync();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            // Attach a message received handler to the device, if not already listening
            if (!listening)
            {
                try
                {
                    msgCount = 0;
                    device.SmsMessageReceived += device_SmsMessageReceived;
                    listening = true;
                    rootPage.NotifyUser("Waiting for message ...", NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);

                    // On failure, release the device. If the user revoked access or the device
                    // is removed, a new device object must be obtained.
                    device = null;
                }
            }
        }
        // Handle a received message event.
        async void device_SmsMessageReceived(SmsDevice sender, SmsMessageReceivedEventArgs args)
        {
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
                                      () =>
            {
                try
                {
                    // Get message from the event args.
                    SmsTextMessage msg = args.TextMessage;
                    msgCount          += 1;

                    ReceivedCountText.Text   = msgCount.ToString();
                    ReceivedFromText.Text    = msg.From;
                    ReceivedMessageText.Text = msg.Body;
                    rootPage.NotifyUser(msgCount.ToString() + ((msgCount == 1) ? " message" : " messages") + " received", NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            });
        }
示例#10
0
        // Handle a request to read a message.
        private async void Read_Click(object sender, RoutedEventArgs e)
        {
            // If this is the first request, get the default SMS device. If this
            // is the first SMS device access, the user will be prompted to grant
            // access permission for this application.
            if (device == null)
            {
                try
                {
                    rootPage.NotifyUser("Getting default SMS device ...", NotifyType.StatusMessage);
                    device = await SmsDevice.GetDefaultAsync();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("Failed to find SMS device\n" + ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            // Clear message display.
            DateText.Text        = "";
            ReadFromText.Text    = "";
            ReadMessageText.Text = "";

            try
            {
                // Parse the message ID - must be number between 1 and maximum message count.
                uint id;
                if (uint.TryParse(ReadIdText.Text, out id) &&
                    (id >= 1) && (id <= device.MessageStore.MaxMessages))
                {
                    rootPage.NotifyUser("Reading message ...", NotifyType.StatusMessage);

                    // Get the selected message from message store asynchronously.
                    ISmsMessage msg = await device.MessageStore.GetMessageAsync(id);

                    // See if this is a text message by querying for the text message interface.
                    ISmsTextMessage textMsg = msg as ISmsTextMessage;
                    if (textMsg == null)
                    {
                        // If it is a binary message then try to convert it to a text message.
                        if (msg is SmsBinaryMessage)
                        {
                            textMsg = SmsTextMessage.FromBinaryMessage(msg as SmsBinaryMessage);
                        }
                    }

                    // Display the text message information.
                    if (textMsg != null)
                    {
                        DateText.Text        = textMsg.Timestamp.DateTime.ToString();
                        ReadFromText.Text    = textMsg.From;
                        ReadMessageText.Text = textMsg.Body;

                        rootPage.NotifyUser("Message read.", NotifyType.StatusMessage);
                    }
                }
                else
                {
                    rootPage.NotifyUser("Invalid ID number entered.", NotifyType.ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);

                // On failure, release the device. If the user revoked access or the device
                // is removed, a new device object must be obtained.
                device = null;
            }
        }
示例#11
0
 // Clean-up when scenario page is left. This is called when the
 // user navigates away from the scenario page.
 protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     // Release the device.
     device = null;
 }