• Operational Research Web Services
    • LIMOS 2012 : Webservices' Development
    • LOGO LIMOS

    •    Homepage   
    •    Svg2Png   
    •    Job shop   
    •    GPS for pedestrian   
    •    Job shop time lags   
    •    VRP   
    •    Community-based GPS    
    •    Bin packing    
    •    NFC android    
    •    Sudoku PPC    
  • Summary   |   Team (contact)   |   Download   |   Tutorial   |   Read a tag

    • Rule of confidentiality 
    • Policy:
               The application uses device sensors but not data is sent to one server. Data are saved only on the device.
    • Web services for the NFC application 
    • Summary:
               NFC (Near Field Communication) is a wireless technology which allows the transfer of data such as text or numbers between two NFC enabled devices.
               NFC tags, for example stickers or wristbands, contain small microchips which can store a small amount of information for transfer to another NFC device, such as a mobile phone.

               With the application NFCeditor, developed by two F3 students from ISIMA engineering school, you can easily read and write on a NFC tag.

               You can read and display informations about the tag :
                 - serial number (ex : 04:CF:FE:32:00:29:80)
                 - type of data (ex : text/plain)
                 - technologies available (ex : NfcA, MifareUltralight)
                 - size
                 - content if it's a NDEF tag

               You can write standard data :
                 - text
                 - link to website or application
    • Team's members 
    • photo de Matthieu
      Matthieu Basse

      Email : mabasse@poste.isima.fr
      ISIMA student
      Blaise Pascal University
      1 Rue de la Chebarde, 63178 Aubière CEDEX
    • photo de Marvin
      Marvin Volga

      Email : mavolga@poste.isima.fr
      ISIMA student
      Blaise Pascal University
      1 Rue de la Chebarde, 63178 Aubière CEDEX
     
    • Type of NFC cards  
    • This application was experimented on a lot of differents NFC tags. You can see them on the picture below and you can also buy some on RapidNFC s

      photo_NFC_tags


    • Download
    • Report:

    • Download PDF rapport (in french).

    • Slide:

    • Download PDF slides (in french).

    • Source application:

    • Download NFC code source.

    • APK application:

    • Download Application


    • Example : How to read a NFC TAG

    • Start Android Studio and create a new Project
      creation_project

      Make sure you select a minimum SDK version of level 10, because NFC is only supported after Android 2.3.3. Then select a Blank activity to begin your project.

      The following steps will help you implement the different functions required.

      Step 1
      In your file Manifest.xml you have to declare some permissions to be able to use the NFC technology.

                
      

      If you want your application to be allowed only for NFC devices on the Google Play Store, you can add this:
                
      

      Still in the Manifest, create an IntentFilter which will allow the application to start when a tag is attached.
               
      			
      			
              
      		
      		
      			
      			
      			
      			
      		
      
              
                  
              
      
              
      


      Step 2
      Create a XML file named nfc_tech_filter in order to specify the technologies you are interested in.

          <?xml version="1.0" encoding="utf-8"?>
      	
      		
      			android.nfc.tech.Ndef
      			android.nfc.tech.IsoDep
      			android.nfc.tech.NfcA
      			android.nfc.tech.NfcB
      			android.nfc.tech.NfcF
      			android.nfc.tech.NfcV
      		
      	
      



      Step 3
      Create a Layout named layout.activity_read to display informations about the tag. You must create as much TextView as information you want to display. For example, if you want to display a text with "Tag Content" and below the content of your tag, you must write:

      	
      		
              
             
      



      Step 4
      Implement the method onCreate of your ReadActivity. In this method you can check if the Android device support NFC and you can initialize your TextViews.

      	@Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_read);
      
              // Detect NFC on the phone
              mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
      
              if (mNfcAdapter == null) {
                  // Stop here, we definitely need NFC
                  Toast.makeText(this, "This device doesn't support NFC.", Toast.LENGTH_LONG).show();
                  finish();
                  return;
              }
      
              if (!mNfcAdapter.isEnabled()) {
                  Toast.makeText(this, "NFC is disabled.", Toast.LENGTH_LONG).show();
              }
              contentTextView = (TextView) findViewById(R.id.textView_content);
              handleIntent(getIntent());
      
          }
      


      Step 5
      Now you have to implement the method handleIntent which is called by the previous method. This method allows you to detect the tag thank to different filters for tags:ACTION_NDEF_DISCOVERED, ACTION_TECH_DISCOVERED, ACTION_TAG_DISCOVERED. You can find more information on the Android Developer web site.

              private void handleIntent(Intent intent) {
              String action = intent.getAction();
      
              if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {          
                  Ndef ndef = Ndef.get(tag);
                  size = ndef.getMaxSize();
                  if (MIME_TEXT_PLAIN.equals(type)) {
                      new NdefReaderTask().execute(tag);
                  } else {
                      Log.d(TAG, "Wrong mime type: " + type);
                  }          
              } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
                  // In case we would still use the Tech Discovered Intent
                  Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                  Ndef ndef = Ndef.get(tag);
                  size = ndef.getMaxSize();
                  String searchedTech = Ndef.class.getName();
                  for (String tech : techList) {
                      if (searchedTech.equals(tech)) {
                          new NdefReaderTask().execute(tag);
                          break;
                      }
                  }
              }
          }
      

      Step 6
      Implement the methods onResume and onNewIntent which allows to update data after the application is back in the foreground.

      	@Override
          protected void onResume() {
              super.onResume();
          }
      
          @Override
          protected void onNewIntent(Intent intent) {
              handleIntent(intent);
          }
      

      Step 7
      Finally, create the class NdefReaderTask inside the ReadActivity class. When the tag is scanned, if it contains data of type TEXT/PLAIN, it can be read thanks to this class in an other Thread than the UI Thread (or Main thread). The using of an other Thread is interesting when you need to carry an action that take a long time because it avoids the application to freeze by lack of resources. The method onPostExecute allows the tag's content to be displayed after the execution of the method doInBackground.

      		@Override
              protected String doInBackground(Tag... params) {
                  Tag tag = params[0];
                  Intent intent = getIntent();
                  Ndef ndef = Ndef.get(tag);
                  if (ndef == null) {
                      // NDEF is not supported by this Tag.
                      return null;
                  }
      
                  NdefMessage ndefMessage = ndef.getCachedNdefMessage();
      
                  NdefRecord[] records = ndefMessage.getRecords();
                  for (NdefRecord ndefRecord : records) {
                      if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_TEXT)) {
                          try {
                              return readText(ndefRecord);
                          } catch (UnsupportedEncodingException e) {
                              Log.e(TAG, "Unsupported Encoding", e);
                          }
                      } else if (ndefRecord.getTnf() == NdefRecord.TNF_WELL_KNOWN && Arrays.equals(ndefRecord.getType(), NdefRecord.RTD_URI)) {
                          try {
                              return readText(ndefRecord);
                          } catch (UnsupportedEncodingException e) {
                              Log.e(TAG, "Unsupported Encoding", e);
                          }
                      }
                  }
                      return null;
              }
      
              private String readText(NdefRecord record) throws UnsupportedEncodingException {
                  byte[] payload = record.getPayload();
                  // Get the Text Encoding
                  String textEncoding = ((payload[0] & 128) == 0) ? new String("UTF-8") : "UTF-16";
                  // Get the Language Code
                  int languageCodeLength = payload[0] & 0063;
                  return new String(payload, languageCodeLength + 1, payload.length - languageCodeLength - 1, textEncoding);
              }
      
              @Override
              protected void onPostExecute(String result) {
                  String techno = new String();
                  if (result != null) {
                      contentTextView.setText(result);              
                  } else {
                      contentTextView.setText("TAG vide");
                  }
              }
          }
      
    • Read a tag content with the NFCeditor app
    • With the same principle of reading explained above, we were able to display the content of the tag, the type of data, the technologies available on the tag, the storage size and the ID of the tag as shown on the picture above.
      photo_NFCeditor_read


    • Number of visitors : 3612
    |
      Last update : 15 december 2015