简体   繁体   中英

Share text using NFC in Windows Phone 8

I am trying to create an app for learning purpose in which one windows phone 8 user sends the text and other user receives it. And the text is shared through NFC. But the problem is the other user is not able to receive the text.

Here is the code=>

Receiver's Code:

ProximityDevice device;
long subscribedMessageId;
private void receive_Click(object sender, RoutedEventArgs e)
    {
        device = ProximityDevice.GetDefault();
        if (device != null)
        {
            subscribedMessageId = device.SubscribeForMessage("Windows.SampleMessage", messageReceivedHandler);
        }
    }

    private void messageReceivedHandler(ProximityDevice sender, ProximityMessage message)
    {
        rtextbox.Text = message.DataAsString;
        device.StopSubscribingForMessage(subscribedMessageId);
    }

Sender's Code:

ProximityDevice device;
long publishedMessageId;
private void send_Click(object sender, RoutedEventArgs e)
    {
        device = ProximityDevice.GetDefault();
        device.StopPublishingMessage(publishedMessageId);
        if (device != null)
        {
            publishedMessageId = device.PublishMessage("Windows.SampleMessage", textbox1.Text);
            textbox1.Text = "";
        }
    }

Both the codes are present on different page. Code is executed when the user clicks on send or receive button respectively.

I am new to NFC so any help will be appreciated.

Unfortunately, NFC gets complicated. There's quite a bit of plumbing involved to handle peer-to-peer communication seamlessly. It's far too much to put into an answer here on the site, so I will have to resort to links.

You can check out this Nokia article and project to work through your understanding of the plumbing and get an application going that will exchange text. http://developer.nokia.com/Resources/Library/Lumia/#!code-examples/nfc-talk.html

Then, if you want to take it up a notch, you can work through this article to upgrade your app to allow image transfer functionality as well. http://developer.nokia.com/Community/Wiki/Transfer_an_Image_with_NFC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM