locked
NFC Application for testing NFC device arrival/departure

    Question

  • Hi,

    I want to develop a c# library in VS 2013 .Net 4.0 for NFC device arrival / departure.

    I tried the following code but I'm not able to invoke "device arrival" event. 

    I took the code from : 

    http://msdn.microsoft.com/en-US/library/windows/apps/windows.networking.proximity.devicearrivedeventhandler.aspx

    Code #########

    using system;

    using system.IO;

    using system.Management;

    using windows.networking.proximity;

    using system.threading.Tasks;

    namespace nfcDeviceArrival

    {

    public class NearfieldProximity

    {

    windows.networking.proximity.proximityDevice _proximityDevice;

    public void nfcDevicearrive()

    {

    _ProximityDevice=windows.netwotrking.proximity.proximityDevice.Getdefault();

    if (proximityDevice != null)

    { proximityDevice.DeviceArrived += ProximityDeviceArrived; proximityDevice.DeviceDeparted += ProximityDeviceDeparted; console.writeline("NFC Device Detected");

    console.writeline("Please tap NFC device ");

    }

    public void ProximityDeviceArrived(ProximityDevice sender)

    {

    console.writeline("NFC device Arrived");

    }

    }

    }

    from the above code I'm not able to execute "ProximityDeviceArrived event" and i'm not getting prinitng statement "NFC device Arrived".

    All I want is C# code that should print a "NFC device Arrived" message in cmd prmpt when I bring NFC device near NFC antenna.

    Thanks,

    Devanathan.D

    reach me @ : deva1805@gmail.com



    • Edited by devaa Monday, October 27, 2014 8:55 AM
    Monday, October 27, 2014 8:46 AM

Answers

  • (1)for Windows Runtime Apps (Windows 8, Windows 8.1, Windows Phone 8.1 base on Windows Runtime)

     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 = "No NFC Device"; }
            }
            async private void proximitydevice_DeviceDeparted(ProximityDevice sender)
            { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Departed")); }
    
            async private void proximitydevice_DeviceArrived(ProximityDevice sender)
            { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Arrived")); }
    
            private void WriteMessage(String message)
            { msgtxt.Text = message; }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                
            }
        }

    (2) for Windows Phone Silverlight Apps (Windows Phone 8.0/8.1 Silverlight)

    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("No NFC Device"); }
            }
    
    
            async private void proximitydevice_DeviceDeparted(ProximityDevice sender)
            {
                Windows.UI.Core.CoreDispatcher x = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                await x.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Departed"));
            }
    
            async private void proximitydevice_DeviceArrived(ProximityDevice sender)
            {
                Windows.UI.Core.CoreDispatcher x = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                await x.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Arrived"));
            }
    
    
            private void WriteMessage(string message)
            { msgtxt.Text = message; }
        }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Monday, October 27, 2014 8:13 PM
  • Hi devaa,

    Just a note: Windows Store App is running over .net framework 4.5 and above but not .net 4.0

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, November 05, 2014 10:08 AM
    Moderator

All replies

  • Does the sample code work?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, October 27, 2014 7:04 PM
    Moderator
  • (1)for Windows Runtime Apps (Windows 8, Windows 8.1, Windows Phone 8.1 base on Windows Runtime)

     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 = "No NFC Device"; }
            }
            async private void proximitydevice_DeviceDeparted(ProximityDevice sender)
            { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Departed")); }
    
            async private void proximitydevice_DeviceArrived(ProximityDevice sender)
            { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Arrived")); }
    
            private void WriteMessage(String message)
            { msgtxt.Text = message; }
    
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                
            }
        }

    (2) for Windows Phone Silverlight Apps (Windows Phone 8.0/8.1 Silverlight)

    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("No NFC Device"); }
            }
    
    
            async private void proximitydevice_DeviceDeparted(ProximityDevice sender)
            {
                Windows.UI.Core.CoreDispatcher x = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                await x.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Departed"));
            }
    
            async private void proximitydevice_DeviceArrived(ProximityDevice sender)
            {
                Windows.UI.Core.CoreDispatcher x = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;
                await x.RunAsync(CoreDispatcherPriority.Normal, () => WriteMessage("Arrived"));
            }
    
    
            private void WriteMessage(string message)
            { msgtxt.Text = message; }
        }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Monday, October 27, 2014 8:13 PM
  • Hi devaa,

    Just a note: Windows Store App is running over .net framework 4.5 and above but not .net 4.0

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, November 05, 2014 10:08 AM
    Moderator