如何使用 NFC 将字符串从 Windows 手机 8 发送到 android 设备 [英] How to send a string using NFC from a windows phone 8 to an android device

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

问题描述

没有使用谷歌的运气,所以我想我会问.

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

到目前为止,我们已经能够做到 android -> android 和 android -> windows phone 8,但我们还没有找到从 windows phone 8 到 android 的方法.

有没有人看过在线指南或知道如何做这样的事情?

我想第一步是找出如何让 Windows Phone 8 上的应用程序在 android NFC 设备附近实现它......然后是弄清楚如何让 android 手机上的应用程序接收到留言.

提前致谢!

* 答案 *

好的,这里有一些答案/提示

我最终将 NFC 消息作为外部类型发送,因为在 Windows 手机上发送 application/my.mimetype 一直给我一个对不起,您的手机无法识别这种类型的文件",即使消息已经通过.

@Override公共 NdefMessage createNdefMessage(NfcEvent 事件) {NdefMessage ndefMessage = new NdefMessage((new NdefRecord[] {createMimeRecord("packageName:externalType",docId.getBytes())}));返回 ndefMessage;}公共 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);返回 mimeRecord;}

您在 android 中要做的就是遵循 SDK 示例 (android-16/17 - AndroidBeamDemo) 中的 android 示例,这里对其进行了非常彻底的解释 - app 我们可以看到同样的数据:

如果您想知道在使用 NDEF 时实际传输/传输的是什么,这里是我们刚刚使用的标签上的 GoToTags Windows 应用程序:

如果 NDEF 库对您来说有点沉重,您可以随时使用自己开发的 NDEF 格式化程序和解析器.在诺基亚 OSS 项目@NFC Tag Reader

中有一个很好的例子.

关于 NFC 电话到电话与 NFC 电话到标签,上面的代码片段适用于任何一种情况.如果你想写一个标签,simlpy 在消息类型中保留 ":WriteTag" 操作.如果您想直接与附近的手机通信,只需删除:WriteTag"操作即可.两者都适用于 WP8<=>Android.

请注意,Android 和 Android 的方式有所不同.WP8 地址 NDEF.WP8 只能读取消息中的第一条 NDEF 记录,而 Android 可以读取所有 NDEF 记录.Android 可以使用非 NDEF 格式的标签并对其进行格式化;WP8 必须使用 NDEF 格式的标签.

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 i.e "hello" from a Windows Phone 8 device to an Android Device?

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?

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.

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.

@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

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" />

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

解决方案

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 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.

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.

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

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

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

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

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:

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

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.

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 手机 8 发送到 android 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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