博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows phone 8 学习笔记(3) 通信
阅读量:6159 次
发布时间:2019-06-21

本文共 23909 字,大约阅读时间需要 79 分钟。

原文:

Windows phone 8 可利用的数据通信方式比较广泛,在硬件支持的前提下,我们可以利用WiFi、蓝牙、临近感应等多种方式。数据交互一般通过套接字来完成,我们将在本文详细的分析。

快速导航:

   
   
   
   
   

一、WP8套接字

1)创建套接字客户端

Windows phone 8中的套接字并不支持发布服务端,我们只能利用它在手机上创建套接字客户端,我们在此例中要用套接字去访问web服务器。首先,我们定义一个SocketClient来表示套接字客户端。

[C#]
public class SocketClient    {        //缓存套接字对象以便在整个生命周期重用        Socket _socket = null;        // 信号通知对象,用于异步操作完成通知        static ManualResetEvent _clientDone = new ManualResetEvent(false);        ///         /// 为每个异步调用设置超时时间        ///         const int TIMEOUT_MILLISECONDS = 30000;        ///         /// 数据缓冲区大小        ///         const int MAX_BUFFER_SIZE = 2048 * 100;        ///         /// 关闭套接字连接和释放所有相关的资源        ///         public void Close()        {            if (_socket != null)            {                _socket.Close();            }        }    }
    这个客户端包含连接服务器、发送信息和接受信息三个方法。我们将如下代码加入SocketClient类:
   
连接:
[C#]
///         /// 尝试用TCP套接字连接到指定主机端口        ///         /// 主机名        /// 端口号        /// 
描述连接结果
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
(delegate(object s, SocketAsyncEventArgs e) { //执行的状态信息 result = e.SocketError.ToString(); //将当前线程设置为收到信息状态,这样被阻止的线程可以继续执行 _clientDone.Set(); }); //标识当前进程为未收到信息状态 _clientDone.Reset(); // 发送一个异步连接请求 _socket.ConnectAsync(socketEventArg); //将当前线程阻止,直到当前线程收到信息或者超时时间已到 _clientDone.WaitOne(TIMEOUT_MILLISECONDS); return result; }
   
发送消息:
[C#]
///         /// 向连接的服务器发送信息        ///         /// 数据正文        /// 
反馈
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
(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#]
///         /// 从连接服务器接收数据        ///         /// 
The data received from the server
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
(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('\0'); } else { response = "错误:" + e.SocketError.ToString(); } _clientDone.Set(); }); _clientDone.Reset(); _socket.ReceiveAsync(socketEventArg); _clientDone.WaitOne(TIMEOUT_MILLISECONDS); } else { response = "套接字没有准备好"; } return response; }

2)通过套接字访问HTTP网站

现在我们有了套接字客户端,这个时候我们来做一个类似于浏览器的功能,我们模拟HTTP请求发送到web服务器,代码如下:

[XAML]
[C#]
private void Button_Click_1(object sender, RoutedEventArgs e)        {            string hostName = textbox1.Text.Replace("http://", string.Empty);            SocketClient client = new SocketClient();            //主机,端口号            textblok1.Text = client.Connect(hostName, 80);            //模拟一个HTTP Get请求            textblok1.Text = client.Send("GET http://" + hostName + " HTTP/1.0\r\nHost: " + hostName + "\r\n\r\n\r\n");            //等待相应            textblok1.Text = client.Receive();        }

 

二、蓝牙

WP8蓝牙支持对等方应用连接,也支持其他蓝牙设备,下面我们看看如何连接到对等方应用和设备。

1)连接到对等方

[C#]
//已经搜索到的对等方列表        IReadOnlyList
peers; // 开始连接到对等应用 async void AppToApp() { // 开始查找对等项,如果有这句话,即可使自己能够被其他蓝牙设备搜索到 PeerFinder.Start(); peers = await PeerFinder.FindAllPeersAsync(); if (peers.Count == 0) { // 没有发现 } else { // 选择第一个对等应用 PeerInformation selectedPeer = peers[0]; // 连接到第一个对等方应用 var streamSocket = await PeerFinder.ConnectAsync(selectedPeer); } }

2)连接到设备

PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; 查找所有已配对的设备。这样连接找到的设备对应的PeerInformation.ServiceName将为空,所以我们不能通过PeerFinder.ConnectAsync(selectedPeer);的方式去连接,具体示例如下:

[C#]
//搜寻全部蓝牙设备并连接第一个        private async void AppToDevice()        {            // 搜索所有配对的设备            PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";            var pairedDevices = await PeerFinder.FindAllPeersAsync();            if (pairedDevices.Count == 0)            {                //没有发现设备            }            else            {                // 选择第一个连接的设备,此时selectedDevice.ServiceName为空                PeerInformation selectedDevice = pairedDevices[0];                // 主动创建一个StreamSocket                StreamSocket socket = new StreamSocket();                // 第二个参数是一个RFCOMM端口号,范围是1-30                await socket.ConnectAsync(selectedDevice.HostName, "1");            }        }

PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";查找使用服务发现协议 (SDP) 并通过既定 GUID 播发服务的设备

[C#]
//搜寻特定GUID的设备        private async void AppToDevice2()        {            PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";            var pairedDevices = await PeerFinder.FindAllPeersAsync();            if (pairedDevices.Count == 0)            {                //没有发现设备            }            else            {                // 选择第一个连接的设备                PeerInformation selectedDevice = pairedDevices[0];                // 主动创建一个StreamSocket                StreamSocket socket = new StreamSocket();                // 这种情况下selectedDevice.ServiceName等于您指定的GUID                await socket.ConnectAsync(selectedDevice.HostName, selectedDevice.ServiceName);            }        }

3)侦听连接请求

[C#]
public Page1()        {            InitializeComponent();            //侦听连接请求需要先添加事件            Loaded += MainPage_Loaded;        }        void MainPage_Loaded(object sender, RoutedEventArgs e)        {            //远程对等类异步连接时触发            PeerFinder.ConnectionRequested += PeerFinder_ConnectionRequested;        }        async void PeerFinder_ConnectionRequested(object sender, ConnectionRequestedEventArgs args)        {            // 获取到请求连接的对等方            var peer = args.PeerInformation;            // 回应连接            var streamSocket = await PeerFinder.ConnectAsync(peer);        }

4)发送消息

[C#]
///         /// 在已连接的情况下发送消息        ///         ///         public async void Send(StreamSocket socket, string msg)        {            var _dataWriter = new DataWriter(socket.OutputStream);            //写入消息的长度            uint strLength = _dataWriter.MeasureString(msg);            _dataWriter.WriteUInt32(strLength);            //写入消息的内容            _dataWriter.WriteString(msg);            uint numBytesWritten = await _dataWriter.StoreAsync();        }

5)接收消息

[C#]
///         /// 在已连接的情况下读取消息        ///         ///         public async Task
Read(StreamSocket socket) { var _dataReader = new DataReader(socket.InputStream); // 读取消息长度 await _dataReader.LoadAsync(sizeof(uint)); uint msgLength = (uint)_dataReader.ReadUInt32(); // 读取消息的内容 await _dataReader.LoadAsync(msgLength); return _dataReader.ReadString(msgLength); }

 

三、NFC

当两个设备距离较近时,可以使用近场通信技术,本文演示一个使用近场通信制作聊天软件的例子。

[XAML]
[C#]
public partial class MainPage : PhoneApplicationPage    {        ProximityDevice device;        long typeId = -1;        long msgId = -1;        public MainPage()        {            InitializeComponent();            device = ProximityDevice.GetDefault();            if (device == null)            {                IsEnabled = false;                textblock1.Text = "您的手机不支持NFC功能";            }            else            {                //设备进入NFC识别范围时触发                device.DeviceArrived += device_DeviceArrived;                //设备离开NFC识别范围时触发                device.DeviceDeparted += device_DeviceDeparted;            }        }        //设备进入NFC识别范围时触发        void device_DeviceArrived(ProximityDevice sender)        {            //创建消息订阅,创建好后就可以直接发消息了            typeId = sender.SubscribeForMessage("Windows.MyMsgType", MsgReceivedHandler);            Dispatcher.BeginInvoke(() => textblock1.Text = "连接状态:已连接");        }        //设备离开NFC识别范围时触发        void device_DeviceDeparted(ProximityDevice sender)        {            if (typeId != -1)            {                //取消消息订阅                sender.StopSubscribingForMessage(typeId);                typeId = -1;            }            Dispatcher.BeginInvoke(() => textblock1.Text = "连接状态:连接中断");        }        ///         /// 接收到消息后的处理逻辑        ///         void MsgReceivedHandler(ProximityDevice sender, ProximityMessage message)        {            Dispatcher.BeginInvoke(() =>            {                listbox1.Items.Add(("对方:" + message.DataAsString));            });         }        ///         /// 发送消息按钮按下时        ///         private void Button_Click_1(object sender, RoutedEventArgs e)        {            if (textbox1.Text != string.Empty)            {                listbox1.Items.Add("我:" + textbox1.Text);                if (msgId != -1)                    device.StopPublishingMessage(msgId);                msgId = device.PublishMessage("Windows.MyMsgType", textbox1.Text);            }        }    }

现在我们看看在模拟器中的效果,如下图。模拟器中实现NFC的模拟效果请下载:

四、获取网络状态

很多时候,应用可能需要利用到网络,这个时候我们需要获取一些信息,比如网络是否连接,当前是连接到WiFi还是运营商蜂窝网络。这些信息关系到您的应用是否能正常工作,或者是否处于免费网络下可执行高流量下载等操作。

1. 数据感知

数据感知功能可以用来获取网络成本信息以及流量计划,帮助用户合理使用手机流量,我们看看使用方法。

[C#]
var connectionProfile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();//网络接口类型var ianaInterfaceType = connectionProfile.NetworkAdapter.IanaInterfaceType;//连接成本信息var connectionCost = connectionProfile.GetConnectionCost();//当前的网络使用成本var networkCostType = connectionCost.NetworkCostType;//计划流量是否快用完var approachingDataLimit = connectionCost.ApproachingDataLimit;//是否超过计划流量var overDataLimit = connectionCost.OverDataLimit;//是否漫游var roaming = connectionCost.Roaming;

2. 网络信息获取

我们可以通过API获取网络接口状态,检查手机数据网络以及WiFi连接情况等。

[XAML]
[C#]
Socket socket;        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)        {            //移动运营商            textblock1.Text = DeviceNetworkInformation.CellularMobileOperator;            //网络信息            System.Text.StringBuilder sb = new System.Text.StringBuilder();            sb.Append("网络是否可用:  ");            sb.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString());            sb.Append("是否启用蜂窝数据:  ");            sb.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString());            sb.Append("是否允许漫游:  ");            sb.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString());            sb.Append("是否启用Wi-Fi:  ");            sb.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString());            //网络接口信息            sb.Append("网络接口类型:  ");            var networkInterfaceType = NetworkInterface.NetworkInterfaceType;            var networkInterfaceTypeString = networkInterfaceType.ToString() + "(" + InterfaceTypeConvert.Convert(networkInterfaceType) + ")";            sb.AppendLine(networkInterfaceTypeString);            sb.Append("网络接口子类型:  ");            var networkSubInterfaceTypeString = string.Empty;            DeviceNetworkInformation.ResolveHostNameAsync(new DnsEndPoint("www.baidu.com", 80),                new NameResolutionCallback(x =>                    {                        networkSubInterfaceTypeString = x.NetworkInterface.InterfaceSubtype.ToString() + "[" + InterfaceTypeConvert.ConvertSub(x.NetworkInterface.InterfaceSubtype) + "]";                        sb.AppendLine(networkSubInterfaceTypeString);                        Dispatcher.BeginInvoke(() => { textblock2.Text = sb.ToString(); });                    }), null);            //套接字连接信息            DnsEndPoint hostEntry = new DnsEndPoint("www.baidu.com", 80);            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            //设置连接要求,必须为使用蜂窝技术网络            socket.SetNetworkRequirement(NetworkSelectionCharacteristics.Cellular);            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();            socketEventArg.RemoteEndPoint = hostEntry;            socketEventArg.Completed += new EventHandler
(delegate(object s, SocketAsyncEventArgs e1) { NetworkInterfaceInfo netInterfaceInfo = socket.GetCurrentNetworkInterface(); StringBuilder sb2 = new StringBuilder(); sb2.AppendLine("最后更新: " + DateTime.Now.ToString()); sb2.Append("网络接口名: "); sb2.AppendLine(netInterfaceInfo.InterfaceName); sb2.Append("网络接口状态: "); sb2.AppendLine(netInterfaceInfo.InterfaceState.ToString()); sb2.Append("网络接口类型: "); sb2.AppendLine(netInterfaceInfo.InterfaceType.ToString()); sb2.Append("网络接口子类型: "); sb2.AppendLine(netInterfaceInfo.InterfaceSubtype.ToString()); Dispatcher.BeginInvoke(() => textblock3.Text = sb2.ToString()); }); socket.ConnectAsync(socketEventArg); base.OnNavigatedTo(e); } private void Button_Click_1(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative)); }

3. 检测网络状态改变

手机的网络连接并不如电脑稳定,当你到信号不好的地方,或者自动连接上WiFi等情况,怎么通过代码自动监测呢,下面演示了当手机网络改变时,列举出日志清单,并实时刷新网络信息。

[XAML]
[C#]
public partial class Page1 : PhoneApplicationPage    {        public ObservableCollection
Changes { get; set; } public ObservableCollection
NetworkInterfaces { get; set; } public Page1() { InitializeComponent(); // 网络可用性改变事件日志 Changes = new ObservableCollection
(); lbNetworkChanges.DataContext = Changes; //当前可用的网络接口 NetworkInterfaces = new ObservableCollection
(); lbNetworkInterfaces.DataContext = NetworkInterfaces; //网络可用性改变时 DeviceNetworkInformation.NetworkAvailabilityChanged += new EventHandler
(ChangeDetected); UpdateNetworkInterfaces(); UpdateNetworkStatus(); } void ChangeDetected(object sender, NetworkNotificationEventArgs e) { string change = string.Empty; switch (e.NotificationType) { case NetworkNotificationType.InterfaceConnected: change = "连接: "; break; case NetworkNotificationType.InterfaceDisconnected: change = "断开: "; break; case NetworkNotificationType.CharacteristicUpdate: change = "变更: "; break; default: change = "未知: "; break; } string changeInformation = String.Format(" {0} {1} {2} ({3})", DateTime.Now.ToShortTimeString(), change, e.NetworkInterface.InterfaceName, InterfaceTypeConvert.Convert(e.NetworkInterface.InterfaceType) + "," + InterfaceTypeConvert.ConvertSub(e.NetworkInterface.InterfaceSubtype)); Dispatcher.BeginInvoke(() => { Changes.Add(changeInformation); UpdateNetworkStatus(); UpdateNetworkInterfaces(); }); } //更新网络接口 private void UpdateNetworkInterfaces() { NetworkInterfaces.Clear(); //获取网络接口列表 NetworkInterfaceList networkInterfaceList = new NetworkInterfaceList(); foreach (NetworkInterfaceInfo networkInterfaceInfo in networkInterfaceList) { NetworkInterfaces.Add(String.Format("{0} ({1},{2})", networkInterfaceInfo.InterfaceName, InterfaceTypeConvert.Convert(networkInterfaceInfo.InterfaceType), InterfaceTypeConvert.ConvertSub(networkInterfaceInfo.InterfaceSubtype))); } } //更新网络状态 private void UpdateNetworkStatus() { tbIsCellularDataEnabled.Text = (DeviceNetworkInformation.IsCellularDataEnabled) ? "是" : "否"; tbIsNetworkAvailable.Text = (DeviceNetworkInformation.IsNetworkAvailable) ? "是" : "否"; tbIsWiFiEnabled.Text = (DeviceNetworkInformation.IsWiFiEnabled) ? "是" : "否"; } //连接设置 private void Button_Click_3(object sender, RoutedEventArgs e) { var settringTypeString = string.Empty; var button = sender as Button; if (button != null) settringTypeString = Convert.ToString(button.Content); var settringType = ConnectionSettingsType.WiFi; switch (settringTypeString) { case "飞行": settringType = ConnectionSettingsType.AirplaneMode; break; case "网络": settringType = ConnectionSettingsType.Cellular; break; case "蓝牙": settringType = ConnectionSettingsType.Bluetooth; break; default: break; } //连接设置选择框 ConnectionSettingsTask connectionSettings = new ConnectionSettingsTask(); // 连接设置的类型 connectionSettings.ConnectionSettingsType = settringType; connectionSettings.Show(); } //刷新 private void Button_Click_2(object sender, RoutedEventArgs e) { UpdateNetworkStatus(); UpdateNetworkInterfaces(); Changes.Clear(); } private void Button_Click_1(object sender, RoutedEventArgs e) { NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); } }

 

五、访问web的几种方式

Windows phone中访问Internet的方式主要有:通过套接字、WebClient 类、HttpWebRequest 类、服务引用等。套接字我们已经讲过。我主要讲解后面三种。

WebClient 类:一般用于通过访问URL获取特定的资源,下面是使用方法:

[C#]
WebClient webClient = new WebClient();webClient.DownloadStringAsync(new Uri("http://www.baidu.com", UriKind.Absolute));webClient.DownloadStringCompleted += (a, b) =>{    if (b.Error == null && !b.Cancelled)    {        Dispatcher.BeginInvoke(() => MessageBox.Show(b.Result));    }};

HttpWebRequest 类:对http做了基本的实现,相当于比直接用socket方便一点。

[C#]
Uri uri = new Uri("http://192.168.100.212"); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri); httpWebRequest.Method = "POST"; //异步获取HTTP请求正文流 httpWebRequest.BeginGetRequestStream(new AsyncCallback(a => {     var httpWebRequest2 = a.AsyncState as HttpWebRequest;     var stream = httpWebRequest2.EndGetRequestStream(a);     var streamWriter = new StreamWriter(stream);     //在HTTP请求正文流中写入要POST的参数     streamWriter.WriteLine("wd=111");     streamWriter.Close();     //开始异步的向远程主机发送HTTP请求     httpWebRequest2.BeginGetResponse(new AsyncCallback(b =>     {         try         {             var httpWebRequest3 = b.AsyncState as HttpWebRequest;             //请求执行完成,得到HTTP相应             WebResponse webResponse = httpWebRequest3.EndGetResponse(b);             var s = webResponse.ContentType;             //获取到HTTP相应正文流             var stream3 = webResponse.GetResponseStream();             //读出来,转换成字符串             var streamReader = new StreamReader(stream3, Encoding.UTF8);             var text = streamReader.ReadToEnd();             Dispatcher.BeginInvoke(() => MessageBox.Show(text));         }         catch (Exception ex)         {             var c = ex.Message;         }     }), httpWebRequest2); }), httpWebRequest);

服务引用:其实于一般的服务引用一样,只不过这里只支持异步实现。

[C#]
//添加服务引用后,下面代码为异步调用web服务ServiceReference1.WebService1SoapClient c = new ServiceReference1.WebService1SoapClient();c.HelloWorldAsync("");c.HelloWorldCompleted += (a, b) =>{    Dispatcher.BeginInvoke(() => MessageBox.Show(b.Result));};

 

作者:[ ]
出处:[ ]
版权声明:本文的版权归作者与博客园共有。转载时须注明原文出处以及作者,并保留原文指向型链接,不得更改原文内容。否则作者将保留追究其法律责任。
你可能感兴趣的文章
每天一个linux命令(19):find 命令概览
查看>>
MySQL kill操作
查看>>
windows下看端口占用
查看>>
Decommissioning a Domain Controller 降域控
查看>>
Character中的奇葩
查看>>
c++书籍推荐
查看>>
轻松监听Azure service health 状态
查看>>
获取SQL SERVER某个数据库中所有存储过程的参数
查看>>
在Linux下编译安装Apache2(2)
查看>>
Method Swizzling 处理一类简单的崩溃
查看>>
AngularJS学习!
查看>>
在Eclipse中搭建Python Django
查看>>
struts国际化
查看>>
Laravel 5.0 - Middleware (中间件)
查看>>
文件特殊权限及facl
查看>>
我的友情链接
查看>>
Android按两次返回键退出应用
查看>>
第一章:认识Redhat Linux
查看>>
文本查看指令
查看>>
我的友情链接
查看>>