C#GSMcomm在触发MessageReceived时提取SMS

时间:2015-11-21 10:51:18

标签: c# gsmcomm

有没有办法提取新收到的邮件的内容,并在触发MessageReceived事件时将其放在字符串变量中?

1 个答案:

答案 0 :(得分:0)

这么简单,只需添加这样的方法:

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    var obj = e.IndicationObject;
    if (obj is MemoryLocation)
    {
        var loc = (MemoryLocation)obj;
        var msg = string.Format("New message received in storage \"{0}\", index {1}.",
                                loc.Storage, loc.Index);
        MessageBox.Show(msg);

        DecodedShortMessage[] messages = CommSetting.comm.ReadMessages(PhoneMessageStatus.All, PhoneStorageType.Sim);
        foreach (DecodedShortMessage message in messages)
        {
            DisplayMessage(message.Data);
        }
        return;
    }
}

private void DisplayMessage(SmsPdu pdu)
{
    if (pdu is SmsDeliverPdu)
    {
        SmsDeliverPdu data = (SmsDeliverPdu)pdu;
        var phoneNumber = data.OriginatingAddress; 
        var msg = data.UserDataText;
        var date = string.Format("{0:dd/MM/yyyy}", data.SCTimestamp.ToDateTime());
        var time = string.Format("{0:HH:mm:ss}", data.SCTimestamp.ToDateTime());

        //read message in listBox1
        listBox1.Items.Add(string.Format("{0}, {1}, {2}, {3}", date, time, phoneNumber, msg));
    }
}

但请勿忘记在连接打开时注册此事件:

comm.MessageReceived += new MessageReceivedEventHandler(comm_MessageReceived);

我希望这有帮助:D