// 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;
            }
        }
示例#2
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;
            }
        }