Samsung Electronics logo

Detecting NFC Tags and Peer Devices



Learning how to detect NFC tags and peer devices is a basic NFC management skill:

  1. To get the default NFC adapter, use the getDefaultAdapter() method:

     var nfcAdapter = webapis.nfc.getDefaultAdapter();
  2. Define the event handlers for NFC tag detection using the NFCTagDetectCallback listener interface:

    setTagDetect() 
    {
       /* When an NFC tag is detected */
       void onattach(nfcTag) 
       {
          console.log("NFC Tag detected. Its type is: " + nfcTag.type);
       }
      
       /* When an NFC tag becomes unavailable */
       void ondetach()
       {
          console.log("NFC Tag unavailable");
       }
    }
  3. Register the listener to use the defined event handlers.

    You can limit the listener to detect only specific NFC tag types by defining the tag types as the second parameter of the setTagListener() method. In the following example, only MIFARE tags are detected.

    /* Defines the tag types to be detected */
    var tagFilter = ["MIFARE_MINI", "MIFARE_1K", "MIFARE_4K", "MIFARE_ULTRA", "MIFARE_DESFIRE"];
    
    /* Registers the event listener */
    nfcAdapter.setTagListener(setTagDetect, tagFilter);
  4. To stop the tag detection, use the unsetTagListener() method:

    nfcAdapter.unsetTagListener();

NFC peers are detected similarly as NFC tags, except that the setPeerListener() method is used to register the NFCPeerDetectCallback listener interface, and the unsetPeerListener() method is used to stop the peer detection.