NFC Library for Android

NFC Library for Android demo example
Delphi and C++ Builder NFC library for Android.
  • uses Android NFC API
  • provides reading, writing and formatting NFC tags
  • supports NDEF, NFC-A, NFC-V, MIFARE Classic and MIFARE Ultralight tags
  • available for Delphi/C++ Builder XE7 - 10.2
  • source code included in registered version
  • royalty free distribution in applications

Library API

function GetNfcAdapter: JNfcAdapter;
procedure ShowNfcSettings;

// foreground dispatch

type
  TOnNfcIntent = procedure(Intent: JIntent);

procedure RegisterNfc(OnNfcIntent: TOnNfcIntent); overload;
procedure EnableForegroundDispatch(NfcAdapter: JNfcAdapter);
procedure DisableForegroundDispatch(NfcAdapter: JNfcAdapter);

// reader mode

type
  TOnTagDiscovered = procedure(Tag: JTag);

  TReaderFlag = (reNfcA, reNfcB, reNfcF, reNfcV, reNfcBarcode, reNoNdefCheck, reNoSound);
  TReaderFlags = set of TReaderFlag;

procedure RegisterNfc(OnTagDiscovered: TOnTagDiscovered); overload;
procedure EnableReaderMode(NfcAdapter: JNfcAdapter; ReaderFlags: TReaderFlags = [reNfcA]);
procedure DisableReaderMode(NfcAdapter: JNfcAdapter);

// utilities

function BytesToHex(Bytes: TJavaArray; Separator: Char = ':'): string; overload;
function BytesToHex(const Bytes: TArray; Separator: Char = ':'): string; overload;
function BytesToHex(const Bytes: TArray; From, Count: Integer; Separator: Char = ':'): string; overload;

function BytesToHexReverse(Bytes: TJavaArray; Separator: Char = ':'): string; overload;
function BytesToHexReverse(const Bytes: TArray; Separator: Char = ':'): string; overload;
function BytesToHexReverse(const Bytes: TArray; From, Count: Integer; Separator: Char = ':'): string; overload;

// NFC tags

type
  TNfcTag = class
    constructor Create(Intent: JIntent);
    constructor Create(Tag: JTag);
    function Id(Separator: Char = ':'): string;

    property MifareClassic: TMifareClassic read;
    property MifareUltralight: TMifareUltralight read;
    property Ndef: TNdef read;
    property NdefFormatable: TNdefFormatable read;
    property NfcA: TNfcA read;
    property NfcV: TNfcV read;
    property Tag: JTag read;
    property TechList: TArray<string> read;
  end;

  TNfcA = class
    function Transceive(Data: TArray<Byte>): TArray<Byte>;

    property Atqa: TArray<Byte> read;
    property MaxTransceiveLength: Integer read;
    property NfcA: JNfcA read;
    property Sak: SmallInt read;
    property Tag: JTag read;
    property Timeout: Integer read write;
  end;

  TNfcV = class
    function EasAlarm: TArray<Byte>;
    function GetSecurity(FirstBlock, BlockCount: Byte): TArray<Byte>;
    function LockAfi: TArray<Byte>;
    function LockBlock(Block: Byte): TArray<Byte>;
    function LockDsfid: TArray<Byte>;
    function LockEas: TArray<Byte>;
    function ReadBlock(Block: Byte): TArray<Byte>;
    function ReadBlocks(FirstBlock, BlockCount: Byte): TArray<Byte>;
    function ResetEas: TArray<Byte>;
    function ResetToReady: TArray<Byte>;
    function SetEas: TArray<Byte>;
    function Transceive(Data: TArray<Byte>): TArray<Byte>;
    function WriteAfi(AFI: Byte): TArray<Byte>;
    function WriteBlock(Block: Byte; Data: TArray<Byte>): TArray<Byte>;
    function WriteBlocks(FirstBlock, BlockCount: Byte; Data: TArray<Byte>): TArray<Byte>;
    function WriteDsfid(DSFID: Byte): TArray<Byte>;

    property BlockCount: Integer read;
    property BlockSize: Integer read;
    property DsfId: Byte read;
    property Ic: Integer read;
    property Inventory: TArray<Byte> read;
    property MaxTransceiveLength: Integer read;
    property NfcV: JNfcV read;
    property ResponseFlags: Byte read;
    property SystemInformation: TArray<Byte> read;
    property Tag: JTag read;
  end;

  TNdef = class
    function MakeReadOnly: Boolean;
    function Write(NdefRecord: TNdefRecord): Boolean;
    function Write(NdefRecords: TArray<TNdefRecord>): Boolean;
    function Write(NdefRecords: array of TNdefRecord): Boolean;

    property CanMakeReadOnly: Boolean read;
    property IsWritable: Boolean read;
    property MaxSize: Integer read;
    property Ndef: JNdef read;
    property Records: TArray<TNdefRecord> read;
    property Tag: JTag read;
    property &Type: string read;
  end;

  TNdefFormatable = class
    function Format: Boolean;
    function FormatReadOnly: Boolean;

    property NdefFormatable: JNdefFormatable read;
    property Tag: JTag read;
  end;

  TMifareUltralight = class
    function ReadData: TArray<Byte>;
    function ReadPage(PageOffset: Integer): TArray<Byte>;
    function ReadPages: TArray<TArray<Byte>>;
    procedure WritePage(PageOffset: Integer; Data: TArray<Byte>);
    procedure WritePages(FirstPageOffset: Integer; Data: TArray<TArray<Byte>>);

    property MaxTransceiveLength: Integer read;
    property MifareUltralight: JMifareUltralight read;
    property PageCount: Integer read;
    property Tag: JTag read;
    property Timeout: Integer read write;
    property &Type: Integer read;
    property TypeDescription: string read;
  end;

  TMifareClassic = class
    property BlockCount: Integer read;
    property BlockSize: Integer read;
    property MifareClassic: JMifareClassic read;
    property MaxTransceiveLength: Integer read;
    property SectorCount: Integer read;
    property Size: Integer read;
    property Tag: JTag read;
    property Timeout: Integer read write;
    property &Type: Integer read;
    property TypeDescription: string read;

    function ReadBlock(Index: Integer): TArray<Byte>;
    function ReadBlocks: TArray<TArray<Byte>>;
    function ReadData: TArray<Byte>;
  end;

  TNdefRecord = class
    class function CreateExternalRecord(const Domain, &Type: string; const Data: TArray<Byte>): TNdefRecord;
    class function CreateApplicationRecord(const PackageName: string): TNdefRecord;
    class function CreateMimeRecord(const MimeType: string; const MimeData: TArray<Byte>): TNdefRecord;
    class function CreateTextRecord(const Text: string; const LanguageCode: string = 'en'): TNdefRecord;
    class function CreateUriRecord(const Uri: string): TNdefRecord;

    property ExternalText: string read;
    property IsAbsoluteUri: Boolean read;
    property IsEmpty: Boolean read;
    property IsExternalType: Boolean read;
    property IsMimeMedia: Boolean read;
    property IsText: Boolean read;
    property IsUnchanged: Boolean read;
    property IsUnknown: Boolean read;
    property IsUri: Boolean read;
    property IsWellKnown: Boolean read;
    property NdefRecord: JNdefRecord read;
    property Payload: TArray<Byte> read;
    property PayloadUtf8: string read;
    property Text: string read;
    property TextLanguageCode: string read;
    property TnfDescription: string read;
    property Uri: string read;
  end;
								

Download and order

Related links