Post on 31-Mar-2015
Insight
Developing for Mobile DevicesRob MilesDepartment of Computer Science
Overview
Windows Mobile platforms> Introduction to the history and capabilities of mobile devices
Visual Studio 2005 and mobile devices> Practical : Creating a sample application (Secret Encoder)
Advanced Programming> Creating Casual Games for Mobile Devices> Practical : A working mobile game – StarLight space shooter
Windows Mobile Platforms
Introduction
Before we look at the process of development, we should look at the platforms themselves
> The origins of the Pocket PC and Smartphone> What constitutes state of the art at the moment> What the future holds
Pocket PC
Originally called the “wallet PC”
Launched in 1996 with Version 1.0 of Windows CE
Superseded in the following year with Version 2.0
Initially available in keyboard and keyboard-less versions
Breakthrough Pocket PC:Compaq Ipaq
This was the first device to deliver on performance, display and battery life
> 120MHz processor
> 32MB of RAM
> Flash ROM
> RS232 and IR ports
> 240x320 TFT colour display
State of the art Pocket PC
Dell Axim V50s> 624Mhz Processor> 64MB RAM> 480x640 colour display> WIFI and Bluetooth> 3D Graphics accelerator
Windows Mobile 5 £150 less than the original
IPAQ!
Smartphone
In 2001 Microsoft announced that it would be launching a range of Smarpthones (codenamed "Stinger")
The Smartphones would be "Windows based"
The version of Windows in question was Windows CE 3.0
Launched in October 2002 with Orange SPV
Breakthrough Smartphone:SPV E200
First to deliver on performance and battery life:
> 32MB user memory> Built in Bluetooth support> Built in camera > Smartphone 2003 Operating System> .NET Compact Framework
State of the art Smartphone: SPV C600
Launched in August 2005> 64MB of internal memory> Fast internal processor (200 MHz)> Small form factor > Runs Windows Mobile 5
The Imate SP5 version of this platform also has WIFI
Pocket PC Phone Edition
Combines PDA with phone> Launched in July 2002> 206-MHz Intel StrongArm
processor> 32MB RAM> Based on Pocket PC 2002
Breakthrough Device: XDA II
Launched in April 2004> 400MHz Processor> 128MB RAM> Camera> Bluetooth> Windows Mobile 2003
State of the Phone Edition:XDA IIS
Launched in April 2005> Integrated Keyboard> Improved performance
Imate Jasjar
First 3G Windows Mobile Device
> Branded as Orange M5000> VGA resolution display
(640x480)> WIFI support> Two cameras> Windows Mobile 5.0
A viable desktop alternative
Other devices of note
Palm Treo> First Palm Windows Mobile 5.0 device> Available in USA with broadband network
speed> Being positioned as a “Blackberry killer"
Imate JAM> Very small Pocket PC phone edition> New form factor for Pocket PC
The Future…
The development of the devices over the last few years has been amazing
The systems are going to get more powerful and more connected
Location based behaviour is now very easy to implement in systems
Visual Studio 2005 and mobile devices
Introduction
The key components The Smartdevice project type Language and Development options The project as an executable program Deploying to a target Simple debugging
Key Development Components
ActiveSync 4.1> connects the Smartphone device to the PC
Visual Studio 2005> provides development environment
Smart Device Developer Kits> provides development project types
SOTI Remote commander> provide a remote viewer for the mobile device
ActiveSync 4.1
The precise version is important Manages the link between the PC and the
Smartphone Can use wired (USB or RS232), infrared
(IRDA) or Bluetooth connection to the developer PC
You do not need to partner with a device in order to develop for it
ActiveSync Deployment
Visual Studio uses an ActiveSync connection to deploy programs and debug them
ActiveSync sets up a TCP/IP connection to the target device
If the connection does not support a native TCP/IP connection a private subnet is set up
ActiveSync Private Subnet
The ActiveSync connection establishes an IP address for the connected device:
> PC Emulator: 192.168.131.75> USB Connected Device: 192.168.55.101
These connections allows programs on the Smartphone to originate connections
ActiveSync 4.1 will also connect to emulators and synchronise with them
Connection Limitations
The private subnet set up by ActiveSync does not allow external programs to originate a connection to the device
This is not a problem, since any application you write will run on a phone which you will be unable to connect to in this way because the GPRS IP address will not be known
If two Smartphones are to exchange information over TCP/IP this should be via a third party server or by using web services
ActiveSync Connections
Universal Serial Bus (USB)> All Smartphones are shipped with a USB cable or cradle for
connection to a PC Infra Red (IRDA)
> Most Smartphones have an IRDA connection which can be used for ActiveSync
Serial port (RS232)> Some Smartphones are also equipped with a serial port which can
be used by ActiveSync. Note that programs on the Smartphone may also make use of this connection
Bluetooth> ActiveSync can connect using the Bluetooth serial port
implementation. This is configured as a serial port connection
Preferred Connections
USB is the most reliable Bluetooth is the most convenient, but can be
problematic to set up and maintain IRDA works surprisingly well, particularly with
notebook and tablet PCs The serial port connection will require a
special cable, and is declining in popularity
Visual Studio 2005
This provides the development environment for the mobile devices
Can create .dll libraries and executables Produces both managed an unmanaged
(native) code Also supports remote debugging in the target
(on both emulator and device)
SOTI Remote Commander
Earlier versions of the mobile operating systems supported remote control tools which were provided in the Mobile Powertoys
> Window Mobile 5.0 does not support these
To write applications which use the target hardware (camera, phone, SMS messages) you need remote access to the device
I use SOTI Remote Commander: (www.soti.com)
Inside a Compact Framework Program
Because the executable is a .NET program you can use ILDASM and other tools to manipulate it
You could even create MSIL applications for the Smartphone if you wish
Deploying the Program
When the program is executed from within Visual Studio it is copied into a directory on the target device and executed from there
You can deploy the program just as an executable file if you wish, by copying it into a directory on the Smartphone using ActiveSync
Finding the Program
The more recent mobile phones are shipped with file browser programs you can use to find and run the executable directly
Pocket PC owners can use the built in file browser
Stopping a program on the target
The program can be stopped at any time If the program is running on a remote device it
will also be stopped If the ActiveSync connection fails Visual
Studio will display an error, but the program will continue to execute on the target device
Selecting the Target Device
The target device is selected from within Visual Studio
If you try to use a real device, and none is connected, the deployment will take a while to time out
Note the different display form factors
Debug and Release Mode
The program can be compiled for Debug or Release mode
This is selected from within Visual Studio
When using Debug mode a Program Debug Database is created alongside the program executable file
Debugging
The debug tools are very powerful You can:
> Pause an executing program> Add a breakpoint to an executing program without stopping it
These facilities work on the target device too You must however have used Debug mode to
build the application The Remote Display Power tool can be useful
here
The Configuration Manager
You can create your own forms of configuration to customise your development
The manager is accessed from the Build menu
Project Properties
The Project Properties give you an even finer level of control of the project
This includes where on the target device the program is actually deployed
Smartphone Games Development
Secret Encoder Program
We are going to explore the development process by developing a simple program for the mobile platforms
This will allow us to explore input/output and program design
The work could be the basis of a student practical exercise
Introduction
The “Secret Encoder” Program
> Encodes text entered, based on a simple keyword
Will allow us to explore the issues of mobile development
> User Input> Output Display> Use of the Menu Buttons> Program termination
Encoder Program
Screen contains three areas:> Encryption key entry> Secret message entry> Output display entry
When the code button is pressed the key is applied to the message to produce the result
The encryption is performed using XOR, so that it is reversible
Key: mykeytext
In: password
Out: xwbqegg231
Code
Getting Started
Once the Smartphone extensions have been loaded you can create a Smart Device application
Call itSecretEncoder
Good Practice
Rename the source file from Form1 to SecretEncoderForm
> Note that the refactoring process automatically offers to rename the classes as well
Then change the text on the form from Form1 to Secret Encoder
> Use the properties of the form to do this
Encoder Project
First we have to add the data entry components for the program
The Visual Studio Toolbox holds only the components which are available for the Smartphone display
We are going to use Label and TextBox components
Encoder Form
Now add four Label and two TextBox components onto the form
Make sure they are given sensible names and aligned correctly
Field Traversal
The Smartphone application begins running with the most recently added TextBox selected first
Moving “forwards” through the fields actually moves towards the “oldest” TextBox
This might not be what the user will want You should add the lowest TextBox first
> Or make use of the tool to change the field order
Smartphone Tab Order
The Smartphone Tab order dialogue, selected from the View menu allows you to re-arrange the tab order of the fields on the form
Click on the fields in the order you want
Data Entry
The present program will run, and the user can move between the two entry boxes and type text into either
We now need a way of triggering the encode action
Adding Menu Keys
To add a menu key just click on the screen above the key and type the name of the key
The menu item is created automatically
Menu Keys Names
Once you have added the keys to the form you can type menu selections onto them by clicking on the “Type Here” item
Smartphone Menu
The menu key is displayed as shown
We now need to bind an event to the key press to perform our encode action
Menu Names
I rename menus (and other components) so that the names of their event handlers make sense
You should do this as soon as you have created one
Adding the Event Handler
Double click on the menu item to create an event hander for it
Encode Action
I have created a method to perform the encode action I could have placed this code inside the event hander
itself, but it is more flexible to create a method to do the job:
This calls an encode method which actually performs the translation of the text itself
Testing the program
The initial version of the encode method simply returns the original text
This allows me to test the program and ensure that it works correctly
The Encode method
Secret Encoder
The encryption works, but it is a very weak method
Note that the space in the input reveals the key character at that position
I leave it to you to create a better one!
Exiting the program
At the moment there is no way to exit the program on the Smartphone device
We can stop it with Visual Studio, but the user will not be able to do that
It is very easy to add an exit menu option
Stopping an Application
We can use the standard application termination method to stop the program:
This frees off any resources and exits the program cleanly
Note that the Windows CE guidelines aren’t keen on you stopping your programs (but I do it anyway)
TextBox Key Entry
You may wish to not show the password when you are using the program
As with standard Windows Forms, the TextBox component can be set to allow password entry
Debugging
The program can be debugged in exactly the same manner as any other Visual Studio application
Any exceptions which are thrown are trapped and you are given the option to debug
Running on the PC
The application will run on a standard PC
Note how the behaviour of the menu has been adapted for a Windows Form
It is often useful to be able to run programs on a PC to test them
> Particularly if they make use of file input/output
Labels and Bugs
There is a bug in this program; some characters are interpreted by the label display component as controls for access keys (even though these have no meaning for Smartphone!)
The key of “cheese” gives the output as shown
TextBox Replacement
Using a TextBox set to read only allows the correct text to be displayed
The read only property of the text box is used to prevent the user changing it
Pocket PC Version
The Pocket PC version is very similar
Just about all of the Smartphone code can be transferred directly over to Pocket PC
Changing the target is very easy
Migrated Code
The Pocket PC has menu keys just like the Smartphone
The program can therefore be used unchanged
Workspaces
You can combine several device projects into a single workspace
The common behaviour (the encoder) has been brought out into a separate project
We will explore this later
Development
You can explore further development of this code:
> Add buttons to the Pocket PC implementation> Store the key in a file on the device> Allow the use of multiple keys> Improve the encoder quality
Advanced DevelopmentOverview
Windows Mobile 5.0 and Casual Games Writing code for Windows Mobile 5.0 Adding compelling features:
> Mobile graphics> Personal Information Management> Messaging> Camera integration
A complete sample game
What is a Casual Game?
A casual game should be: > quick to play > with you all the time > preferably quick to write
Windows Mobile 5.0 is a great platform to create casual games:
> wide range of mobile platforms> powerful and effective programming tools> gives the security of managed code
Windows Mobile 5.0
Common to all the hardware platforms Provides easy access to underlying device
properties and events:> Device configuration> Camera controls> Direct3D> Outlook PIM information> Messaging and Email> GPS device support
Writing GamesSoftware Deployment
For devices containing phones the service provider may lock the phone
To run an application on a locked phone it must be signed
> Most phones can be unlocked for development
Use the Mobile2Market program to sell your code
Writing GamesCheese Breakout
The basis of the game is to bounce the cheese and hit the tomatoes
The player controls the bread bat and hits the cheese with it:
Sprites> transparency > double buffering> animation
User input> event driven keypad
Bouncing CheeseDrawing the cheese
private void Form1_Paint(object sender, PaintEventArgs e){ e.Graphics.DrawImage(cheeseImage,cx,cy);}
private void timer1_Tick(object sender, EventArgs e){ updatePositions(); Invalidate();}
I use the timer tick method to trigger updates I use the timer tick method to trigger updates
Bouncing CheeseUpdating the cheese position
private void updatePositions(){ cx+=xSpeed; if ( ((cx + cheeseImage.Width) >= this.Width) || (cx <= 0) ) { xSpeed *= -1; }
cy+=ySpeed; if ( ((cy + cheeseImage.Height) >= this.Height) || (cy <= 0) ) { ySpeed *= -1; }}
Moving Cheese
Writing GamesUser Input
The event driven Windows Forms input model lends itself well to games
> Events are fired on key down and key up
However, each platform has some input limitations:> No touchscreen on the Smartphone> Smartphone joypad does not support diagonals
Writing GamesWindows Mobile 5.0 Graphics
You can create 2D graphics using GDI> This is perfect for 2D sprite based games
You can also access the screen directly> But the GAPI interface has been deprecated
For 3D graphics you can use Direct3D:> Some devices have hardware acceleration
Dell Pocket PC Coming soon to Smartphones
Writing GamesStructuring your solutions
Windows Mobile 5.0 programs can run on any of the platforms
But you do need to address control issues> The input mechanisms for Pocket PC and Smartphone are
necessarily different
But you can structure your code to address this
Writing GamesStructuring your solutions
The interfaces isolate the game engine from the particular platform
Smartphone UISmartphone UI Pocket PC UIPocket PC UI Pocket PC Pocket PC Phone UIPhone UI
↑↑↑↑ ↑↑↑↑ Interfaces ↓↓↓↓Interfaces ↓↓↓↓
Underlying Game EngineUnderlying Game Engine
Writing GamesStructuring your solutions : UI
The UI components expose their behaviours by means of an interface
The game engine uses this to drive the display
public interface IBlackJackUI{ void DoRedraw(); void DoSettingBet(); void ShowBets(); void DoStartPlayerHand(); void DoStartDealerHand(); void ShowPlayerBust(); ......}
Writing GamesStructuring your solutions : Game
This is how the UI code views the game engine
It passes user events into the engine which then generates display events in response
public interface IBlackJackGame{ void DoHitPlayer(); void DoNewGame(); void DoPlayerStay(); void DoStartHand(); GameMode Mode { get; } ......}
Writing GamesSolutions and Projects
The solution contains Windows PC and Smartphone games
They both use the same game engine
A Pocket PC version would be very easy to add
Blackjack Game
Writing GamesMultiple Platforms
You can create a single binary which will work on multiple platforms
It can determine the actual dimensions of the target and then scale appropriately
Beware of issues with transparent colors when they are scaled however
Can load multiple versions of game images if required
Writing GamesDiscovering Device Configuration
There are a wide range of platforms and configurations
It is easy to find out what is available on the platform in use
Device propertiesFinding the camera
using Microsoft.WindowsMobile.Status;......
bool hasCamera = Convert.ToBoolean( SystemState.GetValue(SystemProperty.CameraPresent) );
if (hasCamera) { cameraMessageLabel.Text = "Camera Present";}else { cameraMessageLabel.Text = “No Camera";}
Camera Finder
Writing GamesDevice Events
You can also bind your code to events generated when a given property changes
Every event which is managed by the system can be monitored in this way
> Battery: level/power supply> Media: currently playing song > Connectivity: network, phone, cradle
This is very easy to use
Writing GamesBinding to the headset event
using Microsoft.WindowsMobile.Status;......
SystemState headsetState;public HeadsetForm(){ InitializeComponent(); headsetState = new SystemState(SystemProperty.HeadsetPresent); headsetState.Changed += new ChangeEventHandler(headsetState_Changed);}
Writing GamesThe headset event handler
void headsetState_Changed(object sender, ChangeEventArgs args){ bool gotHeadset = Convert.ToBoolean(args.NewValue); if (gotHeadset){ headsetPresentLabel.Text = "Headset Present"; } else{ headsetPresentLabel.Text = "No Headset"; }}
Headset Detector
Compelling FeaturesUsing the Outlook PIM
Windows Mobile 5.0 provides direct access to the Outlook on the mobile device
This was previously achieved using the Pocket Outlook Object Model (POOM) but with Windows Mobile 5.0 this is built in
Making use of contact details and sending messages is now very easy
Using OutlookThe OutlookSession class
OutlookSession outlook = new OutlookSession();
System.Collections.Generic.List<Contact> mobileCcontacts = new System.Collections.Generic.List<Contact>();
void findMobileContacts(){ foreach (Contact c in outlook.Contacts.Items) { if (c.MobileTelephoneNumber != "") { mobileCcontacts.Add(c); } }}
Using OutlookSending an SMS message
SmsMessage sms = new SmsMessage(contact.MobileTelephoneNumber, message);try { sms.Send(); MessageBox.Show("Message Sent OK");}catch { MessageBox.Show("Send Message Failed");}
Addressbook Roulette
Compelling FeaturesIncoming messages
It is very easy to create code which will react to messages when they arrive
In this way you can create games which will act on SMS messages or incoming emails
The event code should be quite familiar
Incoming MessagesBinding to the event
mgot = new MessageInterceptor();mgot.InterceptionAction = InterceptionAction.NotifyAndDelete;mgot.MessageReceived += new MessageInterceptorEventHandler(mgot_MessageReceived);......void mgot_MessageReceived(object sender, MessageInterceptorEventArgs e) { Message mess = e.Message; if ( mess.GetType() == typeof(SmsMessage) ) { SmsMessage sms = mess as SmsMessage; if (sms.Body == "bang") { MessageBox.Show("BANG!"); } }}
Phone Exploder
Compelling FeaturesActing on incoming messages
It is also possible to use an incoming SMS to trigger an application to start up
This is done via the properties of the MessageInterceptor instance that is created
Compelling FeaturesUsing the Camera
If the device has a camera you can use this in your games
You do not have direct control over the device itself > This is to protect user privacy
But you can integrate the use of the camera into games that you write
Using the cameraThe camera dialog
The camera is used in a way directly analogous to a File dialog
It returns the name of the file which contains the picture as taken
You can also configure the camera The actual behaviour of the dialog is platform
dependent
Using the cameraPicture taking method
static public string TakePicture(Form parent){ string result = null; CameraCaptureDialog camera = new CameraCaptureDialog(); camera.Mode = CameraCaptureMode.Still; if (camera.ShowDialog() == DialogResult.OK){ result = camera.FileName; } parent.Show(); camera.Dispose(); return result;}
Using camera imagesPicking Pictures
There is also a SelectPictureDialog This works in exactly the same way as the CameraCaptureDialog
It allows the player to choose a picture It also provides a camera capture option if the
device has a camera For this reason I prefer it
Play with Friends
The Play With Friends demoGraphical notes
The game has around 20 or so sprites on the screen at any time
> Small sprite class linked to a few images
They are only created once at the start of the program to reduce garbage collection
A backbuffer is used to implement the scrolling background
> Runs at around 20 Hz on the SPV C600
Resources
Games Programming with Cheese> Now being upgraded to Windows Mobile 5.0> Search msdn for “Smartphone cheese”
Windows Mobile 5.0 SDK> http://msdn.microsoft.com/mobility/> http://blogs.msdn.com/markprenticems/
All the demos are available for download> http://www.robmiles.com/files