Exemplo n.º 1
0
        private async void SendButton_Click(object sender, RoutedEventArgs e)
        {
            if (device == null)

            {
                try

                {
                    Debug.WriteLine("Getting default SMS device ...");

                    device = SmsDevice2.GetDefault();
                }

                catch (Exception ex)

                {
                    Debug.WriteLine(ex.Message);

                    return;
                }
            }



            string msgStr = "";

            if (device != null)

            {
                var wors = recipientbox.Text.Split("; ");
                foreach (var wor in wors)
                {
                    try

                    {
                        // Create a text message - set the entered destination number and message text.

                        SmsTextMessage2 msg = new SmsTextMessage2();

                        msg.To = wor;

                        msg.Body = MessageTB.Text;

                        // Send the message asynchronously

                        Debug.WriteLine("Sending message ...");

                        SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);



                        if (result.IsSuccessful)

                        {
                            msgStr = "";

                            msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();

                            IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;



                            for (int i = 0; i < messageReferenceNumbers.Count; i++)

                            {
                                msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                            }

                            Debug.WriteLine(msgStr);
                        }

                        else

                        {
                            msgStr = "";

                            msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();

                            msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();

                            if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)

                            {
                                msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();



                                if (result.CellularClass == CellularClass.Cdma)

                                {
                                    msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                                }

                                Debug.WriteLine(msgStr);
                            }
                        }
                    }

                    catch (Exception ex)

                    {
                        Debug.WriteLine(ex.Message);
                    }
                }
            }

            else

            {
                Debug.WriteLine("Failed to find SMS device");
            }
        }
Exemplo n.º 2
0
        public async static Task <bool> SendTextMessageAsync(SmsDevice2 device, string[] numbers, string textmessage, string transportId = "0")
        {
            bool             returnresult = true;
            ChatMessageStore store        = await ChatMessageManager.RequestStoreAsync();

            if (string.IsNullOrEmpty(transportId) || await ChatMessageManager.GetTransportAsync(transportId) == null)
            {
                var transports = await ChatMessageManager.GetTransportsAsync();

                if (transports.Count != 0)
                {
                    var transport = transports[0];
                    transportId = transport.TransportId;
                }
                else
                {
                    transportId = await ChatMessageManager.RegisterTransportAsync();
                }
            }
            try
            {
                SmsTextMessage2 message = new SmsTextMessage2();
                message.Body = textmessage;

                foreach (var number in numbers)
                {
                    var num = number.Trim();
                    message.To = num;

                    try
                    {
                        SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(message);

                        var offset = new DateTimeOffset(DateTime.Now);

                        returnresult &= result.IsSuccessful;

                        try
                        {
                            ChatMessage msg = new ChatMessage();

                            msg.Body = textmessage;
                            msg.Recipients.Add(num);

                            msg.LocalTimestamp   = offset;
                            msg.NetworkTimestamp = offset;

                            msg.IsIncoming  = false;
                            msg.TransportId = transportId;

                            msg.MessageOperatorKind = ChatMessageOperatorKind.Sms;
                            msg.Status = result.IsSuccessful ? ChatMessageStatus.Sent : ChatMessageStatus.SendFailed;

                            await store.SaveMessageAsync(msg);
                        }
                        catch
                        {
                        }
                    }
                    catch
                    {
                        returnresult = false;
                    }
                }
            }
            catch
            {
                returnresult = false;
            }

            return(returnresult);
        }
Exemplo n.º 3
0
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            if (SendToText.Text == "")
            {
                rootPage.NotifyUser("Please enter sender number", NotifyType.ErrorMessage);
                return;
            }

            if (SendMessageText.Text == "")
            {
                rootPage.NotifyUser("Please enter message", NotifyType.ErrorMessage);
                return;
            }

            // 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 = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            string msgStr = "";

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

                    // Send the message asynchronously
                    rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();
                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;

                        for (int i = 0; i < messageReferenceNumbers.Count; i++)
                        {
                            msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                        }
                        rootPage.NotifyUser(msgStr, NotifyType.StatusMessage);
                    }
                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            rootPage.NotifyUser(msgStr, NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                rootPage.NotifyUser("Failed to find SMS device", NotifyType.ErrorMessage);
            }
        }
