【译】Win8.1 & WP8: 蓝牙Rfcomm应用

Windows 8.1: Play with Bluetooth Rfcomm

原文链接

浏览新增加到 Windows 8.1 的命名空间,你会发现一个有趣、令人惊叹的对蓝牙的领域的支持。新的操作系统在“Windows.Devices.Bluetooth.Rfcomm”命名空间完整的支持了蓝牙Rfcomm。“无线频率通信”协议是一套简单的传输协议,它允许两个设备使用可能的数据流,就像在网络中使用TCP协议一样。

这意味着Rfcomm协议允许两个设备通过真正的 Socket 建立持久的连接。对遥控装置来说这是有趣的,取得了连续的数据流,并且打开一个广泛的采用定制的外围设备的应用。

它是如何工作的?

通过Rfcomm 通道创建一个连接并不像通过网络在两个设备通过Socket连接那样简单。你必须知道特有的事是蓝牙设备配对,这部分无法控制。我们只能在已经配对的蓝牙设备之间建立连接,这看起来好像是限制,但它完成一些令人讨厌的事,例如:安全检查。已经配对的设备也实现了安全认证,所以我们没有必要参与这一方面。因此,当扫描有效设备时(后续文章中说明)只是想简单的找到已经配对成功的设备。

在了解这一点后,连接过程在某种程度上来说是简单的,依赖于连接方的角色。连接端A 和 B,一个做 Server(A),另一个做 Client(B)。首先,A须要绑定蓝牙接口,关打开监听通道等待 Client 连接。这一阶段叫做“advertising”,A 简单的标识其可用性,并等待对待的连接。B 扫描可配对的和和效的设备进行配对。然后,试着去打开 Socket 连接到监听方。此时服务器接受连接,处理监听和得到已经建立表示 Socket 的连接。这样连接是完全有效的,并且两端都可以读/写好似通用的网络连接。

通过 BT连接 Windows Phone 8和 Windows 8.1

下面的示例演示了在 Lumia 925(WinodwsPhone 8) 与ASUS VivoTab(Windows8.1)之间创建连接的代码。在这个方案中,VivoTab 是服务端用于监听一个连接。另一方面Lumia 925试着连接 VivoTab。连接建立后,在手机上的每次点击通过蓝牙通道被传输到VivoTab 上,表现为点击 VivoTab 的屏幕。

让我们从服务端开始。首先,在manifest 中设置设备能力。表现为三个必须增加的元素,可以通过编辑器和手动修改 XML 来完成,修改后的 XML 如下:

1: <Capabilities>  
2:   <Capability Name="privateNetworkClientServer" />  
3:   <Capability Name="internetClientServer" />  
4:   <m2:DeviceCapability Name="bluetooth.rfcomm">  
5:     <m2:Device Id="any">  
6:       <m2:Function Type="serviceId:A7EA96BB-4F95-4A91-9FDD-3CE3CFF1D8DA" />  
7:     </m2:Device>  
8:   </m2:DeviceCapability>  
9: </Capabilities>  

前面两个能力"Internet(Client & Server)" 和 "PrivateNetworks (Client & Server)"可以直接通过编辑manifest 编辑器来设置。其它部分是Rfcomm协议和创建的服务标识,标识符是一个开发者创建的UUID(Guid),用于创建Rfcomm提供者。

然后,在MainPage 页面的加载处开始监听通道:

1: private async TaskStartListen()  
2: {  
3:     Provider =await RfcommServiceProvider.CreateAsync(  
4:        RfcommServiceId.FromUuid(RfcommServiceUuid));  
5:   
6:    StreamSocketListener listener = new StreamSocketListener();  
7:    listener.ConnectionReceived += HandleConnectionReceived;  
8:   
9:     awaitlistener.BindServiceNameAsync(  
10:        Provider.ServiceId.AsString(),  
11:        SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);  
12:   
13:     var writer = new DataWriter();  
14:    writer.WriteByte(ServiceVersionAttributeType);  
15:    writer.WriteUInt32(ServiceVersion);  
16:   
17:     var data =writer.DetachBuffer();  
18:    Provider.SdpRawAttributes.Add(ServiceVersionAttributeId, data);  
19:    Provider.StartAdvertising(listener);  
20: }  

代码首先创建了一个RfcommServiceProvider 实例,此实例标识协议、并且使用写在 manifest 中的 RfcommServiceUuid来创建。然后创建了StremSocketListener,并通过实例绑定在蓝牙通道上。最后,使用 DataWriter 在完成信息的传递。关于这一方面的更多的信息,请看下面的链接:

 https://www.bluetooth.org/en-us/specification/assigned-numbers/service-discovery

