Messaging & Communication from Mobile Devices .NET Mobile Application Development

Download Report

Transcript Messaging & Communication from Mobile Devices .NET Mobile Application Development

.NET Mobile Application Development
Messaging & Communication from
Mobile Devices
Introduction
 In previous lectures and practical sessions we have
considered
> Developing applications using the .NET Compact Framework
> Consuming XML Web service in mobile applications
 In this session we will consider other forms of
communication from mobile devices, including
> Message queuing
> SMS (text) messaging
> Telephone features
Mobile Device Communication
 Mobile devices can consume distributed computing

technologies used by desktop devices
Unlike desktops, mobile devices also have a rich suite of
communication facilities
> Voice telephony
> GSM/GPRS data transfer
> SMS/EMS/MMS messaging
> Ad-hoc Bluetooth networking
 Mobile devices expose these communication facilities to
the developer
> A basis for rich, flexible applications?
Windows Mobile Platform
 We will use the Windows Mobile (WinCE) platform to


illustrate the use of these communication facilities
Other platforms, e.g. Java MicroEdition also offer similar
features to the developer
Microsoft Mobile Platform
> derivative(s) of Windows CE .NET
> Operating System contains API’s to access the unique hardware
capabilities and communication facilities of mobile devices
- API’s are unmanaged code; no managed equivalents exist
- we must use the P/Invoke feature to use these features from .NET
Compact Framework
Platform Invoke (P/Invoke)
 P/Invoke
> allows managed code to invoke unmanaged functions residing in DLLs
 Three stages involved
> Declaration
- Specify signature of unmanaged function that will be called as a static extern function
- Uses the DllImportAttribute tag, including the DLL name
> Invocation
- Unmanaged function invoked by calling it as normal, specifying any parameters as types
expected by unmanaged code
- May need to declare managed types to replicate unmanaged ones and transform between
them as necessary
- Unmanaged function invocation often wrapped in method of utility class
> Error handling
- P/Invoke may generate MissingMethodException or NotSupportedException
- Unmanaged DLL may produce an error; retrieve error number using
Marshal.GetLastWin32Error()
P/Invoke Data Marshalling
 Marshalling
> Process of moving data between managed and unmanaged code
> Data passed by value or by reference
> Automatic for value types, reference types composed of simple value types, one dimensional
arrays of simple types
> Programmer must assist in marshalling more complex types e.g. reference types composed
of reference types
C# Type
C++ type, pass by value
C++ type, pass by ref
byte
BYTE, char
BYTE*, char*
short, ushort
SHORT, WORD
SHORT*, WORD*
int, uint
int, DWORD
int*, DWORD
long
unsupported
INT64*
float
unsupported
float*
double
unsupported
double*
IntPtr
PVOID
PVOID*
bool
BYTE
BYTE*
string
LPCWSTR
unsupported
P/Invoke Example
 Retrieving power status of a mobile device
> Uses the unmanaged GetSystemPowerStatusEx function in the
“coredll” DLL
 Declaration
> using System.Runtime.InteropServices; to import P/Invoke functionality
> Managed SYSTEM_POWER_STATUS_EX type to hold data values produced by the
call to GetSystemPowerStatusEx
> Signature of GetSystemPowerStatusEx, with DllImportAttribute tag
 Invocation
> Need to pass to the function a boolean flag and an instance of
SYSTEM_POWER_STATUS_EX which it will populate with appropriate data values
 Doing something similar on a Java platform
> is significantly more complicated
> requires much more plumbing code and design time effort
Message Queuing & Mobile
Devices
 Message Queuing is a useful technique for asynchronous
communication between distributed components
 Windows CE supports Message Queuing through unmanaged
operating system functions in coredll.DLL
>
>
>
>
>
CreateMsgQueue
OpenMsgQueue
CloseMsgQueue
ReadMsgQueue
WriteMsgQueue
 P/Invoke needed to call these from managed code
 Message queues are
> fast and thread safe
> useful for interprocess communication
Message Queuing Example
 Uses the unmanaged functions in the “coredll” DLL
> CreateMsgQueue
> ReadMsgQueue
> WriteMsgQueue
 Follows typical approach to using P/Invoke
> Declares managed types to correspond to required unmanaged types
> Places calls to unmanaged code in a managed wrapper
> Clients of the wrapper do not need to know that they are using with unmanaged
code
> All handling of errors due to unmanaged code occurs in one place
 One thread creates a message queue and sends a message to it
 Second (UI) thread reads messages from the queue
Making Voice Calls
 All mobile phones support voice calls
> Programmatic control is possible
 Windows Mobile has Phone API for controlling
voice calls
> Unmanaged API; need to use P/Invoke
> Good example of a P/Invoke wrapper for the Phone API on MSDN
site
- Enables placing of calls and retrieving information from SIM
card
- Our example uses this to create a simple form application for
placing a phone call to a chosen number
SMS Messaging
 Short Message Service (SMS)
> Store and forward text messaging system supported by all mobile phones
> Maximum 160 character text message
> Messages routed through Short Message Service Centre (SMSC) to
recipient’s phone
- Acts as a Post Office; queues messages for later delivery if recipient is
unavailable
> Two-way communication; replies possible
 Enhanced Message Service (EMS)
> allows messages to include formatted text, pictures, animation, sounds and
ring tones.
 Programmatic control possible for
> Sending SMS messages
> Processing received SMS messages
Windows Mobile SMS
Messaging
 Unmanaged SMS API provided by Windows CE
 Good sample wrapper on MSDN site
> Shows basics of setting up the SMS messaging component and sending a message
> Our example uses this wrapper to send a message to a chosen phone number
 Processing of incoming SMS messages is also possible
 Incoming SMS messages are placed in device Inbox
> SMS API has pluggable architecture
- Developer can write plug-in filter through which SMS messages pass on way to
Inbox
- Allows incoming SMS messages to be trapped, handled and removed by an
application before they reach the Inbox
- Allows SMS messaging to be used as a general purpose bi-directional
asynchronous communication channel between mobile clients
Demo: SMS Battleships
Game
 Demo given at Microsoft Mobility Developer conference
2003
> CLI201 SMS Message Interception Demo
 Shows use of SMS for general communication amongst
distributed components on mobile devices
 Free SMS sending services exist on the Web
> Calling these appropriately from a desktop app could provide a
unidirectional communication channel to mobile clients
Summary
 In this session we have discussed
> Calling unmanaged code from managed applications using P/Invoke
> Typical communication features available on mobile devices
> Programmatically accessing these features using P/Invoke
 In the next session we consider
> Using and manipulating data in .NET applications
> Accessing databases in .NET
> Database support features for mobile devices
Reading and Resources
Reading

Wigley & Wheelwright, Microsoft .NET Compact Framework Core Reference, Microsoft Press,
2003
Resources

Using Connection Manager to Establish Data Calls,
http://msdn.microsoft.com/library/en-us/dnppcgen/html/conmgrdtac.asp?frame=true

Sending SMSs from your Microsoft .NET Compact Framework-based Applications,
http://msdn.microsoft.com/library/enus/dnnetcomp/html/netcfsendsms.asp?frame=true

Accessing Phone APIs from the Microsoft .NET Compact Framework,
http://msdn.microsoft.com/library/enus/dnnetcomp/html/netcfphoneapi.asp?frame=true

An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact
Framework, http://msdn.microsoft.com/library/enus/dnnetcomp/html/netcfintrointerp.asp?frame=true

Creating a P/Invoke Library, http://msdn.microsoft.com/library/enus/dnnetcomp/html/PInvokeLib.asp?frame=true