[NFC] NFC carrier detection device (Windows 8 / Windows Phone 8)

This explains how to use Proximity API detected on Windows 8 / Windows Phone 8


       When we want to write about the first thing NFC device capabilities related procedures it is to declare the use of near field communication in the information list.

Windows 8 Windows Phone 8
Proximity statement Statement ID_CAP_PROXIMITY
image image

       This very basic example program, uses only detect if there is another close-up equipment (included Tag) carrier is detected.

       [Example of a 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; }
    }

       This example is very simple and only use ProximityDevice class, first we have to get the system to use NFC device ProximityDevice.GetDefault () static method, the point to note is that it just 'there' NFC devices, as long as there is a NFC device return value will not be null; in other words, there is no way to confirm this approach and NFC device is in a state of open or closed.

       Followed by two events ProximityDevice.DeviceArrived (on behalf of other devices into a carrier range) and ProximityDevice.DeviceDeparted (on behalf of the original equipment have been in the range of from carrier) was added which event delegation function: proximitydevice_DeviceArrived and proximitydevice_DeviceDeparted, particular attention one thing is to delegate the function of these two events will be executed in another thread, it is necessary to change the UI controls through Dispatcher.BeginInvoke method.

         [Example Two 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 wording and Windows Phone 8 is almost the same, the only difference is that cross-thread processing method is to use CoreDispatcher.RunAsync, I read a lot of articles about Windows Phone Proximity blog, and they are long and Sample Code Windows Store App Like using CoreDispatcher.RunAsync method (yes, included in the Microsoft Windows phone 8 Dev Center own Sample), but I have never put that program compilation succeeded; I have tried many different items on the Windows phone 8 CoreDispatcher.RunAsync way to invoke methods, but so far has been in the outcome of failure. If someone successfully tested how to use CoreDispatcher.RunAsync method, the trouble to tell me on what Windows Phone is how it comes out.


Original: Large column  [NFC] NFC carrier detection device (Windows 8 / Windows Phone 8)


Guess you like

Origin www.cnblogs.com/petewell/p/11489843.html