Windows 8 Metro 學習筆記(十六)–NFC

來源:互聯網
上載者:User
NFC(Near Field Commuciation),近距離無線通訊技術,是一種短距離的高頻無線通訊技術。那不禁想到它與Bluetooth藍芽有什麼區別呢?
在網上查詢了一下,區別大概分為以下幾點:
(1)傳輸距離
   NFC的傳輸距離一般為10cm,而Bluetooth的傳輸距離可以達到10米
(2)配置
   NFC不需要手動設定匹配,直接將兩部裝置碰觸一下即可;Bluetooth需要單獨設定和匹配
(3)適用場合
   NFC的適用情境比較多,如支付、打卡、刷卡等;Bluetooth的適用情境就比較局限

NFC對應的WinRT API--Windows.Networking.Proximity
PeerFinder
通過該類可以實現去發現附近的裝置並與之建立socket串連,發送接收資訊。 PeerFinder.TriggeredConnectionStateChanged += new TypedEventHandler<object, TriggeredConnectionStateChangedEventArgs>(TriggeredConnectionStateChangedEventHandler);
PeerFinder.ConnectionRequested += new TypedEventHandler<object, ConnectionRequestedEventArgs>(PeerConnectionRequested); 

PeerFinder.Start();
IReadOnlyList<PeerInformation> _peerInformationList=await PeerFinder.FindAllPeersAsync();
StreamSocket _socket = await PeerFinder.ConnectAsync(peerToConnect);

首先註冊兩個事件:
TriggeredConnectionStateChanged :用於跟蹤與遠程機觸碰期間的一個串連狀態
ConnectionRequested: 當遠程機有串連請求時觸發
調用相應的方法:
FindAllPeersAsync():瀏覽所有在無線地區範圍內運行相同APP的同等電腦
await PeerFinder.ConnectAsync():與範圍內的同等電腦相串連

 

ProximityDevice可以發送訊息或訂閱訊息至直接的裝置,即發送一訊息,然後與另一裝置觸碰一下,別一裝置即可收到相應的訊息
事件:
DeviceArrived: 當在近距離範圍內有裝置進入時觸發
DeviceDeparted: 當在近距離範圍內有裝置移出時觸發

 

//先執行個體化一個ProximityDevice對象,並啟用
ProximityDevice _proximityDevice=ProximityDevice.GetDefault();
//發送訊息,當然發送方法有很多重載,可以發送字元,可以發送二進位流,可以發送Uri
long _publishedMessageId = _proximityDevice.PublishMessage("Windows.SampleMessageType", publishText);

 

//另一裝置與執行subscribe方法,即可獲得訊息long _subscribedMessageId = _proximityDevice.SubscribeForMessage("Windows.SampleMessageType", MessageReceived);
 void MessageReceived(ProximityDevice proximityDevice, ProximityMessage message)
        {
            if (message.MessageType == "StringMessage")
            {
                string mes = message.DataAsString;
            }
            else if(message.MessageType=="BufferMessage")
            {
                IBuffer buffer = message.Data;
            }
        }

 當然還有其它的屬性和方法,這裡就不一一試了,做一個完整的Demo流程,測試一下還是挺好玩的。PeerFind:使用這個對象需要先尋找,串連,然後進行傳輸資訊
ProximityDevice:這種方式相對簡單些,一端發送訊息,另一端只要subscribe就可獲得訊息,但前提是距離得相當地近。

 

如果您對該技術有更深的理解也希望能指點下哦~
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.