如何使用NFC的一款Windows Phone 8的字符串发送到Android设备 [英] How to send a string using NFC from a windows phone 8 to an android device

查看:251
本文介绍了如何使用NFC的一款Windows Phone 8的字符串发送到Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还没有使用谷歌的这个运气好的话,那么我想我会问。

haven't had any luck using google for this so I thought i'd ask.

有没有人有任何经验/知道如何从Windows Phone 8的设备发送一个简单的字符串,如你好到Android设备?

Does anyone have any experience / know how to send a simple string i.e "hello" from a Windows Phone 8 device to an Android Device?

到目前为止,我们已经能够做到的android - > Android和机器人 - >的Windows Phone 8,但我们一直没能找出如何从Windows 8手机做机器人

so far we have been able to do android -> android and android -> windows phone 8 but we haven't been able to find out how to do from windows phone 8 to android.

有没有人见过指南网上或不知道如何做这样的事?

Has anyone seen a guide online or know how to do such a thing?

第一步我想将是找出如何使Windows上的应用程序的手机8实现了近一个Android NFC设备..然后它会找出如何使应用程序在Android手机收到消息。

The first step I guess would be to find out how to make the application on windows phone 8 realize its near an android NFC device .. and then it would be to figure out how to make the application on the android phone receive the message.

在此先感谢!

*答案*

好吧,所以这里有一些答案/提示

Alright so here are some answers/tips

我结束了在发送NFC消息外部类型,因为发送应用程序/ my.mimetype不停地给我一个对不起你的手机不能recorgnize这种类型的文件在Windows Phone上,即使该消息可以通过。

I ended up sending the NFC messages as external type because sending application/my.mimetype kept giving me a "sorry your phone cant recorgnize this type of file" on the windows phone even though the message was getting through.

@Override
public NdefMessage createNdefMessage(NfcEvent event) {

    NdefMessage ndefMessage = new NdefMessage(( 
            new NdefRecord[] {createMimeRecord("packageName:externalType",docId.getBytes())}));

    return ndefMessage;
}

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
    NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, mimeBytes, new byte[0], payload);
    return mimeRecord;
}

所有你需要做的的机器人是按照从SDK样品的Andr​​oid例子(Android的17分之16 - AndroidBeamDemo),它解释了极为彻底的在这里 - 的http://www.tappednfc.com/wp-content/uploads/TAPPED-NFCDeveloperGuide-Part1.pdf

但代替使用应用的mimetype使用上述外部类型并在清单把下面,而不是在意图滤波器mime类型:

but instead of using application mimetype use the above external type and in your manifest put the following instead of the mimetype in the intent filter:

                <data
                android:host="ext"
                android:pathPrefix="/cco.drugformulary:externalType"
                android:scheme="vnd.android.nfc" />

关于阅读和发送使用Windows的消息手机,您可以使用接受的答案家伙说的做什么,它应该工作,但该类型把cco.drugformulary:externalType从上面的(当然你的项目名称)。

regarding reading and sending the message from the windows phone you can use what the accepted answer guy said to do and it should work but for the type put cco.drugformulary:externalType as from above (your project name of course though).

如果您运行的任何问题随时问我。

If you are running into any problems feel free to ask me

推荐答案

在使用WP8 NFC有根本的两种类型的消息,你可以一起工作:针对Windows的消息和NDEF消息。 Windows特定的消息很容易辨认,因为你会被发布他们为Windows。*的消息类型。 NDEF消息却开始使用NDEF消息类型出版。例如,下面是一个Windows应用程序特定的消息:

When using WP8 NFC there's fundamentally two types of messages you can work with: windows-specific messages and NDEF messages. Windows specific messages are easy to spot since you'll be publishing them as "Windows.*" message types. NDEF messages however get published using the "NDEF" message type. For example, here's a Windows app-specific message:

    private void WriteAppSpecificStringToTag(object sender, RoutedEventArgs e)
    {
        ProximityDevice device = ProximityDevice.GetDefault();

        if (device != null)
        {
            device.PublishBinaryMessage("Windows:WriteTag.myApp",
                GetBufferFromString("Hello World!"),
                UnregisterOnSend);

            MessageBox.Show("Tap to write 'Hello World' on tag.");
        }
    }

NDEF是为了优化NFC标签(通常在100个字节)的极其受限的空间环境中的大量使用的跨平台的格式。虽然WP8接近框架允许发送和放大器;接收NDEF消息,它不知道有关NDEF格式的内容。意,所述WP8接近框架发送和接收的字节流。解析字节的数据流,并正确格式化它是你的责任,为应用程序开发人员。

