GithubHelp home page GithubHelp logo

isabella232 / chrome-nfc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlearchive/chrome-nfc

0.0 0.0 0.0 271 KB

Chrome App NFC Library

License: Other

Python 1.21% CSS 0.53% HTML 2.78% JavaScript 95.48%

chrome-nfc's Introduction

Chrome App NFC Library

With this simple library, you can build a Chrome App that communicates over USB with NFC Readers.

Supported NFC Readers

ACR122U SCL3711

Play with the Chrome App sample

  • Check Developer Mode in chrome://extensions
  • Click "Load unpacked extension..." in chrome://extensions and select the sample folder.
  • Launch it.

Caveats

Learn more about USB Devices Caveats at https://developer.chrome.com/apps/app_usb#caveats

Usage

Once you've imported the chrome-nfc.js javascript library into your Chrome App, you need to add the permissions below to your manifest file:

"permissions": [
  "usb",
  {
    "usbDevices": [
      { "vendorId": 1254, "productId": 21905 }, // SCL3711
      { "vendorId": 1839, "productId": 8704 }   // ACR122U
    ]
  }
]

Enumerate NFC readers

chrome.nfc.findDevices(function(devices) {
  console.log("Found " + devices.length + " NFC device(s)");
  for (var i = 0; i < devices.length; i++) {
    var device = devices[i];
    console.log(device.vendorId, device.productId);
  }
});

Read NFC tag

chrome.nfc.findDevices(function(devices) {
  var device = devices[0];
  chrome.nfc.read(device, {}, function(type, ndef) {
    console.log(ndef);
    var uri = ndef.ndef[0]["uri"];
    console.log(uri);
    var text = ndef.ndef[1]["text"];
    console.log(text);
  });
});

Write NFC tag

chrome.nfc.findDevices(function(devices) {
  var device = devices[0];
  var ndef = [
    {"text": "Chromium.org website" },
    {"uri": "http://chromium.org" },
    {"aar": "com.google.samples.apps.iosched" },
  ];
  chrome.nfc.write(device, {"ndef": ndef}, function(rc) {
    if (!rc) {
      console.log("WRITE() success!");
    } else {
      console.log("WRITE() FAILED, rc = " + rc);
    }
  });
});

Emulate NFC tag

chrome.nfc.findDevices(function(devices) {
  var device = devices[0];
  var ndef = [
    {"type": "URI", "uri": "http://chromium.org"}
  ];
  chrome.nfc.emulate_tag(device, {"ndef": ndef}, function(rc) {
    if (!rc) {
      console.log("EMULATE() success!");
    } else {
      console.log("EMULATE() FAILED, rc = " + rc);
    }
  });
});

Read Mifare Classic tag (Logic Mode)

chrome.nfc.findDevices(function(devices) {
  var device = devices[0];
  chrome.nfc.read_logic(device, 0, 2, function(rc, data) {
    console.log(UTIL_BytesToHex(data));
  });
});

Write Mifare Classic tag (Logic Mode)

chrome.nfc.findDevices(function(devices) {
  var device = devices[0];
  var data = new Uint8Array([ // NDEF(http://google.com)
    0xdb, 0x00, 0x03, 0xe1, 0x00, 0x00, 0x00, 0x00, // block 0 (MAD1)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // block 1 (MAD1)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x0f, 0xd1, 0x01, 0x0b, 0x55, 0x03, 0x67, // block 2 (NDEF)
    0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x63, 0x6f,
    0x6d, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // block 3 (NDEF)
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  ]);
  chrome.nfc.write_logic(device, 0, data, function(rc) {
    console.log("WRITE_LOGIC() SUCCESS");
  });
});

Compiling the library

Compiling script requires Python 3.0 and will use online Closure Compiler. Just run

python3 compile.py

and the library will be written to chrome-nfc.js.

chrome-nfc's People

Contributors

beaufortfrancois avatar martinsik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.