5. Read out data with a card reader
Once you've selected the required card reader by means of the getCardReader
method, you need to create a CardHandler
for the card that is present in the selected CardReader
.
The CardHandler
contains the actual implementation to handle whichever card is inserted and differs from one card to another.
Note: Don't worry about the different implementations, the PluginApi
instance deals with this for us.
The data that is available on a card also depends on the card type. Just like the cardInfo
object, both BeID
and BeLawyer
namespaces within the wrapper expose DataKindHints
which can be used to retrieve a specific set of data on the card. The readData
method will return a dictionary that contains a data result for each DataKindHint
that was understood by the handler. All other DataKindHints
that are not understood are ignored.
To create a CardHandler
follow the example below:
let cardReader: CardReader;
let cardHandler: CardHandler;
pluginApi.getCardReader(BeID.cardInfo)
.then((cardReadersWithBeID) => {
cardReader = cardReadersWithBeID[0];
cardHandler = pluginApi.getCardHandler(cardReader);
})
.then(() => {
return cardHandler.readData(cardReader, BeID.DataKindHints.PersonalInformation)
})
.then((dataResult: {[index:string]: any}) => {
// sadly we have to do some magic "casting" over here... the generic getData method currently has no way to statically know the data type
// that will be returned. The modules have defined that when fetching BeID PersonalInformation DataKindHint it will return an instance
// of the BeID.PersonalInformation interface and we must trust the module to do so.
const personalInformation: BeID.PersonalInformation = dataResult[BeID.DataKindHints.PersonalInformation];
console.log(personalInformation);
})
.catch((e: Error) => {
// ...
});