简体   繁体   English

如何使用NFC从Windows Phone 8向Android设备发送字符串

[英]How to send a string using NFC from a windows phone 8 to an android device

haven't had any luck using google for this so I thought i'd ask. 没有任何运气使用谷歌,所以我想我会问。

Does anyone have any experience / know how to send a simple string ie "hello" from a Windows Phone 8 device to an Android Device? 有没有人有经验/知道如何从Windows Phone 8设备向Android设备发送简单的字符串即“hello”?

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. 到目前为止我们已经能够做到android - > android和android - > windows phone 8但是我们还没有找到如何做到从windows phone 8到android。

Has anyone seen a guide online or know how to do such a thing? 有没有人在网上看过指南或知道怎么做这样的事情?

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. 我想的第一步是找出如何使Windows Phone 8上的应用程序实现其附近的Android NFC设备..然后它将弄清楚如何使Android手机上的应用程序接收消息。

Thanks in advance! 提前致谢!

* Answer * *答案*

Alright so here are some answers/tips 好吧所以这里有一些答案/提示

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. 我最终发送NFC消息作为外部类型,因为发送应用程序/ my.mimetype不断给我一个“抱歉你的手机无法记录这种类型的文件”在Windows手机上,即使消息正在通过。

@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;
}

all you have to do in android is to follow the android example from the SDK samples (android-16/17 - AndroidBeamDemo) which is explained extremely thoroughly here - http://www.tappednfc.com/wp-content/uploads/TAPPED-NFCDeveloperGuide-Part1.pdf 在android中你要做的就是按照SDK示例中的android示例(android-16/17 - AndroidBeamDemo)进行解释,这里的解释非常彻底 - http://www.tappednfc.com/wp-content/uploads/TAPPED -NFCDeveloperGuide-Part1.pdf

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: 但不是使用应用程序mimetype使用上面的外部类型,并在您的清单中放置以下而不是在intent过滤器中的mimetype:

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

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). 关于从Windows手机阅读和发送消息,您可以使用接受的答案人员所做的事情,它应该可以工作但是对于类型把cco.drugformulary:externalType从上面(当然你的项目名称)。

If you are running into any problems feel free to ask me 如果您遇到任何问题,请随时问我

When using WP8 NFC there's fundamentally two types of messages you can work with: windows-specific messages and NDEF messages. 使用WP8 NFC时,基本上可以使用两种类型的消息:特定于Windows的消息和NDEF消息。 Windows specific messages are easy to spot since you'll be publishing them as "Windows.*" message types. Windows特定的消息很容易被发现,因为您将它们作为“Windows。*”消息类型发布。 NDEF messages however get published using the "NDEF" message type. 但是,NDEF消息使用“NDEF”消息类型发布。 For example, here's a Windows app-specific message: 例如,这是一个Windows应用程序特定的消息:

    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 is a heavily used cross-platform format meant to optimize for the extremely space constrained environment of NFC tags (often under 100 bytes). NDEF是一种使用频繁的跨平台格式,旨在优化NFC标签极其空间受限的环境(通常低于100字节)。 While the WP8 Proximity framework allows sending & receiving NDEF messages it doesn't know anything about the NDEF format. 虽然WP8 Proximity框架允许发送和接收NDEF消息,但它对NDEF格式一无所知。 Meaning, the WP8 proximity framework sends and receives a stream of bytes. 意思是,WP8邻近框架发送和接收字节流。 Parsing that stream of bytes and formatting it correctly is your responsibility as the app developer. 解析该字节流并正确格式化是您作为应用程序开发人员的责任。

In order to format & parse NDEF messages you'll need to either use a 3rd party framework or build your own. 为了格式化和解析NDEF消息,您需要使用第三方框架或构建自己的框架。 In terms of 3rd party frameworks NDEF Library for Proximity APIs does a great job. 就第三方框架而言, NDEF Library for Proximity API做得非常好。

For example, here's how to format and write an app-specific NDEF message using the NDEF Library : 例如,以下是如何使用NDEF库格式化和编写特定于应用程序的NDEF消息:

    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.");
        }
    }

And here's how to receive and parse NDEF messages in your app: 以下是如何在您的应用中接收和解析NDEF消息:

    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()));
    }

When we run this code snippet on WP8 and tap the previously written NDEF tag we can see the following message: 当我们在WP8上运行此代码片段并点击之前编写的NDEF标记时,我们可以看到以下消息:

MessageBox说Hello World

And if we take the same NFC tag and use Android's NFC TagInfo app we can see the same data: 如果我们使用相同的NFC标签并使用Android的NFC TagInfo应用程序,我们可以看到相同的数据:

Android上的NfcTag Info数据

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时实际传输/传输的是什么,这里是我们刚才使用的标签上的GoToTags Windows应用程序:

GoToTags显示存储在NDEF标记中的二进制数据

If NDEF Library feels a bit heavy for you, you can always crank out your on homegrown NDEF formatter and parser. 如果NDEF Library对您来说感觉有点沉重,那么您可以随时使用自己开发的NDEF格式化程序和解析器。 There's a good example of that in this Nokia OSS project @ NFC Tag Reader 诺基亚OSS项目@ NFC标签阅读器就是一个很好的例子

Regarding NFC phone-to-phone vs. NFC phone-to-tag, the code snippets above will work for either scenario. 关于NFC手机到手机与NFC手机到标签,上面的代码片段适用于任何一种情况。 In case you want to write to a tag, simlpy keep the ":WriteTag" operation in the message type. 如果要写入标记,请在消息类型中保留“:WriteTag”操作。 In case you want to communicate directly with a nearby phone just remove the ":WriteTag" operation. 如果您想直接与附近的手机通信,请删除“:WriteTag”操作。 Both work fine with WP8<=>Android. 两者都适用于WP8 <=> Android。

Do note though that there are differences in how Android & WP8 address NDEF. 请注意,Android和WP8如何解决NDEF存在差异。 WP8 can only read the first NDEF record in a message, whereas Android can read all NDEF records. WP8只能读取消息中的第一个NDEF记录,而Android可以读取所有NDEF记录。 Android can work with non NDEF-formatted tags and format those; Android可以使用非NDEF格式的标签并格式化这些标签; WP8 has to use NDEF formatted tags. WP8必须使用NDEF格式的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用NFC将文件从(Windows)PC发送到android / ios? - How to send file from (windows) PC to android/ios using NFC? 从 Android 设备向 Windows CE 设备发送/获取字符串数据? - Send/get a string data from Android device to Windows CE device? 如何使用电话号码将通知从Android应用发送到另一个Android设备? - How to send a notification from an android app to another android device using phone number? 使用 NFC 配置 Android 设备 - Provisioning an Android device using NFC 如何使用Android手机检测NFC设备的类型? - How to detect the type of NFC devices using an android phone? 如何从您的Android应用程序向Windows Phone发送短信? - How to send sms to windows phone from your android app? 如何使用Android NFC以编程方式将图像从一台设备传输到另一台设备 - How to transfer images from one device into another using android NFC programmatically 如何在Android中使用NFC在两台设备之间发送数据? - How to send data between two devices using NFC in android? 如何在Android和Windows Phone之间使用NFC发送数据 - How to Send Data Using NFC Between Android And WindowsPhone 如何使用Android发送ATR命令以重置NFC安全元素? - How to send ATR command to reset nfc secure element using android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM