WP7 Tpa 09 Web Location Sensors

Embed Size (px)

Citation preview

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    1/23

    TPA - Windows Phone

    Web requests, Location services

    Phone sensors

    Prepared by: Kamil Kowalski

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    2/23

    Agenda

    Web Requests

    WebClient

    HttpWebRequest

    Consuming Web Service

    Location services

    Accelerometer

    Compass Gyroscope

    Q & A

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    3/23

    Web Requests

    WP7 uses the same Web request API as normal .NET Only the asynchronous request methods available

    System.Net offers the WebClient class for simple access

    For non-trivial data handling the WebClient can cause problems while

    it runs in the UI thread, so HttpWebRequest should be preferred

    WebClient cannot be used when you need to access the basic HTTP

    protocol features

    HttpWebRequest is for HTTP requests

    Use BeginGetResponse() method to initialize a HTTP GET

    The response is available in the Callback method through the

    argument

    Typical approach is to get the response stream, and read the contents

    of the stream

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    4/23

    Web RequestsWebClient

    The WebClient does not support concurrent calls You have to manage to wait for the completion of the previous call before

    starting another one

    The WebClient class provides a number of ways to call an httpresource:

    DownloadStringAsync - provides the simplest way of downloading astring resource from the network, like an HTML page, a RSS or ATOMfeed, a plain XML file and so on

    UploadStringAsync - it can be used to send strings to a server and it canalso specify the method to be used to make the call.

    Probably it is the simplest way of making calls to a REST resource

    OpenWriteAsync & OpenReadAsync - these two method allow to sendand receive binary resources to/from the server.

    The use is slightly more complex that the other methods but once the callcompletes you can access a binary stream you can read or write directly.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    5/23

    Web RequestsWebClient

    How to use it the simplest way:

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    6/23

    Web RequestsWebClient

    How to use it beter way:

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    7/23

    Web RequestsHttpWebRequest

    Basic scenario: Create an instance of the HttpWebRequest

    If you have to post something get the request stream

    Then write posted data to the request stream

    Get the response (that is actually what starts the call)

    Get the response stream

    Read returned data from response stream

    Close all thing

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    8/23

    Web RequestsHttpWebRequest

    How to use it the simplest way:

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    9/23

    Web RequestsHttpWebRequest

    How to use it beter way:

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    10/23

    Web RequestsConsuming Web Service

    The WebClient and HttpWebRequest are both complete andeasy to use, the problem is that their usage is for sure heavy,

    because you need to rely on your own protocol made for

    example of XML files.

    The process of serializing and deserializing can become very

    annoying.

    The real answer to this problem are the Web Services - something

    that is based on a known protocol (built on top of HTTP) and makes

    easy the process of sharing data between client and server, hiding

    transport and serialization problems.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    11/23

    Web RequestsConsuming Web Service

    Working with Web Services you have to be aware of threethings that uniquely define the Web Service you want to

    consume: Address, Binding and Contract.

    Address - says where the service is located, in the form of a Uri

    Binding - describes how the client and server will speak each other.

    Serialization and deserialization matter is part of the binding but also

    the transport: HTTP, HTTPS, and so on.

    Contract - the most important part, that describes what the service

    can do.

    In WCF, a contract is an interface with a bunch of methods - usually called

    Operations - and each one do something with some kind of input and output.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    12/23

    Web RequestsConsuming Web Service

    Once you have the Address of the service you have to consume, the firstthing you have to do is creating a Proxy.

    The proxy is a client side class that implements the Contract of the

    service and hides to the developer the presimageence of a Web Service.

    What it happens is that you call a method on the local class and the proxy take care of

    serialize request, making the network call, reading the response and deserializes it.

    Sample web services:

    GeoIP

    http://www.webservicex.net/geoipservice.asmx?wsdl

    Stock Quote

    http://www.webservicex.net/stockquote.asmx?wsdl

    Demo Weather

    http://www.webservicex.net/globalweather.asmx?WSDL

    http://www.webservicex.net/geoipservice.asmx?wsdlhttp://www.webservicex.net/stockquote.asmx?wsdlhttp://www.webservicex.net/globalweather.asmx?WSDLhttp://www.webservicex.net/globalweather.asmx?WSDLhttp://www.webservicex.net/globalweather.asmx?WSDLhttp://www.webservicex.net/globalweather.asmx?WSDLhttp://www.webservicex.net/stockquote.asmx?wsdlhttp://www.webservicex.net/geoipservice.asmx?wsdlhttp://www.webservicex.net/geoipservice.asmx?wsdlhttp://www.webservicex.net/geoipservice.asmx?wsdl
  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    13/23

    Location services

    Windows Phone uses three technologies for finding yourlocation: GPS, WiFi based location, and Cell Tower based

    location (triangulation).

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    14/23

    Location services

    GPS technology relies on satellites in orbit and the phone'sability to receive signals from at least three of these

    satellites.

    The satellites are transmitting all the time from synchronized clocks.

    But because of the satellites being different distances from the phone

    the time received from the satellites will be slightly different, a user'sposition can be calulated from these time differences.

    Cell Tower based location and WiFi based location are similar

    to GPS in how they work.

    When you use either one of these the phone will try to get theidentifiers for near by WiFi access points and cell towers. The

    identifier is sent to a Microsoft service which keeps records of the

    general area in which those WiFi access points or cell towers have

    been seen and returns that information to the device.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    15/23

    Location services

    Each one of these location technologies differs in theaccuracy of the location that it provides.

    GPS is the most accurate with the margin of error being around 10

    meters in favourable conditions.

    The next most precise location technology is WiFi based location.

    WiFi access points don't transmit nearly as far as cell towers.

    Cell tower based location is the least precise.

    Generally speaking the more precise location technology

    (GPS) also has a more significant impact on battery life.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    16/23

    Location services

    Things to Remember Request the Location Services capability in your

    WMAppManifest.xml

    Ask the user for permission to get their location

    Be prepared for the possibility of location services beingdisabled on the phone

    Don't transmit the user's location to any service without

    their permission

    Only use high accuracy location when needed Turn off location services when it is nolonger needed

    Nokia Places API for Windows Phone

    http://nikovrdoljak.wordpress.com/2012/04/16/nokia-places-api-for-windows-phone/http://nikovrdoljak.wordpress.com/2012/04/16/nokia-places-api-for-windows-phone/
  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    17/23

    Accelerometer

    The accelerometer is a sensorin the Windows Phone device

    that measures the acceleration

    on 3 axes (X, Y, Z), relative to

    freefall.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    18/23

    Accelerometer

    In addition to a timestamp, the values are expressed in G-forces (1G = 9.81 m/s2).

    It this means is that if the phone is lying face-up on a perfectly flat

    surface, the Z axis would read1.0, and the other two axes would

    read 0.

    How to get values from accelerometer basic scenario:

    Instantiate a new Accelerometer object.

    Create a new ReadingChanged() event handler to monitor the

    changes in data.

    Pass the results of that event back to our pages thread (the eventfires on a different thread).

    Use the data values in application.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    19/23

    Accelerometer

    The emulator emulates accelerometer data byAccelerometertab inAdditional tools window.

    Feature introduced in Mango release.

    By default the emulator assumes it is standing in portrait position,

    lined up on a flat surface.

    Other ways to emulate accelerometer data:

    use Reactive Extensions to emulate and filter accelerometer data for

    Windows Phone (check here)

    use accelKit (check here)

    http://msdn.microsoft.com/en-us/library/ff637521(VS.92).aspxhttp://tainicom.net/tools/accelkit.aspxhttp://tainicom.net/tools/accelkit.aspxhttp://msdn.microsoft.com/en-us/library/ff637521(VS.92).aspx
  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    20/23

    Compass

    In traditional terms, a compass is used to determine thedirection of the Earths magnetic north pole.

    Mobile phones are using element called magnetometer.

    It can still determine the direction of magnetic north, but it can alsodetermine rotation of the device relative to magnetic north.

    In addition, it is also capable of detecting magnetic fields around thedevice (which often interfere with those other calculations).

    The MagneticHeading is the heading, in degrees, measured clockwisefrom the Earths magnetic north.

    The TrueHeading is the heading, in degrees, measured clockwise from

    the Earths geographic north. The X, Y, and Z values that you retrieve from API are actually

    measured in microteslas, which are a unit of magnetic field strength.

    NOTICE: The emulator doesnt emulate compas data!

    http://en.wikipedia.org/wiki/Microteslahttp://en.wikipedia.org/wiki/Microtesla
  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    21/23

    Gyroscope

    According to Wikipedia, a gyroscope is a device for measuringor maintaining orientation.

    It has the ability to spin on three axes, the X, Y, and Z axis

    While the Accelerometer measures acceleration, the Gyroscope

    measures rotational velocity.

    The data you receive measures the rotational velocity of the device in

    radians per second.

    This means that you can more accurately and smoothly measure the

    current orientation of the device.

    NOTICE: The emulator doesnt emulategyroscope data!

    http://en.wikipedia.org/wiki/Gyroscopehttp://en.wikipedia.org/wiki/Gyroscope
  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    22/23

    Compas & GyroscopeOptional hardware

    Not all Windows Phones will have a Gyroscope and/or

    Compas. In fact, only phones that come out after the Mango

    release will be capable of having a Gyroscope and/or Compas

    It is still an optional piece of hardware. Microsoft has created a Motion class that combines the data

    from the Accelerometer, the Compass, and the Gyroscope

    into one class that we can use more effectively.

  • 8/2/2019 WP7 Tpa 09 Web Location Sensors

    23/23

    Q & A

    ??