Skip to content

Connection & Lifecycle

Every example assumes a connected tag — see the connect-once model.

import { CrypTagEncoder } from 'cryptag';
const tag = new CrypTagEncoder();

Connecting

connect(readerName?)

Connect to an NFC reader and card, the only mandatory manual step. Pass a specific readerName (string) or omit it to use the first available reader.

const res = await tag.connect(); // or tag.connect('ACS ACR1252 1S CL Reader')
if (!res.success) throw new Error(res.error.message);

Returns

{
success: true,
data: { connected: true, reader: 'ACS ACR1252 1S CL Reader', protocol: 'T=1' },
duration: 22
}

disconnect()

End the session, close the transport, and reset internal state.

const res = await tag.disconnect();

Returns

{
success: true,
data: { connected: false, reader: null, protocol: null },
duration: 9
}

Readers

getReaders()

List available PC/SC readers and their card presence. Does not require a connected card.

const res = await tag.getReaders();

Returns

{
success: true,
data: {
readers: [
{ name: 'ACS ACR1252 1S CL Reader', hasCard: true, cardType: 'NTAG424' }
]
},
duration: 11
}

refreshReaders()

Re-scan readers and update card-presence status (detect inserted/removed cards).

const res = await tag.refreshReaders();

Returns

{
success: true,
data: {
readers: [
{ name: 'ACS ACR1252 1S CL Reader', hasCard: true, cardType: 'NTAG424' }
]
},
duration: 13
}

Session State

resetSession()

Reset session state (authentication + discovery cache). Call when a tag is removed or swapped so the next operation re-discovers and re-authenticates against the new tag. Returns nothing.

// after physically swapping the tag on the reader:
tag.resetSession(); // → undefined
const info = await tag.readTag(); // re-discovers the new tag

isConnected()

A PC/SC connection is established and a card is present. Returns a raw boolean.

tag.isConnected(); // → true

isDiscovered()

Tag discovery has completed and is cached. Returns a raw boolean.

tag.isDiscovered(); // → true

isAuthenticated()

An active authentication session exists. Returns a raw boolean.

tag.isAuthenticated(); // → false

getStatus()

Snapshot of all components, returned directly (not wrapped).

const status = tag.getStatus();

Returns

{
connected: true,
discovered: true,
authenticated: false,
authKey: null,
transport: {
initialized: true,
readerCount: 1,
selectedReader: 'ACS ACR1252 1S CL Reader',
connected: true,
protocol: 'T=1'
},
session: {
authenticated: false,
authMode: null,
keyNo: null,
commandCounter: 0,
encryptionCounter: 0,
uid: '04A1B2C3D4E580',
randomUID: false,
hasDiscovery: true
},
keyManager: {
uid: '04A1B2C3D4E580',
currentIsFactory: true,
targetIsFactory: false,
needsChange: true,
systemIdentifier: 'CRYPTAG',
applicationId: '3042F5'
}
}