WP8 ProximityDevice NFC近场通信浅解析
不得不说Windows phone 改为NT内核之后相当的给力,ProximityDevice就是内核里面的东西,在Windows命名空间下。
Proximity cannot be tested using Windows Phone Emulator. You should test proximity using a Windows Phone 8 device.
Bluetooth cannot be tested using Windows Phone Emulator.
ProximityDevice class
类主要使用的成员:
Events:
| DeviceArrived 
 DeviceDeparted | Occurs when a device enters the proximate range. 
 Occurs when a device leaves the proximate range. 
 | 
Methods
| 
 
 
 
 
 
 
 
 | Creates an instance of a ProximityDevice class and activates the specified proximity device interface. 
 Creates an instance of a ProximityDevice class and activates the default proximity provider. 
 
 Publishes a message to subscribers of the specified message type. 
 
 Creates a subscription for a specified message type. 
 | 
该类即封装了nfc设备的操作,简化了流程。
在使用时一端A的应用需要调用subscribeForMessage这个函数进行注册,然后B调用PublishMessage这个方法进行消息的传递。实例代码如下:
 
		
其中需要注意的一点是注册和发布消息参数中字符串的格式必须为"Windows.XXXXX"否则会被系统认为是无效参数
调用比较简单,由于目前模拟器并不支持NFC的测试,所以无法进一步进行研究。但从文档来看,系统对其进行了良好的封装,调用也比较简洁。
项目中应用解析:
碰一下加好友
1、对需求的分析:
碰一下加好友及当两台设备同时开启到这个界面时,会相互发送自己的信息。
2、需求的分解:
从对象角度来说 这个类主要实现如下功能:
a.初始化时调用ProximityDevice.GetDefault()取得底层NFC设备
 
		
b.当发现有其他设备进入到有效区域内则对自己的个人信息进行广播
 
		
 
		
c.检测到其它设备的消息后触发通知此类外的对象,发现好友
 
		
另外:为了此类使用的安全,我们需要在dispose方法中添加取消注册的操作
 
		
如此便可实现碰一碰的需求,上面分析为需求的主要功能点,具体实现参照工程中的相应代码。
对于wp8这个新平台,如有需要交流的地方欢迎加入
QQ群: 182659848
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号