Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi、蓝牙、临近感应等多种方式。数据交互一般通过套接字来完成,我们将在本文详细的分析。
快速导航:
一、WP8套接字
二、蓝牙
三、NFC
四、获取网络状态
五、访问web的几种方式
一、WP8套接字
1)创建套接字客户端
Windows phone 8中的套接字并不支持发布服务端,我们只能利用它在手机上创建套接字客户端,我们在此例中要用套接字去访问web服务器。首先,我们定义一个SocketClient来表示套接字客户端。
[C#]
public
class
SocketClient
{
//缓存套接字对象以便在整个生命周期重用
Socket _socket =
null
;
// 信号通知对象,用于异步操作完成通知
static
ManualResetEvent _clientDone =
new
ManualResetEvent(
false
);
/// <summary>
/// 为每个异步调用设置超时时间
/// </summary>
const
int
TIMEOUT_MILLISECONDS = 30000;
/// <summary>
/// 数据缓冲区大小
/// </summary>
const
int
MAX_BUFFER_SIZE = 2048 * 100;
/// <summary>
/// 关闭套接字连接和释放所有相关的资源
/// </summary>
public
void
Close()
{
if
(_socket !=
null
)
{
_socket.Close();
}
}
}
这个客户端包含连接服务器、发送信息和接受信息三个方法。我们将如下代码加入SocketClient类:
连接:
[C#]
/// <summary>
/// 尝试用TCP套接字连接到指定主机端口
/// </summary>
/// <param name="hostName">主机名</param>
/// <param name="portNumber">端口号</param>
/// <returns>描述连接结果</returns>
public
string
Connect(
string
hostName,
int
portNumber)
{
string
result =
string
.Empty;
//创建一个终结点,主机名、端口号
DnsEndPoint hostEntry =
new
DnsEndPoint(hostName, portNumber);
//创建一个基于流,TCP套接字。
_socket =
new
Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//套接字上下文
SocketAsyncEventArgs socketEventArg =
new
SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = hostEntry;
socketEventArg.Completed +=
new
EventHandler<SocketAsyncEventArgs>(
delegate
(
object
s, SocketAsyncEventArgs e)
{
//执行的状态信息
result = e.SocketError.ToString();
//将当前线程设置为收到信息状态,这样被阻止的线程可以继续执行
_clientDone.Set();
});
//标识当前进程为未收到信息状态
_clientDone.Reset();
// 发送一个异步连接请求
_socket.ConnectAsync(socketEventArg);
//将当前线程阻止,直到当前线程收到信息或者超时时间已到
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
return
result;
}
发送消息:
[C#]
/// <summary>
/// 向连接的服务器发送信息
/// </summary>
/// <param name="data">数据正文</param>
/// <returns>反馈</returns>
public
string
Send(
string
data)
{
string
response =
"操作超时"
;
//套接字是否准备好
if
(_socket !=
null
)
{
//套接字上下文
SocketAsyncEventArgs socketEventArg =
new
SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
socketEventArg.UserToken =
null
;
socketEventArg.Completed +=
new
EventHandler<SocketAsyncEventArgs>(
delegate
(
object
s, SocketAsyncEventArgs e)
{
response = e.SocketError.ToString();
_clientDone.Set();
});
byte
[] payload = Encoding.UTF8.GetBytes(data);
socketEventArg.SetBuffer(payload, 0, payload.Length);
_clientDone.Reset();
_socket.SendAsync(socketEventArg);
_clientDone.WaitOne(TIMEOUT_MILLISECONDS);
}
else
{
response =
"套接字没有准备好"
;
}
return
response;
}
接受消息:
[C#]
/// <summary>
/// 从连接服务器接收数据
/// </summary>
/// <returns>The data received from the server</returns>
public
string
Receive()
{
string
response =
"操作超时"
;
if
(_socket !=
null
)
{
SocketAsyncEventArgs socketEventArg =
new
SocketAsyncEventArgs();
socketEventArg.RemoteEndPoint = _socket.RemoteEndPoint;
//设置接收数据的缓冲区
socketEventArg.SetBuffer(
new
Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);
socketEventArg.Completed +=
new
EventHandler<SocketAsyncEventArgs>(
delegate
(
object
s, SocketAsyncEventArgs e)
{
if
(e.SocketError == SocketError.Success)
{
// Retrieve the data from the buffer
response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
response = response.Trim(
'