一旦使用方法StartAdvertising使提供者进入 advertising 模式,远程的设备可以试着打开一个连接。当一个连接检测到时,ConnectionReceived event被触发、并执行如下代码:

 1 private voidHandleConnectionReceived(StreamSocketListener listener,StreamSocketListenerConnectionReceivedEventArgs args)  
 2 {  
 3    Provider.StopAdvertising();  
 4    listener.Dispose();  
 5     listener = null;  
 6   
 7     this.Socket = args.Socket;  
 8     this.Reader = new DataReader(this.Socket.InputStream);  
 9     this.Run();  
10 }  

提供商的 advertising 模式被停止,监听关闭,这是因为此示例仅处理一个连接。保持 advertising 模式,可以处理多个连接。接受的 Socket 表示已建立的连接,通过此 Socket 可以发送与接收数据。

Windows Phone 8侧的事情简单很多。

 1 private async TaskConnect()  
 2 {  
 3    PeerFinder.AlternateIdentities["Bluetooth:PAIRED"] = "";  
 4   
 5     var devices =await PeerFinder.FindAllPeersAsync();  
 6     var device =devices.FirstOrDefault();  
 7   
 8     if (device != null)  
 9     {  
10         this.Socket = new StreamSocket();  
11         await this.Socket.ConnectAsync(device.HostName, "1");  
12         this.Writer = new DataWriter(this.Socket.OutputStream);  
13         this.bConnect.Visibility = Visibility.Collapsed;  
14         this.LayoutRoot.Tap += LayoutRoot_Tap;  
15     }  
16 }  

使用 PeerFinder 类扫描有效的设备,然后使用第一个设备。此处示例假设仅有一个有效的设备,但实际情况则必须提示一个配对设备的列表来选择。发现的设备通过 HostName 创建和连接 Socket。此时,Windows 8.1 可能询问用户是否接受连接。其余的代码是为了后续的通信创建策略(The rest of the code is to setup thedevice for the following communication.)。实现等待点击屏幕,采集 x 和 y 坐标,并通过已经创建了通道发送坐标:

 1 private async void LayoutRoot_Tap(object sender,System.Windows.Input.GestureEventArgs e)  
 2 {  
 3     if (this.Writer != null)  
 4     {  
 5         varposition = e.GetPosition(this.LayoutRoot);  
 6         this.Writer.WriteInt32(1);  
 7         this.Writer.WriteInt32((int)position.X);  
 8         this.Writer.WriteInt32((int)position.Y);  
 9         await this.Writer.StoreAsync();  
10     }  
11 }  

代码发送了三个整数。第一个代表命令,为了以后的扩展,然后就是坐标。一旦消息进入队列,方法 StoreAsync 通过流发送所有的包。另一方面:

 1 private async void Run()  
 2  {  
 3      while (true)  
 4      {  
 5          try  
 6          {  
 7             Command command = (Command)await this.Reader.ReadInt32Async();  
 8    
 9              switch (command)  
10              {  
11                  case Command.Tap:  
12                     await this.HandleTap();  
13                     break;  
14              }  
15          }  
16          catch (PeerDisconnectedException)  
17          {  
18              return;  
19          }  
20      }  
21  }  
22    
23  private async TaskHandleTap()  
24  {  
25      int x = await this.Reader.ReadInt32Async();  
26      int y = await this.Reader.ReadInt32Async();  
27    
28      await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,  
29          () =>  
30          {  
31             Canvas.SetLeft(ellipse, x);  
32             Canvas.SetTop(ellipse, y);  
33          });  
34  }  

方法 run 是死循环,用于监听一个整数。这个整数是配对方发送来的命令,根据接收到的命令执行不同的处理。在此示例中,读取接下来的两个整数。然后在 canvas 上移动一个小的圆。注意:方法 ReadInt32Async不是 DataReader类的一部分,它是一个扩展方法。

1 public async static Task<Int32> ReadInt32Async(this DataReader reader)  
2 {  
3     uint available = await reader.LoadAsync(sizeof(Int32));  
4   
5     if (available < sizeof(Int32))  
6         throw new PeerDisconnectedException();  
7   
8     return reader.ReadInt32();  
9 }  

 

posted @ 2016-02-19 15:50  91program  阅读(255)  评论(1编辑  收藏  举报