nfcDiscoverDevicesStart()

The function nfcDiscoverDevicesStart triggers a callback function when an NFC (Nearfield communication) device is being discovered.

Synopsis

int nfcDiscoverDevicesStart(string|function_ptr callback);

Parameters

Parameter Meaning
callback

The callback function that is called when the registration of the callback was successful and the NFC device works.

The callback must have the signature:

callback(mapping data)

The callback mapping argument has the following keys (all keys are strings):

reason (string) ... "device" = new device detected, "lost" = device no longer in range

device (mapping) ... mapping with details about the discovered device with the following keys:

type (string) ... Type of the device. Can be a ProprietaryTag, NfcTagType1, NfcTagType2, NfcTagType3, NfcTagType4, MifareTag

uid (string) ... The UID in Hex

url (string) ... The URL of the device. can be empty

messages (list of dyn_mapping) ... only available if reason == "device". A list of NDEF (NFC Data Exchange Format) messages received. Each message is a list of records (each record is a mapping).

NOTE: CTRL has no dyn_dyn_mapping but you can use ,e.g., dyn_anytype or vector<void> to store the list of messages, e.g. vector<void> messages = data["messages"];

Each record in a message is a mapping and has the following keys (all keys are strings):

type (string) ... The NDEF record type

id (string) .. The optional record ID. It can be empty.

payload (string) ... payload data.

Return value

The function nfcDiscoverDevicesStart returns 0 in case of correct callback registration and a functioning NFC device.

It returns -1 in case of

  • no argument
  • illegal callback function
  • nfcDiscoverDevicesStart() was already called in the same script (only one callback function can be registered per script)
  • NFC is not supported on the current device (device has no NFC hardware).

Description

When a device is being discovered (a mobile phone is close to an NFC device), the callback will be triggered. For the description of the callback function, see the "callback" parameter description above.

NFC is only available for mobile UI on Android and for UI on Linux. NFC is not available for Windows.

On Android, Qt NFC only works in foreground applications.

Example

#uses "CtrlMobile"
main(mapping event)
{
  if ( this.toggleState )
    DebugN("start", nfcDiscoverDevicesStart("cb"));
  else
    DebugN("stop", nfcDiscoverDevicesStop());
}
cb(mapping device)
{
  DebugN(device);
  if ( device["reason"] == "lost" )
    return;
  mapping dev = device["device"];
  TEXT_EDIT1.append("******************************");
  TEXT_EDIT1.append("uid: "  + dev.value("uid", ""));
  TEXT_EDIT1.append("type: " + dev.value("type", ""));
  TEXT_EDIT1.append("url: "  + dev.value("url", ""));
  if ( !dev.contains("messages") )
    return;
  vector<void> messages = dev["messages"];
  DebugN("messages", messages);
  for (int i = 0; i < messages.count(); i++)
  {
    dyn_mapping message = messages.at(i);
    for (int j = 0; j < message.count(); j++)
    {
      TEXT_EDIT1.append("-------------------------------");
      mapping record = message.at(j);
      for (int k = 0; k < record.count(); k++)
        TEXT_EDIT1.append(record.keyAt(k) + ": " + record.valueAt(k));
    }
  }
}

Assignment

Communication functions

Availability

UI