TWebNFC
Keep in mind
This component is currently only supported on Android. For compatiblity please visit the MDN docs.
TWebNFC
is a non-visual component to enable usage of NFC (Near Field Communication) from your browser. More specifically, you can read data via NFC as well as write data through NFC. The data must be in the NDEF record format. For more information you can refer to the Web NFC draft.
Supported tag types
- NFC Forum Type 1 (ISO/IEC 14443-3A (NFC-A))
- NFC Forum Type 2 (ISO/IEC 14443-3A (NFC-A))
- NFC Forum Type 3 (ISO/IEC 18092)
- NFC Forum Type 4 (ISO/IEC 14443-4 A/B (NFC A, NFC B))
- NFC Forum Type 5 (ISO/IEC 15693 (NFC-V))
- MIFARE Standard (ISO/IEC 14443-3A (NFC-A))
To see the full type specification please consult the Web NFC draft.
Read data
To read data, start by calling the Scan
method. After accepting the permission prompt, your page will scan for any NFC tags that are hold close to your NFC reader device. If an NFC tag is detected and read, it will trigger the OnReading
event. This event has an TJSNDEFMessageRecord
parameter, where TJSNDEFMessageRecord.jsmessage
gives you direct access to the underlying JavaScript object that contains the messages records.
For example, to read a text NDEF record, you can do the following:
procedure TForm1.DoHandleNFCReading(Sender: TObject; ASerialNumber: string;
AData: TJSNDEFMessageRecord);
var
s: string;
td: TJSTextDecoder;
begin
if (Length(AData.jsmessage.records) > 0) and (AData.jsmessage.records[0].recordType = 'text') then
begin
td := TJSTextDecoder.new;
WebMemo1.Text := td.decode(AData.jsmessage.records[0].data);
end
else
begin
ShowMessage('No text record found');
end;
end;
Write data
We support 3 basic types of records out of the box: empty
, text
and uri
. You can use WriteEmpty
to write an empty record, WriteString(AData: string)
to write a text record and WriteURL(AURL: string)
to write a URI record onto your tag.
You can also write other, custom record types by using WriteCustom
. Specify the record type as a string, the data as a string or binary and finally optionally you can also define the ID and mime-type of the data. For more information please consult the appropriate part of the Web NFC draft.
TForm2 = class(TWebForm)
//...
[async] procedure WebButton1Click(Sender: TObject);
//...
end;
implementation
procedure TForm1.WebButton1Click(Sender: TObject);
var
j: TJSONObject;
begin
try
TAwait.ExecP<JSValue>(nfc.WriteString('my data'))._then(function (aValue: JSValue): JSValue
begin
ShowMessage('Message successfully written!');
end).catch(function (aValue: JSValue): JSValue
begin
ShowMessage('Message could not be written');
end));
finally
j.Free;
end;
end;
Doing any writing without a Scan
call first might result in an immediate scan of the tag via the default NFC reader on your device. If you want to avoid that, you can always call Scan
first, so that the reading that happens after writing is trapped by your application.
Stop the scanning
Simply call the Stop
method to stop the scanning.
Methods for TWebNFC
Method | Description |
---|---|
Scan | Starts the scanning. |
Stop | Stops the scanning. |
WriteCustom(ARecordType: string; AData: string; AID: string = ''; AMediaType: string = ''): TJSPromise | Write a custom NDEF record to a tag. |
WriteCustom(ARecordType: string; AData: TJSUint8Array = nil; AID: string = ''; AMediaType: string = ''): TJSPromise | Write a custom NDEF record to a tag. |
WriteEmpty: TJSPromise | Write an empty NDEF record to a tag. |
WriteString(AData: string): TJSPromise | Write a text NDEF record to a tag. |
WriteURL(AURL: string): TJSPromise | Write a URI NDEF record to a tag. |
Events for TWebNFC
Event | Description |
---|---|
OnReading | Event triggered when a tag is read. |
OnReadingError | Event triggered when a tag could not be read (e.g. unsupported type) |