Tải bản đầy đủ (.pdf) (40 trang)

Session 02 kho tài liệu bách khoa

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.97 MB, 40 trang )

Advanced Windows Store Apps Development – II


 Enumerate Devices
 Describe the Plug and Play (PnP) devices
 Troubleshoot Asynchronous errors
 Describe Sensors
 Explain Location Services

© Aptech Ltd.

Device Capabilities/Session 2

2


Microsoft insists their manufacturers and operators to create devices with better connectivity
so that the device can be tracked anywhere. The manufacturers also implement and follow
these guidelines in case of internal as well as external devices they create. Operators will
provide the Geopositioning service to locate the device.

Whenever a user develops a store apps, they can also integrate it as a device app. With
device metadata, apps can automatically set up the device connected for the first time and
provide more features than normal apps. Apps can make use of features and utilize the
variety of functionalities provided by devices.

© Aptech Ltd.

Device Capabilities/Session 2

3




Windows.Devices.Enumeration and Windows.Devices.Enumeration.Pnp are
the two namespaces to enumerate the devices connected or disconnected. This
Windows.Devices.Enumeration class provides two methods, namely FindAllAsync
and CreateWatcher to enumerate the devices.

FindAllASync

CreateWatcher

• This will search the
device only once as the
device is connected.
• This will not update
whenever the user adds
or delete the devices.

• This will always
enumerate devices by
raising notification as
the user adds or deletes
the devices.

Note -The default class GUID for
Printer: {0ECEF634-6EF0-472A-8085-5AD023ECBCCD}
Webcam: {E5323777-F976-4F5B-9B55-B94699C46E44}
Portable Devices: {6AC27878-A6FA-4155-BA85-F98F491D4F33}
© Aptech Ltd.


Device Capabilities/Session 2

4


Step 1:
To get the GUID class of a device, the user has to insert any portable device such as pen drive,
printer, or webcam. Right-click the device and go to properties as shown in the following figure.

© Aptech Ltd.

Device Capabilities/Session 2

5


Step 2:
Go to Hardware tab, select the device, and go to Properties as shown in the following
figure.

© Aptech Ltd.

Device Capabilities/Session 2

6


Step 3:
In the properties window, select events, and select the device to view the Class GUID as
shown in the following figure.


© Aptech Ltd.

Device Capabilities/Session 2

7


The DeviceWatcher class is in charge for device enumeration; floating exact events each
time the devices are added, removed, or altered after the initial enumeration is finished.
The events of DeviceWatcher class are as follows:

Added– Occurs when new device is found and added by the DeviceWatcher class
EnumerationCompleted – Occurs when the device enumeration is
completed
Removed – Occurs when a device is removed

Stopped – Occurs when enumeration process is stopped

Updated – Occurs when enumeration process is modified

© Aptech Ltd.

Device Capabilities/Session 2

8


Plug and Play (PnP) is a mixture of hardware and software that enables a system to identify, setup,
and get used to hardware structure modifications when any new device is connected by the user. A

user can connect or disconnect any number of devices to/from the computer.
These devices can be made available in the app by enabling the device in the app manifest file as
shown in the following figure.

© Aptech Ltd.

Device Capabilities/Session 2

9


To deliver a good user experience the app needs access to sensitive devices, it requires to handle
the errors that can occur when trying to access a disabled device.

Reasons for not being able to access a device includes:

The user may not have proper permissions to access the
device.

The user may revoke the permission in the settings.

The device capability is not available on the system.

© Aptech Ltd.

Device Capabilities/Session 2

10



Windows.Devices.Sensors namespace is used to capture motion, device orientation, or light
sensors. Windows.Devices.Geolocation is the namespace for finding the location of the
device through GPS or Location Provider. If the user is trying to access the map for the first time, the
app will automatically request the user for permission as shown in the following figure.

© Aptech Ltd.

Device Capabilities/Session 2

11


Windows 8 supports many sensors. In order to work with these sensors, the user must enable
the capabilities in the app manifest file. If the capabilities are not enabled or properly set in the
manifest file, then the user will receive an exception message stating that there is no sensor
available in the device.

