Example #1
0
        void ReceiveMessages()
        {
            Log.Add(LogLevel.Verbose, "SMS", "Reading SMS messages from device...");
            Stopwatch sw = new Stopwatch();

            sw.Start();

            DecodedShortMessage[] msgs = _gsm.ReadMessages(PhoneMessageStatus.All, "MT");

            Log.Add(LogLevel.Verbose, "SMS", String.Format("{0} messages in storage", msgs.Length));

            foreach (DecodedShortMessage msg in msgs)
            {
                string   from     = string.Empty;
                string   text     = string.Empty;
                DateTime received = DateTime.Now;

                bool _fullMessageReceived = false;

                SmsDeliverPdu pdu = (SmsDeliverPdu)msg.Data;

                if (SmartMessageDecoder.IsPartOfConcatMessage(pdu))
                {
                    IConcatenationInfo info = SmartMessageDecoder.GetConcatenationInfo(pdu);

                    Log.Add(LogLevel.Debug, "SMS", string.Format("Received multi-part message {0}: {1}/{2}", info.ReferenceNumber, info.CurrentNumber, info.TotalMessages));

                    if (_concatPdus.ContainsKey(info.ReferenceNumber))
                    {
                        _concatPdus[info.ReferenceNumber].Add(pdu);
                    }
                    else
                    {
                        _concatPdus.Add(info.ReferenceNumber, new List <SmsPdu>()
                        {
                            pdu
                        });
                    }

                    if (SmartMessageDecoder.AreAllConcatPartsPresent(_concatPdus[info.ReferenceNumber]))
                    {
                        _fullMessageReceived = true;

                        from     = pdu.OriginatingAddress;
                        received = pdu.SCTimestamp.ToDateTime();
                        text     = SmartMessageDecoder.CombineConcatMessageText(_concatPdus[info.ReferenceNumber]);
                    }
                }
                else
                {
                    Log.Add(LogLevel.Debug, "SMS", "Received single-part SMS.");

                    _fullMessageReceived = true;

                    from     = String.Format("{0}", pdu.OriginatingAddress);
                    received = pdu.SCTimestamp.ToDateTime();
                    text     = pdu.UserDataText;
                }

                if (_fullMessageReceived)
                {
                    Log.Add(LogLevel.Info, "SMS", String.Format("Incoming SMS from {0}", from));

                    if (NormalizeNumbers)
                    {
                        from = NormalizeNumber(from);
                    }

                    if (_resolveNumbers)
                    {
                        from = ResolveNameByNumber(from);
                    }

                    Log.Add(LogLevel.Debug, "SMS", String.Format("Message from {0} at {1}: {2}", from, received, text));

                    if (OnMessageReceived != null)
                    {
                        OnMessageReceived(this, new Message(from, received, text));
                    }
                }

                _gsm.DeleteMessage(msg.Index, msg.Storage);
            }
            sw.Stop();

            Log.Add(LogLevel.Verbose, "SMS", String.Format("Reading took {0}ms", sw.Elapsed.TotalMilliseconds));
        }