23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The ConnectionProfile Object<br />

Of all the information available through the NetworkInformation object, the most important for apps is<br />

found in ConnectionProfile, most frequently that returned by getInternetConnection-Profile<br />

because that’s the one through which an app’s Internet traffic will flow. The profile is what contains all<br />

the information you need to make decisions about how you’re using the network, especially for cost<br />

awareness. It’s also what you’ll typically check when there’s a change in network status. Scenarios 1 and<br />

3 of the Network information sample retrieve and display most of these details.<br />

Each profile has a profileName property (a string), such as “Ethernet” or the SSID of your wireless<br />

access point, plus a getNetworkNames method that returns a vector of friendly names for the endpoint.<br />

The networkAdapter property contains a NetworkAdapter object for low-level details, should you want<br />

them, and the networkSecuritySettings property contains a NetworkSecurity-Settings object<br />

properties describing authentication and encryption types.<br />

More generally interesting is the getNetworkConnectivityLevel, which returns a value from the<br />

NetworkConnectivityLevel enumeration: none (no connectivity), localAccess (the level you hate to<br />

see when you’re trying to get a good connection!), constrainedInternetAccess (captive portal<br />

connectivity, typically requiring further credentials as is often encountered in hotels, airports, etc.), and<br />

internetAccess (the state you’re almost always trying to achieve). The connectivity level is often a factor<br />

in your app logic and something you typically watch with network status changes.<br />

To track the inbound and outbound traffic on a connection, the getLocalUsage method returns a<br />

DataUsage object that contains bytesReceived and bytesSent, either for the lifetime of the connection<br />

or for a specific time period. Similarly, the getConnectionCost and getDataPlanStatus provide the<br />

information an app needs to be aware of how much network traffic is happening and how much it<br />

might cost the user. We’ll come back to this in “Cost Awareness” shortly, including how to see per-app<br />

usage in Task Manager.<br />

Connectivity Events<br />

It is very common for a running app to want to know when connectivity changes. This way it can take<br />

appropriate steps to disable or enable certain functionality, alert the user, synchronize data after being<br />

offline, and so on. For this, apps need only watch the NetworkInformation.onnetworkstatus-changed<br />

event, which is fired whenever there’s a significant change within the hierarchy of objects we’ve just<br />

seen (and be mindful that this event comes from a WinRT object). For example, the event will be fired if<br />

the connectivity level of a profile changes. It will also be fired if the Internet profile itself changes, as<br />

when a device roams between different networks, or when a metered data plan is approaching or has<br />

exceeded its limit, at which point the user will start worrying about every megabyte of traffic. In short,<br />

you’ll generally want to listen for this event to refresh any internal state of your app that’s dependent on<br />

network characteristics and set whatever flags you use to configure the app’s networking behavior. This<br />

is especially important for transitioning between online and offline and between unlimited and metered<br />

networks; Windows, for its part, also watches this event to adjust its own behavior, as with the<br />

Background Transfer APIs.<br />

634

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!