Showing posts with label windows phone 7. Show all posts
Showing posts with label windows phone 7. Show all posts

Monday, 11 April 2011

WP7 Tutorial - part 5: Orientation and Acceleration - X,Y,Z

Detecting gestures, orientations, and movement can be realized with the accelerometer. The accelerometer is a sensor that measures the acceleration in 3 dimensions (X, Y, and Z). If the device is not moved the accelerations measure are the gravity forces in each direction. If the device is accelerated the measured results are a combination of the acceleration and the gravity.

The accelerometer data can be accessed via the AccelerometerReadingEventArgs class. The class has values for the X, Y, and Z axis. The values are of type double and between -2 and 2 which related to acceleration "for each axis in gravitational units" - 1 is the gravitational force of the earth. See: http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx and http://msdn.microsoft.com/en-us/library/ff431810(v=vs.92).aspx or on page 80ff, C. Petzold, Programming Windows Phone 7.

A typical exercise for understanding the accelerometer is to create a bubble level (a tool to measure if something is horizontal or vertical - e.g. for hanging pictures on the wall). You probably want to freshen up on arctan2 - at least I needed ;-)

See below the c# example reading out the accelerometer on a windows phone 7. You can also download the accelerometer project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

// A simple example to read the accelerometer and display the values
// In order to make it work you have to add the refercerence to 
// Microsoft.Devices.Sensors to your project. To do this right-click
// in the Solution Explorer on References and than choose add Reference
// in the dialog then select Microsoft.Devices.Sensors
// Albrecht Schmidt, University of Stuttgart

// for a more comprehensive example see: 
// http://msdn.microsoft.com/en-us/library/ff431810(v=vs.92).aspx
// http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
// and page 80ff, C. Petzold, Programming Windows Phone 7

namespace Accl_X_Y_Z
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer accelerometer;
    
        public MainPage()
        {
            InitializeComponent();
            // create a new instance
            accelerometer = new Accelerometer();
            // register a callback function for when values change
            accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
            // start the accelerometer
            accelerometer.Start();
        }

        void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            // required as from here the textBlocks cannot be accessed
            Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
        }
        
        void ChangeUI(AccelerometerReadingEventArgs e)
        {
            // show the values on the screen
            textBlock1.Text = "X: " + e.X.ToString("0.000");
            textBlock2.Text = "Y: " + e.Y.ToString("0.000");
            textBlock3.Text = "Z: " + e.Z.ToString("0.000");
        }
    }
}

WP7 Tutorial - part 4: Storing Data on the Phone

If you want to save high scores or preferences you need persistent memory on the phone. On a traditional computer you would create a file and store your information in the file; another option on a Windows PC would be store such information in the registry. For security reasons there is no API to access the file system and there is no global persistent memory across applications on a WP7.

In general there are two ways to store data: (1) in a application specific storage (isolated storage) on the phone or (2) remotely on the internet (or to use another buzzword "in the cloud").
In this example the use of the phone isolated storage API is demonstrated. It is shown how to store and retrieve name-value pairs on the phone (to people who programmed Java ME this is conceptually similar to the record store).

For more details see: http://msdn.microsoft.com/en-us/library/cc221360(v=VS.95).aspx and page 126ff, C. Petzold, Programming Windows Phone 7. It is also possible to created an isolated storage for files see: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(v=VS.95).aspx

See below the c# example using the local storage on a windows phone 7. You can also download the IsolatedStorageSettings project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

// example of how to save to and load from the isolated application storage 
// this helps to create persistence storage within a single application
// in this example it is shown how to do it for a string 
// Albrecht Schmidt, University of Stuttgart  


// For more details see:
// http://msdn.microsoft.com/en-us/library/cc221360(v=VS.95).aspx
// page 126ff, C. Petzold, Programming Windows Phone 7

// storing files/directory structures see:
// http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(v=VS.95).aspx

namespace PhoneStorage
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        #region Save and Load Parameters from the Application Storage
        void saveToAppStorage(String ParameterName, String ParameterValue)
        {
            // use mySettings to access the Apps Storage
            IsolatedStorageSettings mySettings = IsolatedStorageSettings.ApplicationSettings;

            // check if the paramter is already stored
            if (mySettings.Contains(ParameterName))
            {
                // if parameter exists write the new value
                mySettings[ParameterName] = ParameterValue;
            }
            else
            {
                // if parameter does not exist create it
                mySettings.Add(ParameterName, ParameterValue);
            }
        }

        String loadFromAppStorage(String ParameterName)
        {
            String returnValue = "_notSet_";
            // use mySettings to access the Apps Storage
            IsolatedStorageSettings mySettings = IsolatedStorageSettings.ApplicationSettings;

            // check if the paramter exists
            if (mySettings.Contains(ParameterName))
            {
                // if parameter exists write the new value
                mySettings.TryGetValue<String>(ParameterName, out returnValue);
                // alternatively the following statement can be used:
                // returnValue = (String)mySettings[ParameterName];
            }

            return returnValue;
        }
        #endregion

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            saveToAppStorage("myIdentifer1", "Last used @ " + System.DateTime.Now.ToString("HH:mm:ss"));
            textBox1.Text = "saved...";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = loadFromAppStorage("myIdentifer1");
        }

    }
}

Friday, 25 March 2011

WP7 Tutorial - part 3: Using Location

In this example the use of the location API is demonstrated. The API is a high level interface to geo location. How the location is determined (e.g. GPS, GSM cell information) is of no concern to the developer.

The basic approach is to create an instance of GeoCoordinateWatcher and register two callback functions: one for when the status changes and one for when the location changes. The program demonstrates how these call backs are set up and how from within those function the user interface is updated with the received information. If the status is changes, the program checks what the current status is, and shows this in the status line (textBlock8.Text). If the position is changed then the new position information (Position.Location.Longitude, Position.Location.Latitude) - and additional information such as Speed, Altitude, Course, Accuracy are shown.

As an exercise you can build an application that shows you how close you are to a given target. In two input fields you enter the longitude and latitude of the destination (e.g. a geo cache location). And then you can calculate the difference from the current position to the target location and visualize or sonify the distance.

There is another example (Geo coordinate watcher) how to use this API on the Microsoft msdn website. In C. Petzold's book there is also a good example, see page 91ff.

See below the c# example using geo location on a windows phone 7. You can also download the geolocation project directory in a single ZIP-file.

using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Phone.Controls;
using System.Device;
using System.Device.Location;

// the example shows the basic functionality of the location device
// you need to add in the solution explorer a reference to System.Device
// right click on References in the solution explorer, click Add Reference, and then
// System.Device
// Albrecht Schmidt, University of Stuttgart

// for a more comprehensive example see:
// http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinatewatcher.aspx
// http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
// and page 91ff, C. Petzold, Programming Windows Phone 7

namespace Geo_Location
{
public partial class MainPage : PhoneApplicationPage
{
GeoCoordinateWatcher watcher;

// Constructor
public MainPage()
{
InitializeComponent();
}

// the initialize and start button is pressed
private void button1_Click(object sender, RoutedEventArgs e)
{
// initialize the geo watcher with defaul accuracy (battery saving)
// user GeoPositionAccuracy.High for higher accuracy
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
// set movement threhold - as distance in meters - default is 0
watcher.MovementThreshold = 10;

// add a handler that is called when position is changed more than MovementThreshold
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
// a handler for status change
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);

// Start reading location data
watcher.Start();
}

void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
// you cannot change the UI in this function -> you have to call the UI Thread
Deployment.Current.Dispatcher.BeginInvoke(() => ChangeStatusUI(e));
}

void ChangeStatusUI(GeoPositionStatusChangedEventArgs e)
{
String statusType="";
if ((e.Status) == GeoPositionStatus.Disabled)
{
statusType = "GeoPositionStatus.Disabled";
}
if ((e.Status) == GeoPositionStatus.Initializing)
{
statusType = "GeoPositionStatus.Initializing";
}
if ((e.Status) == GeoPositionStatus.NoData)
{
statusType = "GeoPositionStatus.NoData";
}
if ((e.Status) == GeoPositionStatus.Ready)
{
statusType = "GeoPositionStatus.Ready";
}
textBlock8.Text = statusType;
}

void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
// you cannot change the UI in this function -> you have to call the UI Thread
Deployment.Current.Dispatcher.BeginInvoke(() => ChangeUI(e));
}

void ChangeUI(GeoPositionChangedEventArgs<GeoCoordinate> e)
{
textBlock1.Text = "Longitute: " + e.Position.Location.Longitude;
textBlock2.Text = "Latitute: " + e.Position.Location.Latitude;
textBlock3.Text = "Speed: " + e.Position.Location.Speed;
textBlock4.Text = "Altitude: " + e.Position.Location.Altitude;
textBlock5.Text = "Course: " + e.Position.Location.Course;
textBlock6.Text = "Vertical Accuracy: " + e.Position.Location.VerticalAccuracy;
textBlock7.Text = "Horizontal Accuracy: " + e.Position.Location.HorizontalAccuracy;
textBlock8.Text = "location updated at " + System.DateTime.Now.ToString("HH:mm:ss");
}

// the stop button clicked ... stop the watcher
private void button2_Click(object sender, RoutedEventArgs e)
{
if (watcher != null) { watcher.Stop(); }
textBlock8.Text = "location reading stopped";
}
}
}

Saturday, 19 March 2011

WP7 Tutorial - part 2: Vibration

This examples shows how to activate the vibration motor / vibration actuator in the phone. The calls Microsoft.Devices.VibrateController.Default.Start and Microsoft.Devices.VibrateController.Default.Stop are used to switch the actuator on and off.
When switching the vibration on the parameter sets the duration for which it should be on. The duration is between 0 and 5 seconds. With the function TimeSpan.FromMilliseconds(duration), where duration is a number, the parameter can be set easily.

