[NFC] NFC 裝置載波偵測 (Windows 8/ Windows Phone 8)

這篇介紹如何在 Windows 8/Windows Phone 8 上使用 Proximity API 偵測

        當我們要撰寫關於 NFC 裝置相關程式的第一件事就是在資訊清單中宣告使用近場通訊的能力。

Windows 8 Windows Phone 8
宣告 Proximity 宣告 ID_CAP_PROXIMITY
image image

 

 

        這篇的範例程式非常基礎,用途只有偵測是否有另一個近接裝置 (包含 Tag) 的載波被偵測到。

        [範例一 Windows Phone 8 ]

 


 public partial class MainPage : PhoneApplicationPage
    {
        private ProximityDevice proximitydevice;
        public MainPage()
        {
            InitializeComponent();
            proximitydevice = ProximityDevice.GetDefault();
            if (proximitydevice != null)
            {
                proximitydevice.DeviceArrived += proximitydevice_DeviceArrived;
                proximitydevice.DeviceDeparted += proximitydevice_DeviceDeparted;
            }
            else
            { MessageBox.Show("此裝置不具備 NFC 功能"); }
        }

        private void proximitydevice_DeviceDeparted(ProximityDevice sender)
        { Dispatcher.BeginInvoke(() => WriteMessage("離開"));}

        private void proximitydevice_DeviceArrived(ProximityDevice sender)
        { Dispatcher.BeginInvoke(() => WriteMessage("進入")); }

        private void WriteMessage(string message)
        { msgtxt.Text = message; }
    }

 

 

        這個範例非常簡單只用到 ProximityDevice 類別 ,一開始我們必須先使用 ProximityDevice.GetDefault() 靜態方法來取得系統中的 NFC 裝置,要注意的一點是它只管『有沒有』NFC 裝置,只要是有 NFC 裝置回傳值就不會是 null;換言之,這個方法並沒有辦法確認 NFC 裝置是處在開啟或關閉的狀態。

 

 

        接著為兩個事件 ProximityDevice.DeviceArrived (代表有其它裝置進入到載波範圍) 與 ProximityDevice.DeviceDeparted (代表有個原來在載波範圍內的裝置已經脫離) 加入其事件委派函式:proximitydevice_DeviceArrived 與 proximitydevice_DeviceDeparted,特別要注意的一件事情是這兩個事件的委派函式會在另一個執行緒中執行,所以必須透過 Dispatcher.BeginInvoke 方法 來變更 UI 控制項。

 

 

          [範例二 Windows 8 ]

 


    public sealed partial class MainPage : Page
    {
        private ProximityDevice proximitydevice;
        public MainPage()
        {
            this.InitializeComponent();
            proximitydevice = ProximityDevice.GetDefault();
            if (proximitydevice != null)
            {
                proximitydevice.DeviceArrived += proximitydevice_DeviceArrived;
                proximitydevice.DeviceDeparted += proximitydevice_DeviceDeparted;
            }
            else
            { msgtxt.Text = "此裝置不具備 NFC 功能"; }
        }
        async private void proximitydevice_DeviceDeparted(ProximityDevice sender)
        { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("離開")); }

        async private void proximitydevice_DeviceArrived(ProximityDevice sender)
        { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("進入")); }

        private void WriteMessage(String message)
        { msgtxt.Text = message; }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
        }
    }

 

 

        Windows Store App 的寫法和 Windows Phone 8 幾乎是一樣,唯一的不同是跨執行緒的處理是使用 CoreDispatcher.RunAsync 方法 ,我看過很多關於 Windows Phone Proximity 部落格的文章,他們的 Sample Code 都長的和 Windows Store App 一樣用 CoreDispatcher.RunAsync 方法 (是的,包含微軟在 Windows phone 8 Dev Center 自己的 Sample ) ,但我從來未曾把那個程式碼編譯成功過;我也曾在 Windows phone 8 專案上試過很多種不同的方式來呼叫  CoreDispatcher.RunAsync 方法 ,不過截至目前一直處在失敗的結局。如果有人試成功怎麼在 Windows Phone 上用 CoreDispatcher.RunAsync 方法,麻煩告訴我一下是怎麼弄出來的。