Partilhar via


Communications for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

This topic introduces the ways your Windows Phone app can communicate with other apps and with remote data stores. Learn about sockets, Bluetooth, the Proximity API for Near Field Communications (NFC), voice over IP (VoIP), the Open Data Protocol (OData) client, and web services. This topic also introduces the network information and Data Sense APIs.

This topic contains the following sections.

Sockets

For bidirectional communication across the web, such as with a chat app, Windows Phone OS 7.1 supports sockets-based apps. With sockets, the client or the server can initiate communication and either endpoint can send messages to the other independently. Sockets apps use the System.Net.Sockets API. For more info, see Sockets for Windows Phone 8.

Bluetooth

Bluetooth is a wireless communication technology through which devices within a 10-meter proximity can communicate with each other. Using this technology, devices can communicate without a physical connection. Wireless headsets, remote control toys, and multiplayer games are examples of devices and apps that use Bluetooth technology. By using APIs introduced in Windows Phone 8, your app can communicate with another app or device over Bluetooth. For more info, see Bluetooth for Windows Phone 8.

Proximity

Windows Phone 8 supports Proximity communication using Near Field Communication (NFC). Proximity refers to a set of classes in the Windows Runtime that support connections between devices that are within close range of each other. By using this API, your app can establish a connection through a tap, or by browsing for other devices that are running your app—peer apps—within wireless range. For example, one of these apps might be a multiplayer game in which two users tap their phones together to establish a shared game session. To learn more about the Proximity API, see Proximity for Windows Phone 8.

VoIP

Starting in Windows Phone 8, you can develop a voice over IP (VoIP) app for Windows Phone. Using a subset of the Windows Audio Session API (WASAPI) your app can capture and render audio streams. Windows Phone VoIP apps can also stream video-based VoIP calls. For more info, see VoIP apps for Windows Phone 8.

Data Sense

Through the Data Sense feature in Windows Phone 8, a user can specify the limits of their data plans. Data Sense monitors data usage in relation to the user-specified limits. With this information, your app can help users save money by reducing data usage when the user is close to their data limit, or by discontinuing data usage when the user is over their limit. For more info, see How to adjust data usage using the Data Sense API for Windows Phone 8.

Network information

The user experience of any web-based app is highly dependent on the quality and availability of the device’s network connection. The Microsoft.Phone.Net.NetworkInformation namespace provides several classes through which your app can learn more about the network status of the device it is running on. For example, your app can check whether a cellular data or Wi-Fi connection is enabled. You can also use the API to set cellular or non-cellular network preferences. For more information, see Network and network interface information for Windows Phone 8.

Web and data services

Web services enable programmatic access to a wide variety of data over the Internet. A data service is an HTTP-based Web service that implements the Open Data Protocol (OData) to expose data as resources that are defined by a data model and addressable by Uniform Resource Identifiers (URIs).

Web and data services each use an open XML-based language to describe their web-based API. The Web Service Description Language (WSDL) is used to describe the services that a web service offers. The Conceptual Schema Definition Language (CSDL) describes the data model that a data service offers. For more information, see Web Services Description Language (WSDL) and Conceptual Schema Definition File Format.

Web services

Because the vast majority of web services published on the Internet are based on HTTP, you can use the HttpWebRequest and WebClient classes to access web services from Windows Phone apps. To help ease the task of generating the additional code that web services often require, you can use the Service Model Proxy Generation Tool (SLsvcUtil.exe) or the Visual Studio Add Service Reference feature to generate a proxy class. For an example of how to use the WebClient class to access an RSS feed, see How to create a basic RSS reader for Windows Phone 8.

A web service proxy class implements the serialization, request, and response code for a web service, based on the web service WSDL file. You can use the generated proxy class in your Windows Phone app for communicating with the corresponding web service.

Data services (OData)

A data service is an HTTP-based web service that implements the Open Data Protocol (OData) to expose data as resources that are defined by a data model and addressable by URIs. This enables you to access and change data using the semantics of representational state transfer (REST), specifically the standard HTTP verbs of GET, PUT, POST, and DELETE.

Because data services are based on HTTP, you can use the HttpWebRequest and WebClient classes to access data services from Windows Phone apps. To help ease the task of generating the additional code that a data service requires, you can use the WCF Data Service Client Utility, DataSvcUtil.exe, or the Visual Studio Add Service Reference feature to generate a proxy class based on the data service CSDL file. You can use the generated proxy class in your Windows Phone app for communicating with the corresponding data service.

Note

WCF Data Services enables the creation and consumption of Open Data Protocol (OData) services from the web in .NET Framework apps. The OData client for Windows Phone is not part of the Windows Phone SDK and must be downloaded separately. For more information about OData and WCF Data Services, see WCF Data Services Tools for Visual Studio.

Classes and utilities

The following list contains the classes that you can use directly to make web requests, as well as the utilities available to you to generate other classes optimized to make particular kinds of web requests from your Windows Phone apps:

  • WebClient Class: Provides common methods for sending data to and receiving data from a URI-based resource.

  • HttpWebRequest Class: Provides an HTTP-specific implementation of the abstract WebRequest class.

  • Silverlight Service Model Proxy Generation Tool (SLsvcUtil.exe): Generates proxy classes based on a web service WSDL file.

  • Visual Studio Add Service Reference Feature: Generates proxy classes based on either a web service WSDL file or a data service CSDL file.

  • WCF Data Service Client Utility (DataSvcUtil.exe): Generates proxy classes based on a data service CSDL file.

The following table shows which classes can be used for the various types of HTTP-based programming:

Class

General HTTP programming

HTTP-based web service

Data service

WebClient class

yes

yes

yes

HttpWebRequest class

yes

yes

yes

SLsvcUtil.exe generated classes

no

yes

no

Add Service Reference feature generated classes

no

yes

yes

DataSvcUtil.exe generated classes

no

no

yes

The WebClient and HttpWebRequest classes can be used for a wide range of HTTP-based programming, from general HTTP requests to programming web and data services. Depending on how your app uses a web or data service, using the WebClient or HttpWebRequest classes exclusively may require you to write a significant amount of code.

When developing a web or data services client app, an alternative to programming at the HTTP-level is to use a proxy class. A proxy class is a class that represents the web or data service that is based on the corresponding WSDL or CSDL file, respectively. See the following sections of this topic for more information.

Security considerations

When connecting to a web service that requires an app key, don’t store the app key with an app that will run on a device. Instead, you can create a proxy web service to authenticate a user, and then call an external cloud service using the app key. For more information about security recommendations, see Web service security for Windows Phone 8.

Web service limitations

Each Windows Phone app is limited to a maximum of 6 simultaneous outgoing connections.

When porting web service client code for use in a Windows Phone app, check the .NET APIs to ensure that methods used in the code are supported. For more information about supported APIs for Windows Phone, see .NET API for Windows Phone.

See Also

Other Resources

Open Data Protocol Homepage

Open Data Protocol – Client Libraries

WCF Data Service Client Utility (DataSvcUtil.exe)

WCF Data Services Client Library Documentation

Windows Azure Toolkit for Windows Phone

Windows Phone 8: Networking, Bluetooth, and NFC Proximity for Developers (Build 2012)