The standard API only supports to switch on and off the vibration. We experimenting with the code you can explore how to have vibrations of different intensity. To do this you have to switch on and off the vibration (e.g. 100 ms on then 50 ms off) - basically doing pulse-width modulation.

There is more information on the vibration controller on the Microsoft site.

See below the c# example for controling the vibration a windows phone 7.
You can also download the vibration project directory in a single ZIP-file.

using System;
using System.Windows;
using Microsoft.Phone.Controls;

// example of how switch on the vibration motor for a given time
// another call to switch it off
// Albrecht Schmidt, University of Stuttgart

// see:
// http://msdn.microsoft.com/en-us/library/microsoft.devices.vibratecontroller.default(v=VS.92).aspx


namespace Vibration
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
double duration;
duration = Convert.ToDouble(textBox1.Text);
if (duration > 5000)
{
duration = 5000;
}
// starts the vibrations (valid duration are between 0 and 5 seconds)
Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromMilliseconds(duration));
}

private void button2_Click(object sender, RoutedEventArgs e)
{
Microsoft.Devices.VibrateController.Default.Stop();
}
}
}

Friday, 18 March 2011

WP7 Tutorial - part 1: Phone calls and SMS

Over the next weeks I like to share some examples I have created while learning to program the Windows Phone 7 platform. The tutorial is mainly explaining some of the APIs and components I found interesting (in particular related to context-awareness and human computer interaction).

If you are really new to programming on this platform the App-Hub is probably a good place to start. There are also plenty of helpful examples on the Microsoft web page. As prerequisite for this tutorial I assume that you have successfully installed Visual Studio (Express), the windows phone development tools, and that you have managed to get your first "Hello World" written, compiled, and deployed. There is also plenty of material on youtube that helps to get started. The Programming Windows Phone 7 book by Charles Petzold (free available as PDF) is also a good starting point.

Several examples that follow in the tutorial (e.g. vibration, accelerometer) having a real phone to test the programs is highly recommended.

The Windows Phone 7 is a phone and hence I start with a program that makes use of the basic phone functionality: making a phone call and sending an SMS.

This example also highlights the approach taken in several of the APIs. Your program (a third party application) is restricted to transfers control to the phones basic functions when making a call or sending an SMS. Basically the API call opens the phone/SMS applications with the parameters you hand over. From a security point of view this is nice as an application can not send SMS or do phone calls without the user recognizing (and agreeing to) it. From a programming perspective this has disadvantages as automation of functionality (e.g. always sent an SMS when I am coming closer than 10 km to home) is not possible.

The example demonstrates how to access interactively the phone book (using the PhoneNumberChooserTask), how to initiate a phone call task (using Microsoft.Phone.Tasks.PhoneCallTask), and how to initiate a SMS task (using Microsoft.Phone.Tasks.SmsComposeTask).
The PhoneNumberChooserTask is an example of a callback - used a lot when programming for WP7. The basic concept is to register a function that is called when an event happens. In this case the event is that the number is chosen and then the function myPhoneNumberChooser_Completed is called. The nice thing with Visual Studio is that you do not have to type this (or remember the syntax) - just use the TAB-key after you typed +=

See below the c# example for sending SMS and making a phone call. You can also download the Phone/SMS project directory in a single ZIP-file.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

// example how to invoke a phone call and an SMS from a program
// the basic approach is that you can set the phone number (and
// for SMS the message body) and then you call the phone/sms
// application
// Albrecht Schmidt, University of Stuttgart
// more on how to invoke phone tasks (photo, email, ...)
// http://msdn.microsoft.com/en-us/library/ff769543(v=vs.92).aspx

namespace Phone_Call_and_SMS
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}

// choose a phone number
private void button1_Click(object sender, RoutedEventArgs e)
{
PhoneNumberChooserTask myPhoneNumberChooser;
myPhoneNumberChooser = new PhoneNumberChooserTask();
myPhoneNumberChooser.Completed += new EventHandler&lt;PhoneNumberResult&gt;(myPhoneNumberChooser_Completed);
myPhoneNumberChooser.Show();
}

// call back after the phone number is choosen
void myPhoneNumberChooser_Completed(object sender, PhoneNumberResult e)
{
if (e.TaskResult == TaskResult.OK)
{
textBox1.Text = e.PhoneNumber.ToString();
}
}

// making a phone call (opening the sms application with presets)
private void button2_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.PhoneCallTask phonecall = new Microsoft.Phone.Tasks.PhoneCallTask();
phonecall.PhoneNumber = textBox1.Text; // set phone number
phonecall.Show();
}

// sending an sms (opening the sms application with presets)
private void button3_Click(object sender, RoutedEventArgs e)
{
Microsoft.Phone.Tasks.SmsComposeTask sms = new Microsoft.Phone.Tasks.SmsComposeTask();
sms.To = textBox1.Text; // set phone number
sms.Body = textBox2.Text; // set body
sms.Show();
}
}
}