I'm trying get acquainted with NFC capability of MIT app inventor, so I looked at the tutorial for the NFC cup game and downloaded it onto my Nexus 7. However, every time I scan a tag (Topaz 512), the tablet automatically reads it and says "Empty tag." I can't get my application to access it.
Any ideas on what's going on?
--
are you writing to the tag first?
--
I've tried with tags that are written and with empty tags. Either way, the tablet itself reads it (and reports it empty if empty), but the cup game app does not seem to connect with tags at all.
--
I am having the same issue as Leen, using the same tags. Simply using the "write" function in AppInventor is not actually writing the tag; instead, a dialogue about a "New Tag" pops up, which seems related to the Tag Dispatch System...
Jos, which tags specifically have you had success writing to using AppInventor? I'm happy to just buy whatever you're using.
I am beginning to suspect that AI2 is simply not compatible with the tags I am using. I am using Topaz 512 tags, which are NFC forum type 2. Should I switch?
--
Solution found!
For some reason, the none of the tags work right from the manufacturer. After I write a test message using the app NFC TagWriter by NXP, the tags are formatted and work perfectly with AI2
The NFC TagWriter by NXP stores contacts, bookmarks, geo location, Bluetooth Handover, SMS, Mail, text messages and many more to any NFC-enabled tags as well as to items like posters, business cards, watches and many more containing NFC-enabled electronics. Once data has been stored the application allows also reading and viewing the programmed data including options to launch applications automatically based on the contained data.
NOTE: Installing TagWriter on a non-NFC enabled phone is enabled now but it does of course not allow you to actually program tags. Anyhow, you can now prepare NFC data sets e.g. on a large screen device and then share them with an NFC enabled handset to actually make use of them. Besides that you can catch a pre-view on what you could do with a handset supporting NFC. Please contact us at nfcapp.support@nxp.com if you have problems. We will continue our efforts to add new features for your benefits and we work hard to provide a feature rich application ahead of other apps available in the market. Advanced Features: http://inspire.nxp.com/tagwriter/tag-writer-user-manual.pdf Main Features: - Extended support for NXP NTAG 21x Tags – UID mirror, Counter Mirror and Password Feature - WiFi Pairing for handsets supporting this feature - Bluetooth pairing for all NFC enabled Android versions - Application launching with optional data handover - Easily create tag content from your contacts and bookmarks - Create new content with the included NFC data set editor - Convert QR Codes into NFC data sets - View the existing contents of a tag - Export, import and share NFC data sets - Manage your own history of tags viewed and written - Tap to Launch feature to execute content of NFC tags with a simple tap - Backup the contents of a tag before writing it - Erase the contents of a tag - Insert increasing counter values when writing NFC data sets - Write-protect a tag - Write multiple tags in sequence - Write multiple Datasets in CSV format on multiple tags in sequence Semicolon separator: https://inspire.nxp.com/tagwriter/TagWriter_MassEncoding_template.csv Comma separator: https://inspire.nxp.com/tagwriter/TagWriter_MassEncoding_template_eng.csv
Next to the creation and storing of NFC data sets the NFC TagWriter by NXP also allows to automatically launching applications based on the data read from NFC-enabled tags and items containing NFC-enabled electronics. The following NFC data sets are supported to launch applications: - Contact (vCard) to import data into the address book (Contacts) - Bookmarks (URL) to open a given web address - Plain text to display simple messages directly on the screen - SMS (SMS URI) to create a new message - Mail (Mail URI) to create a new mail message - Telephone number (TEL URI) to start a call - Bluetooth to pair and connect devices - WiFi to connect to WiFi Networks - Geo location to open a given destination- File URI to open a file on the device, including file picker UI - URI to create any kind of URI, also those not directly supported by the NXP TagWriter
It supports a series of configuration options adapting the application to your own needs and preferences.
The application fully supports the NFC Forum Type 1 Tag, Type 2 Tag, Type 3 Tag as well as Type 4 Tag portfolio. This includes a whole range of NXP and third party NFC-enabled contactless IC products like MIFARE Ultralight, MIFARE Classic, MIFARE DESFire, NTAG 21x, NTAG I2C, ICODE SLI & many others.TagWriter supports any NDEF pre-formatted tags for all NDEF operations but formating to NDEF feature is limited only to NXP Tags.
Please help us to improve and provide “crash report” and/or contact us via nfcapp.support@nxp.com. This helps us to detect, analyze and provide fix for the ICs that have not been verified to work and so may cause problems or unexpected behavior on use. -- could you possible upload ur code. im all week trying to get this nfc app to work to no avail.
--
Go to Settings -> Application Manager -> Swipe in from the right until you are at the heading all. Scroll down and look for the app tags. Click on it and disable it
This document describes the basic NFC tasks you perform in Android. It explains how to send and receive NFC data in the form of NDEF messages and describes the Android framework APIs that support these features. For more advanced topics, including a discussion of working with non-NDEF data, see Advanced NFC.
There are two major uses cases when working with NDEF data and Android:
Reading NDEF data from an NFC tag
Beaming NDEF messages from one device to another with Android Beam™
Reading NDEF data from an NFC tag is handled with the tag dispatch system, which analyzes discovered NFC tags, appropriately categorizes the data, and starts an application that is interested in the categorized data. An application that wants to handle the scanned NFC tag can declare an intent filter and request to handle the data.
The Android Beam™ feature allows a device to push an NDEF message onto another device by physically tapping the devices together. This interaction provides an easier way to send data than other wireless technologies like Bluetooth, because with NFC, no manual device discovery or pairing is required. The connection is automatically started when two devices come into range. Android Beam is available through a set of NFC APIs, so any application can transmit information between devices. For example, the Contacts, Browser, and YouTube applications use Android Beam to share contacts, web pages, and videos with other devices.
The Tag Dispatch System
Android-powered devices are usually looking for NFC tags when the screen is unlocked, unless NFC is disabled in the device's Settings menu. When an Android-powered device discovers an NFC tag, the desired behavior is to have the most appropriate activity handle the intent without asking the user what application to use. Because devices scan NFC tags at a very short range, it is likely that making users manually select an activity would force them to move the device away from the tag and break the connection. You should develop your activity to only handle the NFC tags that your activity cares about to prevent the Activity Chooser from appearing.
To help you with this goal, Android provides a special tag dispatch system that analyzes scanned NFC tags, parses them, and tries to locate applications that are interested in the scanned data. It does this by:
Parsing the NFC tag and figuring out the MIME type or a URI that identifies the data payload in the tag.
Before you begin writing your NFC applications, it is important to understand the different types of NFC tags, how the tag dispatch system parses NFC tags, and the special work that the tag dispatch system does when it detects an NDEF message. NFC tags come in a wide array of technologies and can also have data written to them in many different ways. Android has the most support for the NDEF standard, which is defined by the NFC Forum.
NDEF data is encapsulated inside a message (NdefMessage) that contains one or more records (NdefRecord). Each NDEF record must be well-formed according to the specification of the type of record that you want to create. Android also supports other types of tags that do not contain NDEF data, which you can work with by using the classes in the android.nfc.tech package. To learn more about these technologies, see the Advanced NFC topic. Working with these other types of tags involves writing your own protocol stack to communicate with the tags, so we recommend using NDEF when possible for ease of development and maximum support for Android-powered devices.
Now that you have some background in NFC tags, the following sections describe in more detail how Android handles NDEF formatted tags. When an Android-powered device scans an NFC tag containing NDEF formatted data, it parses the message and tries to figure out the data's MIME type or identifying URI. To do this, the system reads the first NdefRecord inside the NdefMessage to determine how to interpret the entire NDEF message (an NDEF message can have multiple NDEF records). In a well-formed NDEF message, the first NdefRecord contains the following fields:
3-bit TNF (Type Name Format)
Indicates how to interpret the variable length type field. Valid values are described in described in Table 1.
Variable length type
Describes the type of the record. If using TNF_WELL_KNOWN, use this field to specify the Record Type Definition (RTD). Valid RTD values are described in Table 2.
Variable length ID
A unique identifier for the record. This field is not used often, but if you need to uniquely identify a tag, you can create an ID for it.
Variable length payload
The actual data payload that you want to read or write. An NDEF message can contain multiple NDEF records, so don't assume the full payload is in the first NDEF record of the NDEF message.
The tag dispatch system uses the TNF and type fields to try to map a MIME type or URI to the NDEF message. If successful, it encapsulates that information inside of a ACTION_NDEF_DISCOVERED intent along with the actual payload. However, there are cases when the tag dispatch system cannot determine the type of data based on the first NDEF record. This happens when the NDEF data cannot be mapped to a MIME type or URI, or when the NFC tag does not contain NDEF data to begin with. In such cases, a Tag object that has information about the tag's technologies and the payload are encapsulated inside of a ACTION_TECH_DISCOVERED intent instead.
Table 1 describes how the tag dispatch system maps TNF and type fields to MIME types or URIs. It also describes which TNFs cannot be mapped to a MIME type or URI. In these cases, the tag dispatch system falls back to ACTION_TECH_DISCOVERED.
For example, if the tag dispatch system encounters a record of type TNF_ABSOLUTE_URI, it maps the variable length type field of that record into a URI. The tag dispatch system encapsulates that URI in the data field of an ACTION_NDEF_DISCOVERED intent along with other information about the tag, such as the payload. On the other hand, if it encounters a record of type TNF_UNKNOWN, it creates an intent that encapsulates the tag's technologies instead.
URI based on the URN in the type field. The URN is encoded into the NDEF type field in a shortened form: <domain_name>:<service_name>. Android maps this to a URI in the form:vnd.android.nfc://ext/<domain_name>:<service_name>.
MIME type or URI depending on the Record Type Definition (RTD), which you set in the type field. See Table 2 for more information on available RTDs and their mappings.
Table 2. Supported RTDs for TNF_WELL_KNOWN and their mappings
When the tag dispatch system is done creating an intent that encapsulates the NFC tag and its identifying information, it sends the intent to an interested application that filters for the intent. If more than one application can handle the intent, the Activity Chooser is presented so the user can select the Activity. The tag dispatch system defines three intents, which are listed in order of highest to lowest priority:
ACTION_NDEF_DISCOVERED: This intent is used to start an Activity when a tag that contains an NDEF payload is scanned and is of a recognized type. This is the highest priority intent, and the tag dispatch system tries to start an Activity with this intent before any other intent, whenever possible.
ACTION_TECH_DISCOVERED: If no activities register to handle the ACTION_NDEF_DISCOVERED intent, the tag dispatch system tries to start an application with this intent. This intent is also directly started (without starting ACTION_NDEF_DISCOVERED first) if the tag that is scanned contains NDEF data that cannot be mapped to a MIME type or URI, or if the tag does not contain NDEF data but is of a known tag technology.
If no activities filter for that intent, try to start an Activity with the next lowest priority intent (either ACTION_TECH_DISCOVERED or ACTION_TAG_DISCOVERED) until an application filters for the intent or until the tag dispatch system tries all possible intents.
If no applications filter for any of the intents, do nothing.
Whenever possible, work with NDEF messages and the ACTION_NDEF_DISCOVERED intent, because it is the most specific out of the three. This intent allows you to start your application at a more appropriate time than the other two intents, giving the user a better experience.
Requesting NFC Access in the Android Manifest
Before you can access a device's NFC hardware and properly handle NFC intents, declare these items in your AndroidManifest.xmlfile:
The NFC <uses-permission> element to access the NFC hardware:
The minimum SDK version that your application can support. API level 9 only supports limited tag dispatch via ACTION_TAG_DISCOVERED, and only gives access to NDEF messages via the EXTRA_NDEF_MESSAGES extra. No other tag properties or I/O operations are accessible. API level 10 includes comprehensive reader/writer support as well as foreground NDEF pushing, and API level 14 provides an easier way to push NDEF messages to other devices with Android Beam and extra convenience methods to create NDEF records.
<uses-sdkandroid:minSdkVersion="10"/>
The uses-feature element so that your application shows up in Google Play only for devices that have NFC hardware:
If your application uses NFC functionality, but that functionality is not crucial to your application, you can omit the uses-featureelement and check for NFC avalailbility at runtime by checking to see if getDefaultAdapter() is null.
Because NFC tag deployments vary and are many times not under your control, this is not always possible, which is why you can fallback to the other two intents when necessary. When you have control over the types of tags and data written, it is recommended that you use NDEF to format your tags. The following sections describe how to filter for each type of intent.
ACTION_NDEF_DISCOVERED
To filter for ACTION_NDEF_DISCOVERED intents, declare the intent filter along with the type of data that you want to filter for. The following example filters for ACTION_NDEF_DISCOVERED intents with a MIME type of text/plain:
If your activity filters for the ACTION_TECH_DISCOVERED intent, you must create an XML resource file that specifies the technologies that your activity supports within a tech-list set. Your activity is considered a match if a tech-list set is a subset of the technologies that are supported by the tag, which you can obtain by calling getTechList().
For example, if the tag that is scanned supports MifareClassic, NdefFormatable, and NfcA, your tech-list set must specify all three, two, or one of the technologies (and nothing else) in order for your activity to be matched.
The following sample defines all of the technologies. You can remove the ones that you do not need. Save this file (you can name it anything you wish) in the <project-root>/res/xml folder.
You can also specify multiple tech-list sets. Each of the tech-list sets is considered independently, and your activity is considered a match if any single tech-list set is a subset of the technologies that are returned by getTechList(). This provides AND and ORsemantics for matching technologies. The following example matches tags that can support the NfcA and Ndef technologies or can support the NfcB and Ndef technologies:
In your AndroidManifest.xml file, specify the resource file that you just created in the <meta-data> element inside the <activity>element like in the following example:
If an activity starts because of an NFC intent, you can obtain information about the scanned NFC tag from the intent. Intents can contain the following extras depending on the tag that was scanned:
EXTRA_TAG (required): A Tag object representing the scanned tag.
To obtain these extras, check to see if your activity was launched with one of the NFC intents to ensure that a tag was scanned, and then obtain the extras out of the intent. The following example checks for the ACTION_NDEF_DISCOVERED intent and gets the NDEF messages from an intent extra.
@Overrideprotectedvoid onNewIntent(Intent intent){super.onNewIntent(intent);...if(intent !=null&&NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())){Parcelable[] rawMessages =
intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);if(rawMessages !=null){NdefMessage[] messages =newNdefMessage[rawMessages.length];for(int i =0; i < rawMessages.length; i++){
messages[i]=(NdefMessage) rawMessages[i];}// Process the messages array....}}}
Alternatively, you can obtain a Tag object from the intent, which will contain the payload and allow you to enumerate the tag's technologies:
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Creating Common Types of NDEF Records
This section describes how to create common types of NDEF records to help you when writing to NFC tags or sending data with Android Beam. Starting with Android 4.0 (API level 14), the createUri() method is available to help you create URI records automatically. Starting in Android 4.1 (API level 16), createExternal() and createMime() are available to help you create MIME and external type NDEF records. Use these helper methods whenever possible to avoid mistakes when manually creating NDEF records.
This section also describes how to create the corresponding intent filter for the record. All of these NDEF record examples should be in the first NDEF record of the NDEF message that you are writing to a tag or beaming.
TNF_ABSOLUTE_URI
Note: We recommend that you use the RTD_URI type instead of TNF_ABSOLUTE_URI, because it is more efficient.
You can create a TNF_ABSOLUTE_URI NDEF record in the following way :
byte[] uriField ="example.com".getBytes(Charset.forName("US-ASCII"));byte[] payload =newbyte[uriField.length +1];//add 1 for the URI Prefixbyte payload[0]=0x01;//prefixes http://www. to the URISystem.arraycopy(uriField,0, payload,1, uriField.length);//appends URI to payloadNdefRecord rtdUriRecord =newNdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_URI,newbyte[0], payload);
The intent filter for the previous NDEF record would look like this:
byte[] payload;//assign to your dataString domain ="com.example";//usually your app's package nameString type ="externalType";NdefRecord extRecord =NdefRecord.createExternal(domain, type, payload);
Use TNF_EXTERNAL_TYPE for more generic NFC tag deployments to better support both Android-powered and non-Android-powered devices.
Note: URNs for TNF_EXTERNAL_TYPE have a canonical format of: urn:nfc:ext:example.com:externalType, however the NFC Forum RTD specification declares that the urn:nfc:ext: portion of the URN must be omitted from the NDEF record. So all you need to provide is the domain (example.com in the example) and type (externalType in the example) separated by a colon. When dispatching TNF_EXTERNAL_TYPE, Android converts the urn:nfc:ext:example.com:externalType URN to a vnd.android.nfc://ext/example.com:externalType URI, which is what the intent filter in the example declares.
Android Application Records
Introduced in Android 4.0 (API level 14), an Android Application Record (AAR) provides a stronger certainty that your application is started when an NFC tag is scanned. An AAR has the package name of an application embedded inside an NDEF record. You can add an AAR to any NDEF record of your NDEF message, because Android searches the entire NDEF message for AARs. If it finds an AAR, it starts the application based on the package name inside the AAR. If the application is not present on the device, Google Play is launched to download the application.
AARs are useful if you want to prevent other applications from filtering for the same intent and potentially handling specific tags that you have deployed. AARs are only supported at the application level, because of the package name constraint, and not at the Activity level as with intent filtering. If you want to handle an intent at the Activity level, use intent filters.
If a tag contains an AAR, the tag dispatch system dispatches in the following manner:
Try to start an Activity using an intent filter as normal. If the Activity that matches the intent also matches the AAR, start the Activity.
If the Activity that filters for the intent does not match the AAR, if multiple Activities can handle the intent, or if no Activity handles the intent, start the application specified by the AAR.
If no application can start with the AAR, go to Google Play to download the application based on the AAR.
Note: You can override AARs and the intent dispatch system with the foreground dispatch system, which allows a foreground activity to have priority when an NFC tag is discovered. With this method, the activity must be in the foreground to override AARs and the intent dispatch system.
If you still want to filter for scanned tags that do not contain an AAR, you can declare intent filters as normal. This is useful if your application is interested in other tags that do not contain an AAR. For example, maybe you want to guarantee that your application handles proprietary tags that you deploy as well as general tags deployed by third parties. Keep in mind that AARs are specific to Android 4.0 devices or later, so when deploying tags, you most likely want to use a combination of AARs and MIME types/URIs to support the widest range of devices. In addition, when you deploy NFC tags, think about how you want to write your NFC tags to enable support for the most devices (Android-powered and other devices). You can do this by defining a relatively unique MIME type or URI to make it easier for applications to distinguish.
Android provides a simple API to create an AAR, createApplicationRecord(). All you need to do is embed the AAR anywhere in your NdefMessage. You do not want to use the first record of your NdefMessage, unless the AAR is the only record in the NdefMessage. This is because the Android system checks the first record of an NdefMessage to determine the MIME type or URI of the tag, which is used to create an intent for applications to filter. The following code shows you how to create an AAR:
Android Beam allows simple peer-to-peer data exchange between two Android-powered devices. The application that wants to beam data to another device must be in the foreground and the device receiving the data must not be locked. When the beaming device comes in close enough contact with a receiving device, the beaming device displays the "Touch to Beam" UI. The user can then choose whether or not to beam the message to the receiving device.
Note: Foreground NDEF pushing was available at API level 10, which provides similar functionality to Android Beam. These APIs have since been deprecated, but are available to support older devices. See enableForegroundNdefPush() for more information.
You can enable Android Beam for your application by calling one of the two methods:
setNdefPushMessage(): Accepts an NdefMessage to set as the message to beam. Automatically beams the message when two devices are in close enough proximity.
setNdefPushMessageCallback(): Accepts a callback that contains a createNdefMessage() which is called when a device is in range to beam data to. The callback lets you create the NDEF message only when necessary.
An activity can only push one NDEF message at a time, so setNdefPushMessageCallback() takes precedence over setNdefPushMessage() if both are set. To use Android Beam, the following general guidelines must be met:
The activity that is beaming the data must be in the foreground. Both devices must have their screens unlocked.
You must encapsulate the data that you are beaming in an NdefMessage object.
The NFC device that is receiving the beamed data must support the com.android.npp NDEF push protocol or NFC Forum's SNEP (Simple NDEF Exchange Protocol). The com.android.npp protocol is required for devices on API level 9 (Android 2.3) to API level 13 (Android 3.2). com.android.npp and SNEP are both required on API level 14 (Android 4.0) and later.
Note: If your activity enables Android Beam and is in the foreground, the standard intent dispatch system is disabled. However, if your activity also enables foreground dispatching, then it can still scan tags that match the intent filters set in the foreground dispatching.
To enable Android Beam:
Create an NdefMessage that contains the NdefRecords that you want to push onto the other device.
In general, you normally use setNdefPushMessage() if your Activity only needs to push the same NDEF message at all times, when two devices are in range to communicate. You use setNdefPushMessageCallback when your application cares about the current context of the application and wants to push an NDEF message depending on what the user is doing in your application.
The following sample shows how a simple activity calls NfcAdapter.CreateNdefMessageCallback in the onCreate() method of an activity (see AndroidBeamDemo for the complete sample). This example also has methods to help you create a MIME record:
package com.example.android.beam;import android.app.Activity;import android.content.Intent;import android.nfc.NdefMessage;import android.nfc.NdefRecord;import android.nfc.NfcAdapter;import android.nfc.NfcAdapter.CreateNdefMessageCallback;import android.nfc.NfcEvent;import android.os.Bundle;import android.os.Parcelable;import android.widget.TextView;import android.widget.Toast;import java.nio.charset.Charset;publicclassBeamextendsActivityimplementsCreateNdefMessageCallback{NfcAdapter mNfcAdapter;TextView textView;@Overridepublicvoid onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
setContentView(R.layout.main);TextView textView =(TextView) findViewById(R.id.textView);// Check for available NFC Adapter
mNfcAdapter =NfcAdapter.getDefaultAdapter(this);if(mNfcAdapter ==null){Toast.makeText(this,"NFC is not available",Toast.LENGTH_LONG).show();
finish();return;}// Register callback
mNfcAdapter.setNdefPushMessageCallback(this,this);}@OverridepublicNdefMessage createNdefMessage(NfcEventevent){String text =("Beam me up, Android!\n\n"+"Beam Time: "+System.currentTimeMillis());NdefMessage msg =newNdefMessage(newNdefRecord[]{ createMime("application/vnd.com.example.android.beam", text.getBytes())/**
* The Android Application Record (AAR) is commented out. When a device
* receives a push with an AAR in it, the application specified in the AAR
* is guaranteed to run. The AAR overrides the tag dispatch system.
* You can add it back in to guarantee that this
* activity starts when receiving a beamed message. For now, this code
* uses the tag dispatch system.
*///,NdefRecord.createApplicationRecord("com.example.android.beam")});return msg;}@Overridepublicvoid onResume(){super.onResume();// Check to see that the Activity started due to an Android Beamif(NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
processIntent(getIntent());}}@Overridepublicvoid onNewIntent(Intent intent){// onResume gets called after this to handle the intent
setIntent(intent);}/**
* Parses the NDEF Message from the intent and prints to the TextView
*/void processIntent(Intent intent){
textView =(TextView) findViewById(R.id.textView);Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);// only one message sent during the beamNdefMessage msg =(NdefMessage) rawMsgs[0];// record 0 contains the MIME type, record 1 is the AAR, if present
textView.setText(newString(msg.getRecords()[0].getPayload()));}}
Note that this code comments out an AAR, which you can remove. If you enable the AAR, the application specified in the AAR always receives the Android Beam message. If the application is not present, Google Play is started to download the application. Therefore, the following intent filter is not technically necessary for Android 4.0 devices or later if the AAR is used:
With this intent filter, the com.example.android.beam application now can be started when it scans an NFC tag or receives an Android Beam with an AAR of type com.example.android.beam, or when an NDEF formatted message contains a MIME record of type application/vnd.com.example.android.beam.
Even though AARs guarantee an application is started or downloaded, intent filters are recommended, because they let you start an Activity of your choice in your application instead of always starting the main Activity within the package specified by an AAR. AARs do not have Activity level granularity. Also, because some Android-powered devices do not support AARs, you should also embed identifying information in the first NDEF record of your NDEF messages and filter for that as well, just in case. See Creating Common Types of NDEF records for more information on how to create records.