SimpleOrientationSensor
The SimpleOrientationSensor is used to sense the device orientation. Many devices have
recognized the necessity that when the user rotates the device, the display shall also rotate to the
new orientation. The images or screen should be adjusted accordingly. The view of the screen
should remain original even if the device is rotated by the user.

LightSensor

The LightSensor is used to increase or decrease the brightness of the device. This light sensor
will automatically turn Off the display after a certain period of time. It also adjusts the display
brightness or contrast according to the environment.

© Aptech Ltd.


Device Capabilities/Session 2

12


Compass

To detect the magnetic position towards the north to navigate the user using GPS. It
keeps details of directions. Moreover, always points the direction indicator towards the
north direction. This allows the user to know the directions in which he/she is moving.

Inclinometer

This sensor provides the rotation values by showing the pitch, roll, and yaw values. It
monitors the surface movements and deformations if any.

© Aptech Ltd.

Device Capabilities/Session 2

13


To make use of the device orientation sensor, the user has to write the code to make the screen
rotate whenever the user rotates the device. To make use of this orientation the user has to select
the supported rotations from the application tab in the app manifest file as shown in the following
figure.

© Aptech Ltd.


Device Capabilities/Session 2

14


To check and determine the current position of the device, the user has to check the device by
writing the code shown in the following Code Snippet.
Code Snippet:
switch (SimpleOrientation)
{
case SimpleOrientation.NotRotated:
AlertBox.Text = “Not Rotated”;
break;
case SimpleOrientation.Rotated90DegreesCounterclockwise:
AlertBox.Text = “90 Degrees Rotated”;
break;
case SimpleOrientation.Rotated180DegreesCounterclockwise:
AlertBox.Text = “180 Degrees Rotated”;
break;
case SimpleOrientation.Rotated270DegreesCounterclockwise:
AlertBox.Text = “270 Degrees Rotated”;
break;

© Aptech Ltd.

Device Capabilities/Session 2

15



Code Snippet (Cont.):
case SimpleOrientation.Facedown:
AlertBox.Text = “Face Down”;
break;
case SimpleOrientation.Faceup:
AlertBox.Text = “Face Up”;
break;
default:
AlertBox.Text = “Unknown”;
break;
}

The code given in Code Snippet, checks for various orientation positions of the current device using
the switch case block and accordingly takes the action.
This code will only work on the device that has the orientation sensor. If there is no device
orientation sensor present on the device, then this orientation detection code will not work. The app
will ignore the rotate event and will not show any effect on the screen.

© Aptech Ltd.

Device Capabilities/Session 2

16


Location providers can control the location of a device through:
 Wi-Fi
 Tracking IP Address
 Tracking mobile Network

 Global Position System (GPS)
The user has to use the Windows Location Provider for developing the apps. The
Location Provider calculates the latitude and longitude; based on the calculated
location, the device finds the accuracy of the position in which the device is
located. The Location Provider retrieves latitude, longitude, and device
information based on accuracy. The namespace Windows.Devices.Geolocation
will provide the necessary classes and providers needed to access the location
information.

© Aptech Ltd.

Device Capabilities/Session 2

17


To access the location of the device the user has to write the code as given in Code Snippet.

Code Snippet:

g = new Geolocator();
Geoposition p = await g.GetGeopositionAsync();
txtLatitude.Text = “Latitude: “ + p.Coordinate.Point.Position.Latitude.
ToString();
txtLongitude.Text = “Longitude: “ + p.Coordinate.Point.Position.Longitude.
ToString();
txtAccuracy.Text = “Accuracy: “ + p.Coordinate.Accuracy.ToString();

© Aptech Ltd.


Device Capabilities/Session 2

18


The code given in Code Snippet is used to track the Geoposition of a device. This
GeoPosition will show the latitude and longitude point, which shows the exact place
where the device is located. When the code is executed, the output will request user to access
the device location as shown in the following figure:

© Aptech Ltd.

Device Capabilities/Session 2

19


As the user allows to access the device location permission, the app will display the device
latitude, longitude, and the accuracy as shown in the following figure.

© Aptech Ltd.

Device Capabilities/Session 2

20


 Geofencing provides an API to track the device and generate a trigger when the
