### PN532 Initialization with Adapters
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Shows how to initialize a PN532 instance and register different adapter plugins for communication. Examples include using WebSerialAdapter, WebbleAdapter, and SerialPortAdapter (Node.js). It also demonstrates fetching the firmware version.
```javascript
// setup
const {
_, // lodash: https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js
Pn532: { Pn532, Packet, utils: Pn532utils },
Crypto1,
Pn532Hf14a,
Pn532WebbleAdapter,
Pn532WebserialAdapter,
} = window
// Pn532WebserialAdapter
const pn532usb = new Pn532()
pn532usb.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
console.log(JSON.stringify(await pn532usb.getFirmwareVersion())) // {"firmware":"1.6","ic":"PN532","iso14443a":true,"iso14443b":true,"iso18092":true}
// Pn532WebbleAdapter
const pn532ble = new Pn532()
pn532ble.use(new Pn532WebbleAdapter()) // A pn532 instance must register exactly one adapter plugin
console.log(JSON.stringify(await pn532ble.getFirmwareVersion())) // {"firmware":"1.6","ic":"PN532","iso14443a":true,"iso18092":true}
// Pn532SerialPortAdapter
import Pn532SerialPortAdapter from 'pn532.js/plugin/SerialPortAdapter.js'
// Run serialport-list to list port, see https://serialport.io/docs/bin-list
const pn532node = new Pn532()
pn532node.use(new Pn532SerialPortAdapter(), { path: '/dev/tty.usbserial-120' })
console.log(JSON.stringify(await pn532node.getFirmwareVersion())) // {"firmware":"1.6","ic":"PN532","iso14443a":true,"iso18092":true}
```
--------------------------------
### Install PN532.js with yarn
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Installs the PN532.js library using yarn. It also shows how to install the 'serialport' package if you intend to use the library in a Node.js environment.
```bash
$ yarn add pn532.js
# Also install SerialPort if you want to run in node.js
$ yarn add serialport
```
--------------------------------
### Install PN532.js with npm
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Installs the PN532.js library using npm. It also shows how to install the 'serialport' package if you intend to use the library in a Node.js environment.
```bash
$ npm install pn532.js
# Also install SerialPort if you want to run in node.js
$ npm install serialport
```
--------------------------------
### MIFARE Classic 1k Sector Operations (PN532)
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates reading and writing entire sectors of a MIFARE Classic 1k card using the PN532 library. Includes examples for reading with Key A and attempting reads with Key B then Key A, as well as writing a sector.
```javascript
// setup
const { _, Pn532: { Pn532, Packet, utils: Pn532utils }, Pn532Hf14a, Pn532WebserialAdapter } = window
const pn532 = new Pn532()
pn532.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
pn532.use(new Pn532Hf14a())
const key = Packet.fromHex('FFFFFFFFFFFF')
// read a sector
let resp3 = await pn532.$hf14a.mfReadSector({ sector: 0, isKb: 1, key })
console.log(JSON.stringify(resp3)) // {"data":"Packet(64): 0102030404080400000000000000BEAF... (truncated)","success":[1,1,1,1]}
console.log(resp3.data.hex) // 0102030404080400000000000000BEAF... (truncated)
console.log(resp3.data.inspect) // Packet(64): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF... (truncated)
// try to read a sector with key B then Key A
let resp4 = await pn532.$hf14a.mfReadSectorKeyBA({ sector: 0, ka: key, kb: key })
console.log(JSON.stringify(resp4)) // {"data":"Packet(64): 0102030404080400000000000000BEAF... (truncated)","success":{"key":[1,1],"read":[1,1,1,1]}}
console.log(resp4.data.hex) // 0102030404080400000000000000BEAF... (truncated)
console.log(resp4.data.inspect) // Packet(64): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF... (truncated)
// write a sector
const dataSector = new Packet(64)
dataSector.set(Packet.fromHex('FFFFFFFFFFFF08778F00FFFFFFFFFFFF'), 48)
let resp6 = await pn532.$hf14a.mfWriteSector({ sector: 1, isKb: 1, key, data: dataSector })
console.log(JSON.stringify(resp6)) // {"success":[1,1,1,1]}
// try to write a sector with key B then Key A
let resp7 = await pn532.$hf14a.mfWriteSectorKeyBA({ sector: 1, ka: key, kb: key, data: dataSector })
console.log(JSON.stringify(resp7)) // {"success":[1,1,1,1]}
```
--------------------------------
### MIFARE Classic 1k Block Operations (PN532)
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates reading and writing individual blocks of a MIFARE Classic 1k card using the PN532 library. Includes examples for reading with Key A and attempting reads with Key B then Key A, as well as writing a block.
```javascript
// setup
const { _, Pn532: { Pn532, Packet, utils: Pn532utils }, Pn532Hf14a, Pn532WebserialAdapter } = window
const pn532 = new Pn532()pn532.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
pn532.use(new Pn532Hf14a())
const key = Packet.fromHex('FFFFFFFFFFFF')
// read a block
let resp1 = await pn532.$hf14a.mfReadBlock({ block: 0, isKb: 1, key })
console.log(resp1.hex) // 0102030404080400000000000000BEAF
console.log(resp1.inspect) // Packet(16): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF
// try to read a block with key B then Key A
let resp2 = await pn532.$hf14a.mfReadBlockKeyBA({ block: 0, ka: key, kb: key })
console.log(resp2.hex) // 0102030404080400000000000000BEAF
console.log(resp2.inspect) // Packet(16): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF
// write a block
const dataBlock = Packet.fromHex('000102030405060708090A0B0C0D0E0F')
await pn532.$hf14a.mfWriteBlock({ block: 1, isKb: 1, key, data: dataBlock })
// try to write a block with key B then Key A
await pn532.$hf14a.mfWriteBlockKeyBA({ block: 1, ka: key, kb: key, data: dataBlock })
```
--------------------------------
### Reading UID, ATQA, SAK from Mifare Classic 1k
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Provides an example of how to read the Unique Identifier (UID), Answer to Request (ATQA), and Select Acknowledge (SAK) from a Mifare Classic 1k card using the PN532 library. This involves initializing the PN532 with a WebSerialAdapter and the Hf14a plugin.
```javascript
// setup
const {
_, // lodash: https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js
Pn532: { Pn532, Packet, utils: Pn532utils },
Pn532Hf14a,
Pn532WebserialAdapter,
} = window
const pn532 = new Pn532()
pn532.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
pn532.use(new Pn532Hf14a())
console.log(JSON.stringify(await pn532.$hf14a.mfSelectCard())) // {"pack":"Packet(9): 010004080407460D6D","atqa":"Packet(2): 0004","sak":"Packet(1): 08","uid":"Packet(4): 07460D6D","rats":"Packet(0)"}
```
--------------------------------
### Import PN532.js modules (ES Modules)
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates how to import various modules from the PN532.js library using ES module syntax. This includes the main Pn532 class, Packet, utilities, Crypto1, and different adapter/plugin modules.
```javascript
import { Pn532, Packet, utils: Pn532utils } from 'pn532.js'
import Crypto1 from 'pn532.js/Crypto1.js'
import LoggerRxTx from 'pn532.js/plugin/LoggerRxTx.js'
import Pn532Hf14a from 'pn532.js/plugin/Hf14a.js'
import Pn532SerialPortAdapter from 'pn532.js/plugin/SerialPortAdapter.js'
import Pn532WebbleAdapter from 'pn532.js/plugin/WebbleAdapter.js'
import Pn532WebserialAdapter from 'pn532.js/plugin/WebserialAdapter.js'
```
--------------------------------
### CDN Integration for PN532.js
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Includes necessary script tags for loading PN532.js and its dependencies (lodash) from jsDelivr CDN. It also lists the core library and various plugin scripts for different functionalities like Crypto1, Hf14a, LoggerRxTx, WebbleAdapter, and WebserialAdapter.
```html
```
--------------------------------
### Import PN532.js modules (CommonJS)
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates how to import various modules from the PN532.js library using CommonJS syntax. This includes the main Pn532 class, Packet, utilities, Crypto1, and different adapter/plugin modules.
```javascript
const { Pn532, Packet, utils: Pn532utils } = require('pn532.js')
const Crypto1 = require('pn532.js/Crypto1.js')
const LoggerRxTx = require('pn532.js/plugin/LoggerRxTx.js')
const Pn532Hf14a = require('pn532.js/plugin/Hf14a.js')
const Pn532SerialPortAdapter = require('pn532.js/plugin/SerialPortAdapter.js')
const Pn532WebbleAdapter = require('pn532.js/plugin/WebbleAdapter.js')
const Pn532WebserialAdapter = require('pn532.js/plugin/WebserialAdapter.js')
```
--------------------------------
### MIFARE Classic 1k Card Operations by Keys (PN532)
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates reading and writing an entire MIFARE Classic 1k card by iterating through sectors and applying provided keys. This is useful for cards with unknown or multiple keys.
```javascript
// setup
const { _, Pn532: { Pn532, Packet, utils: Pn532utils }, Pn532Hf14a, Pn532WebserialAdapter } = window
const pn532 = new Pn532()
pn532.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
pn532.use(new Pn532Hf14a())
const key = Packet.fromHex('FFFFFFFFFFFF')
// try to read a Mifare Classic 1k by keys
let resp5 = await pn532.$hf14a.mfReadCardByKeys({ sectorMax: 16, keys: [key] })
console.log(JSON.stringify(resp5)) // {"data":"Packet(1024): 0102030404080400000000000000BEAF... (truncated)","success":{"key":["Packet(6): FFFFFFFFFFFF",... (truncated)],"read":[1,1,1,1,... (truncated)]}}
console.log(resp5.data.hex) // 0102030404080400000000000000BEAF... (truncated)
console.log(resp5.data.inspect) // Packet(1024): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF... (truncated)
// try to write a Mifare Classic 1k by keys
const dataCard = new Packet(1024)
dataCard.set(Packet.fromHex('0102030404080400000000000000BEAF'))
for (let i = 0; i < 16; i++) dataCard.set(Packet.fromHex('FFFFFFFFFFFF08778F00FFFFFFFFFFFF'), i * 64 + 48)
let resp8 = await pn532.$hf14a.mfWriteCardByKeys({ sectorMax: 16, keys: [key], data: dataCard })
console.log(JSON.stringify(resp8)) // {"success":[1,1,1,1,... (truncated)]}
```
--------------------------------
### PN532.js MIFARE Classic Gen1A Operations
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates various operations for MIFARE Classic Gen1A cards using the PN532.js library. This includes reading and writing individual blocks, entire sectors, and the whole card. It also shows how to set a random UID and wipe the card. The code requires the PN532 library, Pn532Hf14a plugin, and Pn532WebserialAdapter.
```js
// setup
const {
_, // lodash: https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js
Pn532: { Pn532, Packet, utils: Pn532utils },
Pn532Hf14a,
Pn532WebserialAdapter,
} = window
const pn532 = new Pn532()
pn532.use(new Pn532WebserialAdapter()) // A pn532 instance must register exactly one adapter plugin
pn532.use(new Pn532Hf14a())
// read a block
let resp1 = await pn532.$hf14a.mfReadBlockGen1a({ block: 0 })
console.log(resp1.hex) // 0102030404080400000000000000BEAF
console.log(resp1.inspect) // Packet(16): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF
// read a sector
let resp2 = await pn532.$hf14a.mfReadSectorGen1a({ sector: 0 })
console.log(JSON.stringify(resp2)) // {"data":"Packet(64): 0102030404080400000000000000BEAF... (truncated)","success":[1,1,1,1]}
console.log(resp2.data.hex) // 0102030404080400000000000000BEAF... (truncated)
console.log(resp2.data.inspect) // Packet(64): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF... (truncated)
// read a Mifare Classic 1k Gen1A
let resp3 = await pn532.$hf14a.mfReadCardGen1a({ sectorMax: 16 })
console.log(JSON.stringify(resp3)) // {"data":"Packet(1024): 0102030404080400000000000000BEAF... (truncated)","success":[1,1,1,1,... (truncated)]}
console.log(resp3.data.hex) // 0102030404080400000000000000BEAF... (truncated)
console.log(resp3.data.inspect) // Packet(1024): 01 02 03 04 04 08 04 00 00 00 00 00 00 00 BE AF... (truncated)
// write a block
const dataBlock = Packet.fromHex('000102030405060708090A0B0C0D0E0F')
await pn532.$hf14a.mfWriteBlockGen1a({ block: 1, data: dataBlock })
// write a sector
const dataSector = new Packet(64)
dataSector.set(Packet.fromHex('FFFFFFFFFFFF08778F00FFFFFFFFFFFF'), 48)
let resp4 = await pn532.$hf14a.mfWriteSectorGen1a({ sector: 1, data: dataSector })
console.log(JSON.stringify(resp4)) // {"success":[1,1,1,1]}
// write a Mifare Classic 1k Gen1A
const dataCard = new Packet(1024)
dataCard.set(Packet.fromHex('0102030404080400000000000000BEAF'))
for (let i = 0; i < 16; i++) dataCard.set(Packet.fromHex('FFFFFFFFFFFF08778F00FFFFFFFFFFFF'), i * 64 + 48)
let resp5 = await pn532.$hf14a.mfWriteCardGen1a({ sectorMax: 16, data: dataCard })
console.log(JSON.stringify(resp5)) // {"success":[1,1,1,1,... (truncated)]}
// set a random UID
await pn532.$hf14a.mfSetUidGen1a({ uid: new Packet(_.times(4, () => _.random(0, 0xFF))) })
// wipe a Mifare Classic 1k Gen1A
let resp6 = await pn532.$hf14a.mfWipeGen1a({ sectorMax: 16, uid: Packet.fromHex('01020304') })
console.log(JSON.stringify(resp6)) // {"success":[1,1,1,1,... (truncated)]}
```
--------------------------------
### Accessing PN532.js Globals
Source: https://github.com/taichunmin/pn532.js/blob/master/README.md
Demonstrates how to access the globally available PN532 library components and its plugins after they have been loaded via CDN. This includes lodash, the core Pn532 class, Packet, Pn532utils, Crypto1, and various adapter and plugin classes.
```javascript
// setup
const {
_, // lodash: https://cdn.jsdelivr.net/npm/lodash@4/lodash.min.js
Pn532: { Pn532, Packet, utils: Pn532utils },
Crypto1,
Pn532Hf14a,
Pn532LoggerRxTx,
Pn532WebbleAdapter,
Pn532WebserialAdapter,
} = window
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.