Exemplo n.º 4
0
        // Clicking of the Start App button
        private async void Send_Click(object sender, RoutedEventArgs e)
        {
            // This event occurs when the user clicks the Start App button and is distinguished from the event where
            // this button click is triggered by a high acceleration event.
            if (HighAccEvent == false)
            {
                // If the device does have an accelerometer
                if (_accelerometer != null)
                {
                    // Set the report interval to enable the sensor for polling
                    _accelerometer.ReportInterval = _desiredReportInterval;
                    _dispatcherTimer.Start();
                }

                // No Accelerometer found. Notify the user.
                else
                {
                    rootPage.NotifyUser("No accelerometer found. This app is not compatible with your device.", NotifyType.ErrorMessage);
                }
            }


            // This sections corresponds to the the event in which the clicking of the start app button is triggered by HighAccEvent
            else
            {
                // Geolocation based on bing API
                Geolocator geolocator = new Geolocator();
                geolocator.DesiredAccuracyInMeters = 500;
                try
                {
                    Geoposition geoposition = await geolocator.GetGeopositionAsync(
                        maximumAge : TimeSpan.FromMinutes(5),
                        timeout : TimeSpan.FromSeconds(10)
                        );

                    // 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 = SmsDevice2.GetDefault();
                        }
                        catch (Exception ex)
                        {
                            rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                            return;
                        }
                    }

                    string msgStr = "";
                    if (device != null)
                    {
                        try
                        {
                            // Create a text message - set the entered destination number and message text.
                            SmsTextMessage2 msg = new SmsTextMessage2();

                            // If the user has not yet entered the number
                            if (Mnumber == null)
                            {
                                rootPage.NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                                return;
                            }

                            msg.To   = Mnumber;
                            msg.Body = "Emergency alert! I have been in a possible accident at " + geoposition.Coordinate.Latitude.ToString("0.00") + ", " + geoposition.Coordinate.Longitude.ToString("0.00") + ". Please help.";

                            // Send the message asynchronously
                            rootPage.NotifyUser("Sending Message to emergency contact and rescue services.", NotifyType.StatusMessage);
                            SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                            if (result.IsSuccessful)
                            {
                                msgStr  = "";
                                msgStr += "Message sent to: " + Mnumber + "(Predefined Emergency Number)" + System.Environment.NewLine;
                                msgStr += "Emergency alert! I have been in a possible accident at Map Coordinates:" + geoposition.Coordinate.Latitude.ToString("0.00") + ", " + geoposition.Coordinate.Longitude.ToString("0.00") + ". Please help." + System.Environment.NewLine;

                                IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;
                                rootPage.NotifyUser(msgStr, NotifyType.StatusMessage);
                            }

                            else
                            {
                                msgStr  = "";
                                msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                                msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                                if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                                {
                                    msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                                    if (result.CellularClass == CellularClass.Cdma)
                                    {
                                        msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                                    }
                                    rootPage.NotifyUser(msgStr, NotifyType.ErrorMessage);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                        }
                    }
                    else
                    {
                        if (Mnumber != "default")
                        {
                            rootPage.NotifyUser("Could not connect to network. SMS could not be sent", NotifyType.ErrorMessage);
                        }
                        else
                        {
                            rootPage.NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                { // No action needed
                }
            }
        }
        private async Task Send_Message(bool Emergency = true)
        {
            if (device == null)
            {
                try
                {
                    device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    return;
                }
            }

            string msgStr = "";

            if (device != null)
            {
                try
                {
                    // Create a text message - set the entered destination number and message text.
                    SmsTextMessage2 msg = new SmsTextMessage2();

                    // TODO : What to do below?
                    msg.To = Mnum.countryCode + Mnum.tenDigits;
                    // msg.To = Mnum.tenDigits;

                    try
                    {
                        if (Emergency)
                        {
                            msg.Body = "The Be-Safe app on this user's device has encountered a high acceleration event. Since your number has been listed as the emergency contact by the user, it is highly recommended that an immediate call is made to assure their safety. User's Location Coordinates : " + pos.Coordinate.Latitude.ToString("0.00") + ", " + pos.Coordinate.Longitude.ToString("0.00");
                        }
                        else
                        {
                            msg.Body = "The Be-Safe app is extremely sorry. The user of this number is completely safe, the previous message was a false alarm!";
                        }
                    }
                    catch (Exception)
                    {
                        msg.Body = "Emergency alert! I have been in a possible accident detected automatically by a high acceleration event. Please help.";
                    }
                    // Send the message asynchronously
                    if (Emergency)
                    {
                        NotifyUser("Sending Message to emergency contact.", NotifyType.StatusMessage);
                    }

                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Message sent to: " + Mnum.countryCode + Mnum.tenDigits + " (Predefined Emergency Number)" + System.Environment.NewLine;
                        try
                        {
                            if (Emergency)
                            {
                                msgStr += "The Be-Safe app on this user's device has encountered a high acceleration event. Since your number has been listed as the emergency contact by the user, it is highly recommended that an immediate call is made to assure their safety. User's Location Coordinates : " + pos.Coordinate.Latitude.ToString("0.00") + ", " + pos.Coordinate.Longitude.ToString("0.00") + System.Environment.NewLine;
                            }
                            else
                            {
                                msgStr += "The Be-Safe app is extremely sorry. The user of this number is completely safe, the previous message was a false alarm!";
                            }
                        }
                        catch (Exception)
                        {
                            msgStr += "Emergency alert! I have been in a possible accident detected automatically by a high acceleration event. Please help.";
                        }
                        if (Emergency)
                        {
                            MainPage.DisplayToast("Emergency Text Sent to " + Mnum.countryCode + Mnum.tenDigits);
                        }
                        else
                        {
                            MainPage.DisplayToast("False Alarm Message Sent");
                        }

                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;
                        NotifyUser(msgStr, NotifyType.StatusMessage);
                        if (!Emergency)
                        {
                            FalseAlarmButton.Visibility = Visibility.Collapsed;
                            textBlock3.Text             = "False Alarm Message Sent. Sorry for the inconvenience.";
                        }
                        else
                        {
                            FalseAlarmButton.Visibility = Visibility.Visible;
                        }
                    }

                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            NotifyUser(msgStr, NotifyType.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    NotifyUser(ex.Message, NotifyType.ErrorMessage);
                }
            }
            else
            {
                if (Mnum != null)
                {
                    NotifyUser("Could not connect to network. SMS could not be sent", NotifyType.ErrorMessage);
                }
                else
                {
                    NotifyUser("You have not yet entered your emergency contact number!", NotifyType.ErrorMessage);
                }
            }
        }
Exemplo n.º 6
0
        public static async void sendSMS(String to, String text)
        {
            if (device == null)
            {
                try
                {
                    //DialogBox.ShowOk("Notification", "Getting default SMS device ...");
                    device = SmsDevice2.GetDefault();
                }
                catch (Exception ex)
                {
                    //rootPage.NotifyUser(ex.Message, NotifyType.ErrorMessage);
                    DialogBox.ShowOk("Error", ex.Message);
                    return;
                }
            }

            string msgStr = "";

            if (device != null)
            {
                try
                {
                    // Create a text message - set the entered destination number and message text.
                    SmsTextMessage2 msg = new SmsTextMessage2();
                    msg.To   = to;
                    msg.Body = text;

                    // Send the message asynchronously
                    //rootPage.NotifyUser("Sending message ...", NotifyType.StatusMessage);
                    SmsSendMessageResult result = await device.SendMessageAndGetResultAsync(msg);

                    if (result.IsSuccessful)
                    {
                        msgStr  = "";
                        msgStr += "Text message sent, cellularClass: " + result.CellularClass.ToString();
                        IReadOnlyList <Int32> messageReferenceNumbers = result.MessageReferenceNumbers;

                        for (int i = 0; i < messageReferenceNumbers.Count; i++)
                        {
                            msgStr += "\n\t\tMessageReferenceNumber[" + i.ToString() + "]: " + messageReferenceNumbers[i].ToString();
                        }
                        DialogBox.ShowOk("Success", msgStr);//change??
                    }
                    else
                    {
                        msgStr  = "";
                        msgStr += "ModemErrorCode: " + result.ModemErrorCode.ToString();
                        msgStr += "\nIsErrorTransient: " + result.IsErrorTransient.ToString();
                        if (result.ModemErrorCode == SmsModemErrorCode.MessagingNetworkError)
                        {
                            msgStr += "\n\tNetworkCauseCode: " + result.NetworkCauseCode.ToString();

                            if (result.CellularClass == CellularClass.Cdma)
                            {
                                msgStr += "\n\tTransportFailureCause: " + result.TransportFailureCause.ToString();
                            }
                            DialogBox.ShowOk("Error", msgStr);
                        }
                    }
                }
                catch (Exception ex)
                {
                    DialogBox.ShowOk("Error", ex.Message);
                }
            }
            else
            {
                DialogBox.ShowOk("Error", "Failed to find SMS device");
            }
        }