Class SCard
- All Implemented Interfaces:
AutoCloseable
Communicate directly with smart card readers and cards through PC/SC.
Chilkat.SCard is the low-level PC/SC smart card interface for
applications that need direct reader and card communication. It supports
reader discovery, reader connection, card status checking, APDU transmission,
reader-control commands, status-change monitoring, transactions, and
diagnostic access to underlying PC/SC errors. Use this class when the
application needs APDU-level control rather than a higher-level certificate,
minidriver, or PKCS#11 abstraction.
Reader discovery
Enumerate available PC/SC smart card readers and identify the reader the application should use.
Connect to cards
Connect to a reader/card, check card presence and status, and manage the active PC/SC connection.
Transmit APDUs
Send APDU commands directly to the card and receive the raw response bytes and status words.
Reader-control commands
Issue lower-level reader control commands when supported by the reader driver and PC/SC environment.
Status monitoring
Monitor reader or card status changes so applications can detect card insertion, removal, or state changes.
Transactions and diagnostics
Use PC/SC transactions for coordinated card access and inspect detailed error information when reader or card operations fail.
Common pattern:
Establish the PC/SC context, list readers, connect to the selected reader,
check card status, transmit APDU commands, process the response bytes, and
disconnect when finished. Use Chilkat.SCard for direct PC/SC
reader and APDU-level work; use Chilkat.ScMinidriver for
card-minidriver certificate and key-container operations, and
Chilkat.Pkcs11 for PKCS#11 token or HSM sessions.
For an extended overview, see SCard Class Overview.
</section>
Event-capable: long-running methods report progress through a ProgressListener installed
with setProgressListener(ProgressListener), onAbortCheck(BooleanSupplier),
onPercentDone(IntPredicate) or onProgressInfo(BiConsumer).
Instances hold a native Chilkat object: close them with ChilkatObject.close() (try-with-resources) when
done; an unclosed instance is freed when it becomes unreachable.
- See Also:
- Online reference
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidEstablishes a temporary exclusive access mode for doing a series of commands in a transaction.booleanCheck the current status of the currently connected reader.booleanEstablish a connection to a reader.voiddisconnect(String disposition)Terminates a connection with a reader.voidendTransaction(String disposition)Ends a previously begun transaction.voidestablishContext(String scope)Creates an Application Context to the PC/SC Resource Manager.voidfindSmartcards(JsonObject json)Returns JSON containing information about the smartcards currently inserted into readers.The name of the active protocol if connected smart card reader, or an empty string if not connected.voidGet an attribute from the IFD Handler (reader driver).getAttribStr(String attr)Get a string typed attribute from the IFD Handler (reader driver).longgetAttribUint(String attr)Get an unsigned integer typed attribute from the IFD Handler (reader driver).This is the Current ATR of a card in the connected reader.The name of the currently connected smart card reader, or an empty string if not connected.Contains the stringuserorsystemif this object has established a context (by calling EstablishContext).For Linux systems only.Returns the installed listener, ornull.The current status of the connected reader.The last error returned by an underlying PC/SC function.voidgetStatusChange(int maxWaitMs, StringTable stReaderNames, JsonObject json)Blocks execution until the current availability of the cards in a specific set of readers changes.voidCancels an ongoing GetStatusChange method call.voidlistReaderGroups(StringTable readerGroups)Returns a list of currently available reader groups on the system.voidlistReaders(StringTable st)Returns a list of currently available readers on the system.voidonAbortCheck(BooleanSupplier callback)Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it.voidonPercentDone(IntPredicate callback)Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it.voidonProgressInfo(BiConsumer<String,String> callback)Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds.voidReestablishes a connection to a reader that was previously connected to using Connect().voidDestroys a communication context to the PC/SC Resource Manager.voidsendControl(long controlCode, BinData bdSend, BinData bdRecv)Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.voidsendControlHex(long controlCode, String sendData, BinData bdRecv)Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.voidsetPcscLibPath(String value)Sets thePcscLibPathproperty; seegetPcscLibPath().voidsetProgressListener(ProgressListener listener)Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.voidSends an APDU to the smart card contained in the currently connected reader.voidtransmitHex(String protocolName, String apduHex, BinData bdRecv, int maxRecvLen)Sends an APDU to the smart card contained in the currently connected reader.Methods inherited from class com.chilkatsoft.chilkat2.ChilkatObject
close, getDebugLogFilePath, getLastErrorHtml, getLastErrorText, getLastErrorXml, getLastMethodSuccess, getVerboseLogging, getVersion, isClosed, setDebugLogFilePath, setLastMethodSuccess, setVerboseLogging, toString
-
Constructor Details
-
SCard
public SCard()Creates a newSCardobject.
-
-
Method Details
-
getActiveProtocol
The name of the active protocol if connected smart card reader, or an empty string if not connected. Possible values areT0,T1,raw,undefined, orif not connected to a reader.- Returns:
- the value of the
ActiveProtocolproperty (read-only)
-
getCardAtr
This is the Current ATR of a card in the connected reader.- Returns:
- the value of the
CardAtrproperty (read-only)
-
getConnectedReader
The name of the currently connected smart card reader, or an empty string if not connected.- Returns:
- the value of the
ConnectedReaderproperty (read-only)
-
getContext
Contains the stringuserorsystemif this object has established a context (by calling EstablishContext). Contains the empty string if no context is established.- Returns:
- the value of the
Contextproperty (read-only)
-
getPcscLibPath
For Linux systems only. Specifies the full path of the libpcsclite.so shared lib. This property should only be used if the libpcsclite.so is in a non-standard location or if Chilkat cannot automatically located it.- Returns:
- the value of the
PcscLibPathproperty
-
setPcscLibPath
Sets thePcscLibPathproperty; seegetPcscLibPath().- Parameters:
value- the new value (notnull)
-
getReaderStatus
The current status of the connected reader. Possible values are:absent- There is no card in the reader.present- There is a card in the reader, but it has not been moved into position for use.swallowed- There is a card in the reader in position for use. The card is not powered.powered- Power is being provided to the card, but the reader driver is unaware of the mode of the card.negotiable- The card has been reset and is awaiting PTS negotiation.specific- The card has been reset and specific communication protocols have been established.
- Returns:
- the value of the
ReaderStatusproperty (read-only)
-
getScardError
The last error returned by an underlying PC/SC function. Can be one of the following:SCARD_W_REMOVED_CARD- The smart card has been removed, so that further communication is not possible.SCARD_W_RESET_CARD- The smart card has been reset, so any shared state information is invalid. ...
- Returns:
- the value of the
ScardErrorproperty (read-only)
-
beginTransaction
public void beginTransaction()Establishes a temporary exclusive access mode for doing a series of commands in a transaction.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
checkStatus
public boolean checkStatus()Check the current status of the currently connected reader. Calling this method updates the ReaderStatus, ActiveProtocol, and CardAtr properties. If this method returnsfalse, none of the properties are updated.- Returns:
- the result; this method never throws
ChilkatException
-
connect
Establish a connection to a reader. Thereaderis the name of a reader returned from ListReaders. TheshareModecan beshared,exclusive, ordirect. ThepreferredProtocolcan be0(valid only if the shareMode =direct),T0,T1,raw, orno_preference. (No preference is effectively T0 or T1.)If successful, the state of this object instance is that it's connected to the reader.
- Parameters:
reader- thereaderargument (notnull)shareMode- theshareModeargument (notnull)preferredProtocol- thepreferredProtocolargument (notnull)- Returns:
- the result; this method never throws
ChilkatException
-
disconnect
Terminates a connection with a reader. Thedispositioncan be one of the following values:leave: Do nothing.reset: Reset the card (warm reset).unpower: Power down the card (cold reset).eject: Eject the card.
- Parameters:
disposition- thedispositionargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
endTransaction
Ends a previously begun transaction. Thedispositionis the action to be taken on the reader, and can beleavewhich is to do nothing,reset,unpower, oreject.- Parameters:
disposition- thedispositionargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
establishContext
Creates an Application Context to the PC/SC Resource Manager. This must be the first WinSCard function called in a PC/SC application. Thescopecan beuserorsystem. After calling, this object will have context and all other methods will use the established context. TheContextproperty will hold the valueuserorsystemif context was established, or will be empty if no context was established.- Parameters:
scope- thescopeargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
findSmartcards
Returns JSON containing information about the smartcards currently inserted into readers.- Parameters:
json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAttrib
Get an attribute from the IFD Handler (reader driver).The
attrcan be one of the following:ASYNC_PROTOCOL_TYPESATR_STRINGCHANNEL_IDCHARACTERISTICSCURRENT_BWTCURRENT_CLKCURRENT_CWTCURRENT_DCURRENT_EBC_ENCODINGCURRENT_FCURRENT_IFSCCURRENT_IFSDCURRENT_IO_STATECURRENT_NCURRENT_PROTOCOL_TYPECURRENT_WDEFAULT_CLKDEFAULT_DATA_RATEDEVICE_FRIENDLY_NAMEDEVICE_IN_USEDEVICE_SYSTEM_NAMEDEVICE_UNITESC_AUTHREQUESTESC_CANCELESC_RESETEXTENDED_BWTICC_INTERFACE_STATUSICC_PRESENCEICC_TYPE_PER_ATRMAX_CLKMAX_DATA_RATEMAX_IFSDMAXINPUTPOWER_MGMT_SUPPORTSUPRESS_T1_IFS_REQUESTSYNC_PROTOCOL_TYPESUSER_AUTH_INPUT_DEVICEUSER_TO_CARD_AUTH_DEVICEVENDOR_IFD_SERIAL_NOVENDOR_IFD_TYPEVENDOR_IFD_VERSIONVENDOR_NAME
The attribute data is returned in
bd.- Parameters:
attr- theattrargument (notnull)bd- thebdargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAttribStr
Get a string typed attribute from the IFD Handler (reader driver).The
attrcan be one of the following, but should be limited to the particular attributes that return string values.ASYNC_PROTOCOL_TYPESATR_STRINGCHANNEL_IDCHARACTERISTICSCURRENT_BWTCURRENT_CLKCURRENT_CWTCURRENT_DCURRENT_EBC_ENCODINGCURRENT_FCURRENT_IFSCCURRENT_IFSDCURRENT_IO_STATECURRENT_NCURRENT_PROTOCOL_TYPECURRENT_WDEFAULT_CLKDEFAULT_DATA_RATEDEVICE_FRIENDLY_NAMEDEVICE_IN_USEDEVICE_SYSTEM_NAMEDEVICE_UNITESC_AUTHREQUESTESC_CANCELESC_RESETEXTENDED_BWTICC_INTERFACE_STATUSICC_PRESENCEICC_TYPE_PER_ATRMAX_CLKMAX_DATA_RATEMAX_IFSDMAXINPUTPOWER_MGMT_SUPPORTSUPRESS_T1_IFS_REQUESTSYNC_PROTOCOL_TYPESUSER_AUTH_INPUT_DEVICEUSER_TO_CARD_AUTH_DEVICEVENDOR_IFD_SERIAL_NOVENDOR_IFD_TYPEVENDOR_IFD_VERSIONVENDOR_NAME
- Parameters:
attr- theattrargument (notnull)- Returns:
- the result (never
null) - Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getAttribUint
Get an unsigned integer typed attribute from the IFD Handler (reader driver).The
attrcan be one of the following, but should be limited to the particular attributes that return unsigned integer values.ASYNC_PROTOCOL_TYPESATR_STRINGCHANNEL_IDCHARACTERISTICSCURRENT_BWTCURRENT_CLKCURRENT_CWTCURRENT_DCURRENT_EBC_ENCODINGCURRENT_FCURRENT_IFSCCURRENT_IFSDCURRENT_IO_STATECURRENT_NCURRENT_PROTOCOL_TYPECURRENT_WDEFAULT_CLKDEFAULT_DATA_RATEDEVICE_FRIENDLY_NAMEDEVICE_IN_USEDEVICE_SYSTEM_NAMEDEVICE_UNITESC_AUTHREQUESTESC_CANCELESC_RESETEXTENDED_BWTICC_INTERFACE_STATUSICC_PRESENCEICC_TYPE_PER_ATRMAX_CLKMAX_DATA_RATEMAX_IFSDMAXINPUTPOWER_MGMT_SUPPORTSUPRESS_T1_IFS_REQUESTSYNC_PROTOCOL_TYPESUSER_AUTH_INPUT_DEVICEUSER_TO_CARD_AUTH_DEVICEVENDOR_IFD_SERIAL_NOVENDOR_IFD_TYPEVENDOR_IFD_VERSIONVENDOR_NAME
Returns 0xFFFFFFFF on failure.
- Parameters:
attr- theattrargument (notnull)- Returns:
- the result (see
ChilkatObject.getLastMethodSuccess()where the description mentions failure)
-
getStatusChange
Blocks execution until the current availability of the cards in a specific set of readers changes.This function receives a list of reader names in
stReaderNames. It then blocks waiting for a change in state to occur for a maximum blocking time ofmaxWaitMs(in milliseconds) or forever if 0 is used.Information about the current reader states and which reader(s) changed is returned in
json. See the example below for more information.To wait for a reader event (reader added or removed) you may use the special reader name
\\?PnP?\Notification.To cancel the ongoing call, call Cancel().
The
stReaderNamescontains the reader names to check. Thejsonis empty on input, and if the call returns success contains information about the state (after the event change) of each reader.- Parameters:
maxWaitMs- themaxWaitMsargumentstReaderNames- thestReaderNamesargument (notnull)json- thejsonargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
getStatusChangeCancel
public void getStatusChangeCancel()Cancels an ongoing GetStatusChange method call. This would be called from a separate thread in your application if GetStatusChange was called synchronously.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
listReaderGroups
Returns a list of currently available reader groups on the system. The reader groups are returned inreaderGroups.- Parameters:
readerGroups- thereaderGroupsargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
listReaders
Returns a list of currently available readers on the system.- Parameters:
st- thestargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
reconnect
Reestablishes a connection to a reader that was previously connected to using Connect().In a multi application environment it is possible for an application to reset the card in shared mode. When this occurs any other application trying to access certain commands will be returned the value SCARD_W_RESET_CARD. When this occurs Reconnect() must be called in order to acknowledge that the card was reset and allow it to change its state accordingly.
The
shareModecan beshared,exclusive, ordirect. ThepreferredProtocolcan be0(valid only if the shareMode =direct),T0,T1,raw, orno_preference. (No preference is effectively T0 or T1.) Theactionis the desired action taken on the card/reader. It can beleave,reset,unpower, oreject.If successful, the state of this object instance is that it's connected to the reader.
- Parameters:
shareMode- theshareModeargument (notnull)preferredProtocol- thepreferredProtocolargument (notnull)action- theactionargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
releaseContext
public void releaseContext()Destroys a communication context to the PC/SC Resource Manager. This must be the last function called in a PC/SC application.- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
sendControl
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.This is useful for creating client side reader drivers for functions like PIN pads, biometrics, or other extensions to the normal smart card reader that are not normally handled by PC/SC.
The command data is sent in
bdSend. The response is written tobdRecv.- Parameters:
controlCode- thecontrolCodeargument (0 .. 4294967295)bdSend- thebdSendargument (notnull)bdRecv- thebdRecvargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
sendControlHex
Sends a command directly to the IFD Handler (reader driver) to be processed by the reader.This is useful for creating client side reader drivers for functions like PIN pads, biometrics, or other extensions to the normal smart card reader that are not normally handled by PC/SC.
The command data is provided as a hex string in
sendData. The response is written tobdRecv.- Parameters:
controlCode- thecontrolCodeargument (0 .. 4294967295)sendData- thesendDataargument (notnull)bdRecv- thebdRecvargument (notnull)- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
transmit
Sends an APDU to the smart card contained in the currently connected reader. TheprotocolNamecan beT0,T1, orraw. The APDU to be sent is contained inbdSend. The response from the card is contained inbdRecv. ThemaxRecvLenis the maximum response size (in bytes) willing to be accepted.- Parameters:
protocolName- theprotocolNameargument (notnull)bdSend- thebdSendargument (notnull)bdRecv- thebdRecvargument (notnull)maxRecvLen- themaxRecvLenargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
transmitHex
Sends an APDU to the smart card contained in the currently connected reader. TheprotocolNamecan beT0,T1, orraw. The APDU (in hexadecimal) to be sent is passed inapduHex. The response from the card is contained inbdRecv. ThemaxRecvLenis the maximum response size (in bytes) willing to be accepted.- Parameters:
protocolName- theprotocolNameargument (notnull)apduHex- theapduHexargument (notnull)bdRecv- thebdRecvargument (notnull)maxRecvLen- themaxRecvLenargument- Throws:
ChilkatException- if the operation fails;ChilkatException.getLastErrorText()says why
-
setProgressListener
Installs the listener that receives this object's progress events (abortCheck,percentDone,progressInfo) during its long-running methods; replaces any listener or lambda installed before;nullremoves it.- Parameters:
listener- the listener, ornull- Throws:
IllegalStateException- if called from inside one of this object's own callbacks
-
getProgressListener
Returns the installed listener, ornull.- Returns:
- the listener installed with
setProgressListener(ProgressListener)or one of theon...methods
-
onAbortCheck
Installs a listener whose only callback isabortCheck: called everyHeartbeatMsmilliseconds during a long-running method; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onPercentDone
Installs a listener whose only callback ispercentDone: called with the completion percentage (0..100) as a long-running method progresses; returntrueto abort it. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-
onProgressInfo
Installs a listener whose only callback isprogressInfo: called with a name/value pair of progress information as a long-running method proceeds. Replaces any listener installed before.- Parameters:
callback- the callback, ornullto remove the listener
-