// 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; }
// 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; } }
// 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; } }
// 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); } }); }
// 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; }
// 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; } }
// 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; } }