NDEF is a heavily used cross-platform format meant to optimize for the extremely space constrained environment of NFC tags (often under 100 bytes). While the WP8 Proximity framework allows sending & receiving NDEF messages it doesn't know anything about the NDEF format. Meaning, the WP8 proximity framework sends and receives a stream of bytes. Parsing that stream of bytes and formatting it correctly is your responsibility as the app developer.

为了格式化和放大器;解析NDEF消息,你要么需要使用第三方的框架或建立你自己的。在第三方框架 NDEF库接近的API 做了伟大的工作方面。

In order to format & parse NDEF messages you'll need to either use a 3rd party framework or build your own. In terms of 3rd party frameworks NDEF Library for Proximity APIs does a great job.

例如,这里是如何格式化和使用编写一个应用程序特定的NDEF消息NDEF库

For example, here's how to format and write an app-specific NDEF message using the NDEF Library:

    private void WriteNDEFRecordToTag(object sender, RoutedEventArgs e)
    {
        ProximityDevice device = ProximityDevice.GetDefault();

        if (device != null)
        {
            device.PublishBinaryMessage("NDEF:WriteTag",
                new NdefMessage()
                {
                    new NdefRecord
                    {
                        TypeNameFormat = NdefRecord.TypeNameFormatType.ExternalRtd,
                        Type = "myApp".Select(c => (byte) c).ToArray(),
                        Payload = "Hello World!".Select(c => (byte) c).ToArray()
                    }
                }.ToByteArray().AsBuffer(),
                UnregisterOnSend);

            MessageBox.Show("Tap to write 'Hello World' on tag.");
        }
    }

和这里是如何接收和您的应用程序解析NDEF消息:

And here's how to receive and parse NDEF messages in your app:

    private void ReadNDEFRecordFromTag(object sender, RoutedEventArgs e)
    {
        ProximityDevice device = ProximityDevice.GetDefault();

        if (device != null)
        {
            device.SubscribeForMessage("NDEF", ndefMessageRecieved);

            MessageBox.Show("Registered to NFC tag. Tap with NFC tag.");
        }
    }

    private void ndefMessageRecieved(ProximityDevice sender, ProximityMessage message)
    {
        var ndefMessage = NdefMessage.FromByteArray(message.Data.ToArray());

        StringBuilder sb = new StringBuilder();
        foreach (NdefRecord record in ndefMessage)
        {
            sb.AppendLine(Encoding.UTF8.GetString(record.Payload, 0, record.Payload.Length));
        }
        Dispatcher.BeginInvoke(() => MessageBox.Show(sb.ToString()));
    }

当我们运行在WP8这个code片断,然后点击previously写NDEF标签,我们可以看到以下消息:

When we run this code snippet on WP8 and tap the previously written NDEF tag we can see the following message:

如果我们采取同样的NFC标签,并使用Android的<一个href="https://play.google.com/store/apps/details?id=at.mroland.android.apps.nfctaginfo&hl=en">NFC TagInfo 应用程序,我们可以看到同样的数据:

And if we take the same NFC tag and use Android's NFC TagInfo app we can see the same data:

如果你想知道究竟是什么被发送/当你使用NDEF trasnfered,这里的GoToTags Windows应用程序上,我们只需要使用标签:

In case you're wondering what actually gets transmitted/trasnfered when you use NDEF, here's GoToTags Windows App on the tag we just use:

如果NDEF图书馆感觉有点沉重了你,你总是可以炮制出你对自产自销NDEF格式化和解析。还有的,在这个诺基亚OSS项目@ NFC标签阅读器

If NDEF Library feels a bit heavy for you, you can always crank out your on homegrown NDEF formatter and parser. There's a good example of that in this Nokia OSS project @ NFC Tag Reader

对于NFC手机到手机与NFC手机到标签中,code段以上将适用于这两种情况。如果你想要写标签,simlpy保持:WriteTag操作消息类型。如果您想直接与附近的电话沟通只是删除了:WriteTag操作。无论做工精细与WP8&LT; =>安卓

Regarding NFC phone-to-phone vs. NFC phone-to-tag, the code snippets above will work for either scenario. In case you want to write to a tag, simlpy keep the ":WriteTag" operation in the message type. In case you want to communicate directly with a nearby phone just remove the ":WriteTag" operation. Both work fine with WP8<=>Android.

确实注意到,虽然有在差异如何Android的&放大器; WP8解决NDEF。 WP8只能读取第一个NDEF记录中的任何消息,而机器人可以读取所有NDEF记录。机器人可以与非NDEF格式化标签的工作和格式化的; WP8必须使用NDEF格式标签。

Do note though that there are differences in how Android & WP8 address NDEF. WP8 can only read the first NDEF record in a message, whereas Android can read all NDEF records. Android can work with non NDEF-formatted tags and format those; WP8 has to use NDEF formatted tags.

这篇关于如何使用NFC的一款Windows Phone 8的字符串发送到Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