Skip to content

Instantly share code, notes, and snippets.

@defrindr
Created June 23, 2024 12:11
Show Gist options
  • Save defrindr/95af746427b58359e48e9903a440c4a9 to your computer and use it in GitHub Desktop.
Save defrindr/95af746427b58359e48e9903a440c4a9 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button id="connect">Konek ke NFC</button>
<script>
let device;
document.addEventListener("DOMContentLoaded", (event) => {
let button = document.getElementById("connect");
button.addEventListener("click", async () => {
const VENDOR_ID = 0x072f;
try {
// Periksa apakah browser mendukung WebUSB
if (navigator.usb) {
console.log("WebUSB didukung");
// Minta izin pengguna untuk mengakses perangkat NFC
navigator.usb
.requestDevice({
filters: [
{
vendorId: VENDOR_ID,
},
],
})
.then(async function (device) {
console.log("Perangkat NFC ditemukan:", device);
// Buka koneksi WebUSB ke perangkat NFC
await device.open();
console.log("Koneksi WebUSB dibuka");
console.log("configurations:", device.configurations);
if (device.configuration === null) {
console.log("selectConfiguration");
await device.selectConfiguration(1);
}
console.log("interfaces:", device.configuration.interfaces);
console.log("claimInterface");
await device.claimInterface(0);
// Kirim perintah untuk membaca UID tag NFC
device
.controlTransferOut(
0x01,
0x0f,
0x00,
0x00,
new Uint8Array([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
])
)
.then(function () {
// Baca respons dari perangkat NFC
device
.controlTransferIn(0x01, 0x0f, 0x00, 0x00, 7)
.then(function (response) {
console.log(
"UID tag NFC:",
response.data.buffer.slice(0, 7)
);
});
});
});
} else {
console.log("WebUSB tidak didukung");
}
} catch (error) {}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment