.NET 电话通信库 第四部分 - 接收短信






4.68/5 (18投票s)
2006年12月10日
1分钟阅读

353523

8566
使用 atSMS 库接收短信。
引言
在文章的第二部分中,我向您展示了如何使用内置在手机中的 GSM 调制解调器发送短信。 但是,我没有向您展示如何检测传入的短信。 要检测收到的新短信,这有点棘手,因为不同制造商的手机可能需要不同的配置。 在本文中,我将向您展示如何使用开源 atSMS 库检测新短信。
工作原理
为了检测传入的短信,您必须使用“AT+CNMI”命令将新消息指示设置为终端设备 (TE)。
对于“+CNMI”命令,值格式为 mode,mt,bm,ds,bfr
。 为了接收短信,mt
必须为 1。 对于其他值,不同的手机可能需要设置不同的值。 默认情况下,atSMS 库将 mode
和 mt
设置为 1,这对于大多数手机来说应该足够了。 但是,您始终可以使用 CheckATCommands
函数来检查您的手机支持的值。
请注意,在我的测试过程中,注意到只有某些诺基亚手机才能检测到 Class 2 短信。 对于较新的诺基亚手机型号,例如 N70、N80,“+CNMI”不受支持,您无法检测到传入的短信。
代码
通过将 NewMessageIndication
设置为 True
,如果您的手机支持“+CNMI”,则应引发 NewMessageReceived
事件。
Imports ATSMS
Public Class MainForm
Private WithEvents oGsmModem As New GSMModem
Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub btnPhone_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnConnect.Click
If cboComPort.Text = String.Empty Then
MsgBox("COM Port must be selected", MsgBoxStyle.Information)
Return
End If
oGsmModem.Port = cboComPort.Text
If cboBaudRate.Text <> String.Empty Then
oGsmModem.BaudRate = Convert.ToInt32(cboBaudRate.Text)
End If
If cboDataBit.Text <> String.Empty Then
oGsmModem.DataBits = Convert.ToInt32(cboDataBit.Text)
End If
If cboStopBit.Text <> String.Empty Then
Select Case cboStopBit.Text
Case "1"
oGsmModem.StopBits = Common.EnumStopBits.One
Case "1.5"
oGsmModem.StopBits = Common.EnumStopBits.OnePointFive
Case "2"
oGsmModem.StopBits = Common.EnumStopBits.Two
End Select
End If
If cboFlowControl.Text <> String.Empty Then
Select Case cboFlowControl.Text
Case "None"
oGsmModem.FlowControl = Common.EnumFlowControl.None
Case "Hardware"
oGsmModem.FlowControl = Common.EnumFlowControl.RTS_CTS
Case "Xon/Xoff"
oGsmModem.FlowControl = Common.EnumFlowControl.Xon_Xoff
End Select
End If
Try
oGsmModem.Connect()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Return
End Try
Try
oGsmModem.NewMessageIndication = True
Catch ex As Exception
End Try
btnSendMsg.Enabled = True
btnSendClass2Msg.Enabled = True
btnCheckPhone.Enabled = True
btnDisconnect.Enabled = True
MsgBox("Connected to phone successfully !", MsgBoxStyle.Information)
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
oGsmModem.Disconnect()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
btnSendMsg.Enabled = False
btnSendClass2Msg.Enabled = False
btnCheckPhone.Enabled = False
btnDisconnect.Enabled = False
btnConnect.Enabled = True
End Sub
Private Sub btnSendMsg_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendMsg.Click
If txtPhoneNumber.Text.Trim = String.Empty Then
MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
Return
End If
If txtMsg.Text.Trim = String.Empty Then
MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
Return
End If
Try
Dim msg As String = txtMsg.Text.Trim
Dim msgNo As String
If StringUtils.IsUnicode(msg) Then
msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
Common.EnumEncoding.Unicode_16Bit)
Else
msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
Common.EnumEncoding.GSM_Default_7Bit)
End If
MsgBox("Message is sent. Reference no is " & msgNo, _
MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message & ". Make sure your SIM memory" & _
" is not full.", MsgBoxStyle.Critical)
End Try
'Try
' Dim storages() As Storage = oGsmModem.GetStorageSetting
' Dim i As Integer
' txtStorage.Text = String.Empty
' For i = 0 To storages.Length - 1
' Dim storage As Storage = storages(i)
' txtStorage.Text += storage.Name & "(" & _
' storage.Used & "/" & storage.Total & "), "
' Next
'Catch ex As Exception
' txtStorage.Text = "Not supported"
'End Try
End Sub
Private Sub btnSendClass2Msg_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendClass2Msg.Click
If txtPhoneNumber.Text.Trim = String.Empty Then
MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
Return
End If
If txtMsg.Text.Trim = String.Empty Then
MsgBox("Phone number must not be empty", MsgBoxStyle.Critical)
Return
End If
Try
Dim msg As String = txtMsg.Text.Trim
Dim msgNo As String
If StringUtils.IsUnicode(msg) Then
msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
Common.EnumEncoding.Unicode_16Bit)
Else
msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, _
Common.EnumEncoding.Class2_7_Bit)
End If
MsgBox("Message is sent. Reference no is " & msgNo, _
MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message & ". Make sure your SIM memory is not full.", _
MsgBoxStyle.Critical)
End Try
'Try
' Dim storages() As Storage = oGsmModem.GetStorageSetting
' Dim i As Integer
' txtStorage.Text = String.Empty
' For i = 0 To storages.Length - 1
' Dim storage As Storage = storages(i)
' txtStorage.Text += storage.Name & "(" & _
' storage.Used & "/" & storage.Total & "), "
' Next
'Catch ex As Exception
' txtStorage.Text = "Not supported"
'End Try
End Sub
Private Sub oGsmModem_NewMessageReceived(ByVal e As _
ATSMS.NewMessageReceivedEventArgs) Handles _
oGsmModem.NewMessageReceived
txtMsg.Text = "Message from " & e.MSISDN & ". Message - " & _
e.TextMessage & ControlChars.CrLf
End Sub
Private Sub btnCheckPhone_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCheckPhone.Click
MsgBox("Going to analyze your phone. It may take a while", _
MsgBoxStyle.Information)
oGsmModem.CheckATCommands()
If oGsmModem.ATCommandHandler.Is_SMS_Received_Supported Then
MsgBox("Your phone is able to receive SMS. Message " & _
"indication command is " & _
oGsmModem.ATCommandHandler.MsgIndication, _
MsgBoxStyle.Information)
oGsmModem.NewMessageIndication = True
Else
MsgBox("Sorry. Your phone cannot receive SMS", _
MsgBoxStyle.Information)
End If
End Sub
End Class
在下一篇文章中,我将向您展示如何使用 atSMS 库发送 vCard、vCalendar 和 WAP Push 消息。