user enters or leaves a particular location or area.
 Store apps support hundreds of Geofencing options. If more than one

Geofencing is triggered then the app will automatically notify the location that is
active.
 The GeofenceStateChanged event is called, as one or more objects in the
collection are changed. As soon as the Geofencing is triggered, the
ReadReports() method is called to return the Geofencing list, which is
modified.
 The user has to check the entry and exit state when the device is identified in the
defined location by showing a notification by setting the ID in
CreateGeofence() method.
 GeofenceMonitor will save the Geofencing list automatically and it will be
active until the app is executed. The user has to use the Remove() method to clear
the list.

© Aptech Ltd.

Device Capabilities/Session 2

21


Whenever users creates any app, they have to handle the errors if it occurs at runtime. If an
app is developed without handling errors, then the error will be reflected in the app. This will
also close the app abruptly or abnormally. This will lead to bugs in the app.
The code shows how the try catch block is used to capture or handle certain error that has
occurred in the app. Method fnErrorHandle() will handle an exception or error thrown by
the app.
Code Snippet:
async void fnErrorHandle()
{
try {

Int i; int j, int k;
i=5;
j=”Test”;
k=i+j; }
catch (Exception ex)
{
new MessageDialog(ex.Message).ShowAsync();
}}

© Aptech Ltd.

Device Capabilities/Session 2

22


Step 1: Create a blank new application from File → New Project → Blank App
Step 2: Write the code given in following Code Snippet to create the design view of the page
Following Code Snippet shows how to create a layout with components:
Code Snippet:
xmlns=” />xmlns:x=” />xmlns:local=”using:DeviceTesting”
xmlns:d=” />xmlns:mc=” />mc:Ignorable=”d”>
<Grid x:Name=”ContentRoot” Background=”Black” Margin=”100,20,100,20”>
<Grid.RowDefinitions>
<RowDefinition Height=”Auto”/>
<RowDefinition Height=”*”/>

© Aptech Ltd.


Device Capabilities/Session 2

23


Code Snippet (Cont.):
<RowDefinition Height=”Auto”/>
</Grid.RowDefinitions>
<StackPanel x:Name=”Header” Grid.Row=”0”>
<StackPanel Orientation=”Horizontal”/>
</StackPanel>
HorizontalScrollBarVisibility=”Auto” Grid.Row=”1”>
<StackPanel>
<StackPanel Orientation=”Horizontal” HorizontalAlignment=”Left”>
<StackPanel/>
<StackPanel Margin=”25” >
<StackPanel x:Name=”Description” MaxWidth=”500” Height=”250”>
<StackPanel>
<StackPanel Orientation=”Horizontal” Margin=”0,10,0,0”/>
Enumerate” Margin=”10,10,0,0” Width=”888” />
<ListBox Name=”lstDevices” Width=”400” HorizontalAlignment=”Left”>
<ListBoxItem Name=”Webcam”>Webcams</ListBoxItem>

© Aptech Ltd.

Device Capabilities/Session 2

24



Code Snippet (Cont.):
<ListBoxItem Name=”PortDevices”>Portable Devices</ListBoxItem>
<ListBoxItem Name=”Printer”>Printers</ListBoxItem>
</ListBox>
<StackPanel Orientation=”Horizontal” Margin=”0,10,0,0”>
<TextBlock Text=”Class GUID:” HorizontalAlignment=”Left” VerticalAlignment=”Center” />
<TextBox Name=”txtClassGuid” Margin=”10,0,0,0” MinWidth=”350” />
</StackPanel>
<Button Name=”btnEnumerate” Content=”Enumerate” Margin=”0,10,10,0” Click=”btnEnumerate_click” />
</StackPanel> </StackPanel>
</StackPanel> </StackPanel>
<TextBlock Text=”Output” FontSize=”15” Margin=”0,25,0,20” />
<StackPanel HorizontalAlignment=”Left”>
<TextBlock Name=”txtResult” />
<ListBox Name=”lstResult” IsEnabled=”False” BorderThickness=”0” />
</StackPanel> </StackPanel>
</ScrollViewer>
</Grid>
</Page>

© Aptech Ltd.

Device Capabilities/Session 2

25



×