Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NFC support? #1

Open
OR13 opened this issue Apr 21, 2022 · 0 comments
Open

NFC support? #1

OR13 opened this issue Apr 21, 2022 · 0 comments

Comments

@OR13
Copy link
Member

OR13 commented Apr 21, 2022

import React from "react";
const base32Decode = require("base32-decode");

const b32vc =
  "3ECQDKQBQIIXQH3IOR2HA4Z2F4XXOM3JMQXG64THF53GCY3DNFXGC5DJN5XC65RRDA7YEDYLDAVXQH3VOJXDU5LWMNUTUYLGGV3HG2DEMU4DIM3KMY4DGMLKGEZDQZTKDA2HQICDJ5LESRBNGE4SAVTBMNRWS3TBORUW63RAINSXE5DJMZUWGYLUMUMB66BAINHVMSKEFUYTSICWMFRWG2LOMF2GS33OEBBWK4TUNFTGSY3BORSRQLQ2LXTFF2AYEQNHBNSKNAMDBAQZAQAVQIXNAGYSIJELXLKBGEIGT3ETNQ2W4MLFKBIFYIH4PTABRBJERWVYVF6IUGA5U4MD6DAVNIYTCOBTG4ZTQNJWHEIWGTLPJAMCSY2NN5EBQGDCJZNBQO5FDA7Q4GBIMRFE6SCODATGKU2NJFKEQGBHMRGWC3DFCZVDCOJVHAWTANZNGE3RQQFFDA7Q2GBANBBU6VSJIQWTCOITM5FDAN2CLAYDGGBTPAMEGT2WJFCC2MJZEBLGCY3DNFXGKICNN5SGK4TOMEMDE32NN5SGK4TOMEQEE2LPORSWG2AYHCSRQPYHDAMRUYDQ5KABQMLYSBSXSSTIMJDWG2KPNFFEMWSFKJKFCU2JONEW2SJSJZBUSNS2NVDHGYZSKVZUS3KOPFQVQULJJ5WHG2KZNJMTASLMGE4S4LRXGRKWE4TVOY2UYUBTKJXEMZDII5FDQYKCGRLGM3LGKNGTO6LUOJLWCU2SL5TVG3BZOFMDCMSDKFKG4NKWOY2XS2CZJRBE25KSPFLUQT3XOVRU2RLOKFPVUS3ONB4S2R3CGFMTKQLHDA4REGCEQMMQIAKYELWQDMJEESF3VVATCEDJ5SJWYNLOGFSVAUC4ED6HZQAYQUSI3K4KS7EKLARO2ANREQSIXOWUCMIQNHWJG3BVNYYWKUCQLQQPY7GADCCSJDNLRKL4RI";

function buf2hex(buffer) {
  // buffer is an ArrayBuffer
  return Array.prototype.map
    .call(new Uint8Array(buffer), (x) => ("00" + x.toString(16)).slice(-2))
    .join("");
}

function App() {
  const [state, setState] = React.useState({
    NDEFReader: "NDEFReader" in window,
  });
  return (
    <div className="App">
      <button
        onClick={async () => {
          console.log("asdf");
          if ("NDEFReader" in window) {
            const { NDEFReader } = window;
            const writer = new NDEFReader();

            const jsonRecord = {
              recordType: "mime",
              mediaType: "application/ld+cbor",
              data: Buffer.from("hello"),
            };
            const res = await writer.write({
              records: [jsonRecord],
            });
            setState({ res, write: true });
          }
        }}
      >
        write
      </button>

      <button
        onClick={async () => {
          console.log("asdf");
          if ("NDEFReader" in window) {
            const { NDEFReader } = window;
            /* Scan NFC tags */
            const reader = new NDEFReader();
            reader.scan().then(() => {
              console.log("Scan started successfully.");
              reader.onerror = () => {
                console.log(
                  "Cannot read data from the NFC tag. Try another one?"
                );
              };
              reader.onreading = async (event) => {
                let parsedRecords = [];
                for (const record of event.message.records) {
                  console.log("Record type:  " + record.recordType);
                  console.log("MIME type:    " + record.mediaType);
                  console.log("Record id:    " + record.id);
                  console.log("Record data:    " + record.data);

                  parsedRecords.push({
                    id: record.id,
                    recordType: record.recordType,
                    mediaType: record.mediaType,
                    data: Buffer.from(
                      buf2hex(record.data.buffer),
                      "hex"
                    ).toString("utf8"),
                  });
                }
                console.log("parsedRecords...", parsedRecords);
                setState({ parsedRecords });
              };
            });
          }
        }}
      >
        read
      </button>

      <pre>{JSON.stringify(state, null, 2)}</pre>
    </div>
  );
}

export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant