In this example, refer to the NFC ForegoundDispatch in ApiDemos to introduce the basic steps for compiling Android NFC, because only MifareClassic tags are available at hand, you need to modify the ForegoundDispatch code to detect NFC tags of the MifareClassic type. The basic steps for reading and writing NFC tags of other types are the same.
1. Declare NFC-related permissions and function options in the Android manifest file:
Permission statement:
<Uses-permission android: name = "android. permission. NFC"/>
The minimum version is required. NFC is supported only when Android2.3 (Level 10) is started. Therefore, the minimum version must be set to 10.
<Uses-sdk android: minSdkVersion = "10"/>
If you want to publish the product on the Android Market, you must specify that the mobile phone supports NFC.
<Uses-feature android: name = "android. hardware. nfc" android: required = "true"/>
Declare for Activity that it supports processing NFC tags
For example, the declaration of the example Activity in Manifest is as follows:
<Activity android: name = ". NFCDemoActivity"
Android: label = "@ string/app_name"
Android: launchMode = "singleTop">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
<Intent-filter>
<Action android: name = "android. nfc. action. NDEF_DISCOVERED"/>
<Data android: mimeType = "text/plain"/>
</Intent-filter>
<Intent-filter>
<Action
Android: name = "android. nfc. action. TAG_DISCOVERED"
>
</Action>
<Category
Android: name = "android. intent. category. DEFAULT"
>
</Category>
</Intent-filter>
<! -Add a technology filter->
<Intent-filter>
<Action android: name = "android. nfc. action. TECH_DISCOVERED"/>
</Intent-filter>
<Meta-data android: name = "android. nfc. action. TECH_DISCOVERED"
Android: resource = "@ xml/filter_nfc"
/>
</Activity>
The sequence specified by Activity NDEF_DISCOVERED, TECH_DISCOVERED, and TAG_DISCOVERED is very important. When an Android device detects an NFC Tag approaching, the Intent containing NFC messages will be sent to the corresponding Activity according to the Order stated by the Action.
2. Android NFC message sending mechanism
When an Android device detects an NFC Tag, the ideal action is to trigger the most appropriate Activity to process the detected Tag, this is because NFC usually works at a very close distance (<4 m). If you need to select an appropriate application to process tags, it is easy to disconnect the communication with tags. Therefore, you need to select the appropriate Intent filter to only process the Tag type you want to read and write.
Android supports two NFC message sending mechanisms: Intent and foreground Activity.
When the system detects a Tag, the Android system provides the Intent filter defined in manifest to select an appropriate Activity to process the corresponding Tag, when multiple activities can process the corresponding Tag type, the Activity selection window is displayed and selected by the user:
The front-end Activity message sending mechanism allows an Activity running on the front-end to read and write NFC tags with priority. If Android detects an NFC Tag, if the foreground Activity can process tags of this type, the Activity has priority, without the Activity selection window.
The two methods basically use Intent-filter to specify the Tag type that the Activity can process. One is to use Android Manifest, and the other is to declare it through code.
Display the priority of message sending when Android detects the Tag:
In this example, NFCDemoActivity supports two NFC message sending mechanisms. The XML above specifies the Intent message sending mechanism.
<Meta-data android: name = "android. nfc. action. TECH_DISCOVERED"
Android: resource = "@ xml/filter_nfc"
/>
Filter_nfc indicates the NFC Tag type that supports processing. filter_nfc.xml is defined as follows:
<Resources xmlns: xliff = "urn: oasis: names: tc: xliff: document: 1.2">
<! -Capture anything using NfcF->
<Tech-list>
<Tech> android. nfc. tech. NfcA </tech>
<Tech> android. nfc. tech. MifareClassic </tech>
<Tech> android. nfc. tech. MifareUltralight </tech>
</Tech-list>
</Resources>
Because I only have tags OF THE MifareClassic type, only the Tag types related to MifareClassic are defined. If you can handle all NFC types supported by Android, you can define them as follows:
[Html]
<Resources xmlns: xliff = "urn: oasis: names: tc: xliff: document: 1.2">
<Tech-list>
<Tech> android. nfc. tech. IsoDep </tech>
<Tech> android. nfc. tech. NfcA </tech>
<Tech> android. nfc. tech. NfcB </tech>
<Tech> android. nfc. tech. NfcF </tech>
<Tech> android. nfc. tech. NfcV </tech>
<Tech> android. nfc. tech. Ndef </tech>
<Tech> android. nfc. tech. NdefFormatable </tech>
<Tech> android. nfc. tech. MifareClassic </tech>
<Tech> android. nfc. tech. MifareUltralight </tech>
</Tech-list>
</Resources>
<Resources xmlns: xliff = "urn: oasis: names: tc: xliff: document: 1.2">
<Tech-list>
<Tech> android. nfc. tech. IsoDep </tech>
<Tech> android. nfc. tech. NfcA </tech>
<Tech> android. nfc. tech. NfcB </tech>
<Tech> android. nfc. tech. NfcF </tech>
<Tech> android. nfc. tech. NfcV </tech>
<Tech> android. nfc. tech. Ndef </tech>
<Tech> android. nfc. tech. NdefFormatable </tech>
<Tech> android. nfc. tech. MifareClassic </tech>
<Tech> android. nfc. tech. MifareUltralight </tech>
</Tech-list>
</Resources>
With the statement in this Manifest, when Android detects a Tag, the Activity selection window is displayed, as shown in Reading Example.
When NFCDemoActiviy is running on the foreground, we hope that only the NFCDemoActiviy can process tags of the Mifare type. In this case, we can use the foreground message sending mechanism. The following code is similar to the NFC example in ApiDemos:
[Java]
Public class NFCDemoActivity extends Activity {
Private NfcAdapter mAdapter;
Private PendingIntent mPendingIntent;
Private IntentFilter [] mFilters;
Private String [] [] mTechLists;
Private TextView mText;
Private int mCount = 0;
@ Override
Public void onCreate (Bundle savedState ){
Super. onCreate (savedState );
SetContentView (R. layout. foreground_dispatch );
MText = (TextView) findViewById (R. id. text );
MText. setText ("Scan a tag ");
MAdapter = NfcAdapter. getdefaadapter adapter (this );
// Create a generic PendingIntent that will be deliver
// To this activity. The NFC stack
// Will fill in the intent with the details of
// Discovered tag before delivering
// This activity.
MPendingIntent = PendingIntent. getActivity (this, 0,
New Intent (this,
GetClass (). addFlags (Intent. FLAG_ACTIVITY_SINGLE_TOP), 0 );
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef
= New IntentFilter (NfcAdapter. ACTION_TECH_DISCOVERED );
Try {
Ndef. addDataType ("*/*");
} Catch (MalformedMimeTypeException e ){
Throw new RuntimeException ("fail", e );
}
MFilters = new IntentFilter [] {
Ndef,
};
// Setup a tech list for all MifareClassic tags
MTechLists
= New String [] [] {new String [] {MifareClassic. class. getName ()}};
}
@ Override
Public void onResume (){
Super. onResume ();
MAdapter. enableForegroundDispatch (this,
MPendingIntent, mFilters, mTechLists );
}
@ Override
Public void onNewIntent (Intent intent ){
Log. I ("Foreground dispatch ",
"Discovered tag with intent:" + intent );
MText. setText ("Discovered tag" +
++ MCount + "with intent:" + intent );
}
@ Override
Public void onPause (){
Super. onPause ();
MAdapter. disableForegroundDispatch (this );
}
}
Public class NFCDemoActivity extends Activity {
Private NfcAdapter mAdapter;
Private PendingIntent mPendingIntent;
Private IntentFilter [] mFilters;
Private String [] [] mTechLists;
Private TextView mText;
Private int mCount = 0;
@ Override
Public void onCreate (Bundle savedState ){
Super. onCreate (savedState );
SetContentView (R. layout. foreground_dispatch );
MText = (TextView) findViewById (R. id. text );
MText. setText ("Scan a tag ");
MAdapter = NfcAdapter. getdefaadapter adapter (this );
// Create a generic PendingIntent that will be deliver
// To this activity. The NFC stack
// Will fill in the intent with the details of
// Discovered tag before delivering
// This activity.
MPendingIntent = PendingIntent. getActivity (this, 0,
New Intent (this,
GetClass (). addFlags (Intent. FLAG_ACTIVITY_SINGLE_TOP), 0 );
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef
= New IntentFilter (NfcAdapter. ACTION_TECH_DISCOVERED );
Try {
Ndef. addDataType ("*/*");
} Catch (MalformedMimeTypeException e ){
Throw new RuntimeException ("fail", e );
}
MFilters = new IntentFilter [] {
Ndef,
};
// Setup a tech list for all MifareClassic tags
MTechLists
= New String [] [] {new String [] {MifareClassic. class. getName ()}};
}
@ Override
Public void onResume (){
Super. onResume ();
MAdapter. enableForegroundDispatch (this,
MPendingIntent, mFilters, mTechLists );
}
@ Override
Public void onNewIntent (Intent intent ){
Log. I ("Foreground dispatch ",
"Discovered tag with intent:" + intent );
MText. setText ("Discovered tag" +
++ MCount + "with intent:" + intent );
}
@ Override
Public void onPause (){
Super. onPause ();
MAdapter. disableForegroundDispatch (this );
}
}
Only one row is changed. Change the Tag of the NfcF type to the NFC Tag of the MifareClassic type.
MTechLists = new String [] [] {new String [] {MifareClassic. class. getName ()}};
Run this example. Every time the Tag is near, the count is increased by 1.
Download this example: http://www.bkjia.com/uploadfile/2012/0517/20120517093609938.zip
Excerpted from the mobile app