100 likes | 217 Views
Transform your application by incorporating Near Field Communication (NFC) capabilities that enable seamless communication with other devices at a distance of 3–4 centimeters. Discover nearby peers using ProximityDevice and PeerFinder. Easily send and receive messages by subscribing to message events and establish socket connections between apps using tap gestures. The interconnected experience enhances user interaction and promotes data sharing. Leverage these APIs to create a dynamic, networked environment in your app.
E N D
Universal APIs Windows Phone 8.x + Windows 8.x
Windows.Networking.Proximity.ProximityDevice “Makes your app able to communicate with other devices within an approximate range of 3–4 centimeters, and exchange a small payload of data during the tap.”
ProximityDevice - Receiving ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device!= null) { long Id = device.SubscribeForMessage ("Windows.SampleMessageType", messageReceived); Debug.WriteLine("Published Message. ID is {0}", Id); // Store the unique message Id so that it // can be used to stop subscribing for this message type } private void messageReceived(ProximityDevice sender, ProximityMessage message) { Debug.WriteLine("Received from {0}:'{1}'", sender.DeviceId, message.DataAsString); }
Windows.Networking.Proximity.PeerFinder “Lets you discover another instance of your app on a nearby device and create a socket connection between the peer apps by using a tap gesture, or by browsing. A peer app is another instance of an app that is running on another device.”
PeerFinder – Finding other Windows Phone peers ProximityDevice device = ProximityDevice.GetDefault(); // Make sure NFC is supported if (device!= null) { PeerFinder.TriggeredConnectionStateChanged += OnTriggeredConnectionStateChanged; // Start finding peer apps, while making this app discoverable by peers PeerFinder.Start(); }
PeerFinder – Finding other Windows Phone peers StreamSocket _streamSocket; void OnTriggeredConnectionStateChanged(object sender, TriggeredConnectionStateChangedEventArgsargs) { switch (args.State) { case TriggeredConnectState.Listening: // Connecting as host break; case TriggeredConnectState.PeerFound: // Proximity gesture is complete and user can pull their devices away. // Remaining work is to establish the connection using a different //transport, like TCP/IP or Bluetooth break;
PeerFinder – Finding other Windows Phone peers case TriggeredConnectState.Connecting: // Connecting as a client break; case TriggeredConnectState.Completed: // Connection completed, retrieve the socket over which to communicate _streamSocket = args.Socket; break; case TriggeredConnectState.Canceled: break; case TriggeredConnectState.Failed: // Connection was unsuccessful break; } }