简体   繁体   English

编写NFC标签以执行基本的android任务

[英]Write NFC Tags to a perform basic android tasks

Objective :- I want to write nfc tag and associate it with basic android functions like toggle bluetooth, wifi etc. When the android app reads the card it should be able to perform the associated tasks. 目标 :-我想编写nfc标签并将其与基本的android功能(如切换蓝牙,wifi等)关联。当android应用读取卡时,它应该能够执行相关的任务。

I have basic idea of reading and writing nfc tag already. 我已经有了读写nfc标签的基本思想。 But I need guidance reagrding how can we associate task with nfc tags, as I found no resource on the same. 但是我需要指导我们如何才能将任务与nfc标签相关联,因为我发现同一资源没有。 What is the standard & best way to achieve it. 什么是实现它的标准和最佳方法。 I know I can write string on the tag and then latter read it to perform task, but I don't know its the best way or not 我知道我可以在标签上写字符串,然后再读取它来执行任务,但是我不知道它的最佳方法

This app is already doing this.I need to do something similar. 这个应用程式已经在执行这项工作。我需要做类似的事情。

I have referred and used to https://github.com/nadam/nfc-reader . 我已经提到并习惯使用https://github.com/nadam/nfc-reader for reading the tags and https://github.com/balloob/Android-NFC-Tag-Writer for writing tags. 用于读取标签,并https://github.com/balloob/Android-NFC-Tag-Writer用于写入标签。

Please help. 请帮忙。

When you tap a tag, Android search in all applications, which one could open it. 轻按标签后,Android会在所有应用程序中搜索,可以打开该应用程序。

So you could create tag with a specific "mime-type" and an application which open this kind of tag. 因此,您可以创建带有特定“ mime类型”的标签以及打开此类标签的应用程序。 An activity is launched because it have "good" filter on this specific "mime-type". 启动活动是因为该活动在此特定的“ MIME类型”上具有“良好”过滤器。 You have now to do your action (toogle-wifi, ...) in this activity. 现在,您必须在此活动中执行操作(toogle-wifi,...)。

You could create NDEF message with "specific" mime type as this: 您可以使用以下“特定” mime类型创建NDEF消息:

String data = "tooggle_wifi";
// New record
NdefRecord appRecord = NdefRecord.createApplicationRecord(context.getPackageName());

// Record with actual data we care about
NdefRecord relayRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                                                new String("application/my.specific.tag")
                                                .getBytes(Charset.forName("US-ASCII")),
                                                null, data.getBytes());

Now to catch this kind of tag, in your AndroidManifest, on activity add this filter: 现在要捕获此类标记,请在您的AndroidManifest中,在活动时添加以下过滤器:

http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#ndef-disc http://developer.android.com/guide/topics/connectivity/nfc/nfc.html#ndef-disc

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="application/my.specific.tag" />
</intent-filter>

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

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