歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Windows Phone 8 近場通信 NFC / Bluetooth Proximity

Windows Phone 8 近場通信 NFC / Bluetooth Proximity

日期:2017/3/1 9:53:52   编辑:Linux編程

今天給大家介紹下 windows phone 8 的近場通信技術,常用近場通信技術有幾種 NFC、藍牙(Bluetooth)、Wifi 以上三種都是在WP8的API中所支持的,NFC我個人感覺是一個可以讓人耳目一新的功能。而且NFC設備目前被很多手機廠商應用,目前NFC技術在手機上應用主要有以下五類。

升級到WP8必需知道的13個特性 系列文章目錄地址:http://www.linuxidc.com/Linux/2013-08/89003.htm

  1. 接觸通過(Touch and Go),如門禁管理、車票和門票等,用戶將儲存著票證或門控密碼的設備靠近讀卡器即可,也可用於物流管理。
  2. 接觸支付(Touch and Pay),如非接觸式移動支付,用戶將設備靠近嵌有NFC模塊的POS機可進行支付,並確認交易。
  3. 接觸連接(Touch and Connect),如把兩個NFC設備相連接(如圖1中手機和筆記本電腦),進行點對點(Peer-to-Peer)數據傳輸,例如下載音樂、圖片互傳和交換通訊錄等。
  4. 接觸浏覽(Touch and Explore),用戶可將NFC手機接靠近街頭有NFC功能的智能公用電話或海報,來浏覽交通信息等。
  5. 下載接觸(Load and Touch),用戶可通過GPRS網絡接收或下載信息,用於支付或門禁等功能,如前述,用戶可發送特定格式的短信至家政服務員的手機來控制家政服務員進出住宅的權限。

可能有人會問到關於NFC的安全問題,以及傳輸速度問題,這裡我也給大家列出NFC的特性來幫助大家了解NFC。

  1. 當設備之間的距離在 3-4 厘米(1-1.5 英寸)內時發生通信。
  2. 通信是非常具有選擇性和意圖性的,因為用戶有意將各自的設備放在一起進行連接。
  3. 最大的理論數據傳輸速率是 424 kbits/s。典型的數據傳輸速率在 30 kbits/s 至 60 kbits/s 之間。
  4. 也可以在 NFC 設備和無動力 NFC 芯片或標記之間發生通信。
  5. 另外我們還可以使用 System.Security.Cryptography.AesManaged 對數據流進行加密。

如何在我們的應用中使用NFC呢?下來我逐一給大家介紹。

首先 還是設置我們的 WMAppManifest.xml 文件標記我們應用需要是有近場通信技術

這裡我還選擇了NetWoking 是因為後面我還會使用 Bluetooth 和 TCP/IP (Wi-Fi)連接。

建立NFC的連接我們要用到 Windows.Networking.Proximity.ProximityDevice 我們可以使用 Windows.Networking.Proximity.ProximityDevice.GetDefault(); 來判斷手機硬件是否支持NFC。

        private void InitializeProximityDevice()
        {
            if (mProximityDevice == null)
            {
                mProximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();
            }

            if (mProximityDevice != null)
            {
                mProximityDevice.DeviceArrived += ProximityDeviceArrived;
                mProximityDevice.DeviceDeparted += ProximityDeviceDeparted;
                AddToSysMsgList(MSG_NFC_EXISTS);
            }
            else { AddToSysMsgList(MSG_NFC_NOT_EXISTS); }
        }

上面的代碼還看到了連個事件 DeviceArrived 和 DeviceDeparted 分別用於判斷一個NFC設備進入和離開我們設備的感應區域。

下面我列舉一個發送消息的code

ProximityDevice device = ProximityDevice.GetDefault();

// Make sure NFC is supported
if (device!= null)
{
  long Id = device.PublishMessage("Windows.SampleMessageType", "Hello World!");
  Debug.WriteLine("Published Message. ID is {0}", Id);

  // Store the unique message Id so that it 
  // can be used to stop publishing this message
}

注冊接收消息的code

Windows.Networking.Proximity.ProximityDevice proximityDevice;

private void InitializeProximityDevice()
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

    if (proximityDevice != null) {
        proximityDevice.DeviceArrived += ProximityDeviceArrived;
        proximityDevice.DeviceDeparted += ProximityDeviceDeparted;

        WriteMessageText("Proximity device initialized.\n");
    }
    else
    {
        WriteMessageText("Failed to initialized proximity device.\n");
    }
}

private void ProximityDeviceArrived(Windows.Networking.Proximity.ProximityDevice device)
{
    WriteMessageText("Proximate device arrived. id = " + device.DeviceId + "\n");
}

private void ProximityDeviceDeparted(Windows.Networking.Proximity.ProximityDevice device)
{
    WriteMessageText("Proximate device departed. id = " + device.DeviceId + "\n");
}

// Write a message to MessageBlock on the UI thread.
private Windows.UI.Core.CoreDispatcher messageDispatcher = Window.Current.CoreWindow.Dispatcher;

async private void WriteMessageText(string message, bool overwrite = false)
{
    await messageDispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (overwrite)
                MessageBlock.Text = message;
            else
                MessageBlock.Text += message;
        });
}

這裡WP8除了支持信息的傳輸還支持更多的文件類型

Windows.Networking.Proximity.ProximityDevice proximityDevice;

private void PublishLaunchApp()
{
    proximityDevice = Windows.Networking.Proximity.ProximityDevice.GetDefault();

if (proximityDevice != null)
    {
        // The format of the app launch string is: "<args>\tWindows\t<AppName>".
        // The string is tab or null delimited.

        // The <args> string can be an empty string ("").
        string launchArgs = "user=default";

        // The format of the AppName is: PackageFamilyName!PRAID.
        string praid = "MyAppId"; // The Application Id value from your package.appxmanifest.

        string appName = Windows.ApplicationModel.Package.Current.Id.FamilyName + "!" + praid;

        string launchAppMessage = launchArgs + "\tWindows\t" + appName;

        var dataWriter = new Windows.Storage.Streams.DataWriter();
        dataWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
        dataWriter.WriteString(launchAppMessage);
        var launchAppPubId =
        proximityDevice.PublishBinaryMessage(
            "LaunchApp:WriteTag", dataWriter.DetachBuffer());
    }
}

詳細請參考 如果你使用Nokia的NDEF 請參考

其中包含了 配對請求,Tag信息的寫入,以及設備間交互。

其中交互功能可以使用連接deeplink的形式打開應用傳入參數或者打開應用或者應用商店。參考我之前的帖子:Windows Phone 8 中的應用間通信 http://www.linuxidc.com/Linux/2013-08/89004.htm

Copyright © Linux教程網 All Rights Reserved