<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>day32 &amp;mdash; Gerrit Niezen</title>
    <link>https://gerritniezen.com/tag:day32</link>
    <description>Maker of open-source software and hardware.</description>
    <pubDate>Mon, 20 Apr 2026 04:35:47 +0000</pubDate>
    <image>
      <url>https://i.snap.as/aMPXpIot.png</url>
      <title>day32 &amp;mdash; Gerrit Niezen</title>
      <link>https://gerritniezen.com/tag:day32</link>
    </image>
    <item>
      <title>Getting started with Web NFC</title>
      <link>https://gerritniezen.com/getting-started-with-web-nfc?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[In preparation for the Stay-at-home TH/NG Jam next Friday, I thought I&#39;d at least check if I can get the basic tech, including Web NFC, working.&#xA;&#xA;Web NFC is an open specification, but so far it is only supported on Chrome for Android behind a feature flag, meaning you&#39;ll have to enable experimental web platform features in chrome://flags. It has also been available as an origin trial since Chrome v81.&#xA;&#xA;First, we need a basic index.html file:&#xA;&#xA;html&#xA;  head&#xA;    titleSmart Paper Maps/title&#xA;    script src=&#34;main.js&#34;/script&#xA;    link rel=&#34;stylesheet&#34; type=&#34;text/css&#34; href=&#34;main.css&#34;&#xA;  /head&#xA;  body&#xA;    button id=&#34;scan&#34; class=&#34;buttons&#34;Scan/button&#xA;    p id=&#34;p1&#34; class=&#34;text&#34;/p&#xA;  /body&#xA;/html&#xA;&#xA;All we&#39;re doing is linking to our main.js and main.css files, and setting up a button and a p section that we can use to display text. Our main.css just makes the button and text a bit bigger to make it easier to tap and read:&#xA;&#xA;.buttons{&#xA;    height:200px;&#xA;    width:200px;&#xA;    font-size: xx-large;&#xA;}&#xA;&#xA;.text{&#xA;  font-size: xx-large;&#xA;}&#xA;&#xA;And finally, the actual code that reads the NFC tags:&#xA;&#xA;document.addEventListener(&#39;DOMContentLoaded&#39;, event =  {&#xA;  const scanButton = document.getElementById(&#39;scan&#39;);&#xA;  const reader = new NDEFReader();&#xA;  &#xA;  scanButton.addEventListener(&#39;click&#39;, async () =  {&#xA;    try {&#xA;      console.log(&#39;Scanning..&#39;);&#xA;      await reader.scan();&#xA;    } catch(e) {&#xA;      console.error(&#34;Error: &#34;, e);&#xA;    }&#xA;  });&#xA;  &#xA;  reader.onreading = event =  {&#xA;    console.log(&#39;Event:&#39;, event);&#xA;    document.getElementById(&#34;p1&#34;).innerHTML = event.serialNumber;&#xA;  };&#xA;});&#xA;&#xA;We wait for the page to finish loading, initialise the NFC tag reader module NDEFReader(), and wait for the &#34;Scan&#34; button to be clicked. When we click the button, the browser will prompt for permission the first time. Once permission is granted, it will start scanning for NFC tags, and display the serial number when a tag gets read.&#xA;&#xA;Because Web NFC requires an HTTPS Server, you&#39;ll have to set up a basic one to get this working. I&#39;m using Node.js 12, and saved the following as simple-http-server.js:&#xA;&#xA;const https = require(&#39;https&#39;);&#xA;const fs = require(&#39;fs&#39;);&#xA;const express = require(&#39;express&#39;);&#xA;&#xA;const options = {&#xA;  key: fs.readFileSync(&#39;newkey.key&#39;),&#xA;  cert: fs.readFileSync(&#39;newkey.crt&#39;)&#xA;};&#xA;const app = express();&#xA;app.use(express.static(_dirname));&#xA;&#xA;https.createServer(options, app).listen(8080);&#xA;&#xA;Do an npm install to install the Express server module, and set up the SSL keys using:&#xA;&#xA;openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout newkey.key -out newkey.crt&#xA;&#xA;Then run it with node simple-http-server.js. If you don&#39;t see any errors, you did it right. Go to your computer&#39;s IP address:8080 to access the page. You&#39;ll see some security warnings that you&#39;ll have to acknowledge, since we created our own self-signed keys.&#xA;&#xA;I want to be able to view and debug my phone on my computer. Luckily, Chrom(ium) and Android makes this really easy. First, enable developer options on your Android phone and plug in your device via USB. To enable remote debugging, go to chrome://inspect/#devices in Chrom(ium) on your computer. You should be able to see your phone under Remote Target, and can then click on Inspect under the right browser tab to view your phone screen and the Chrome developer tools.&#xA;&#xA;Hopefully, if everything is working, you&#39;ll see the following when you press the scan button and move over an NFC tag:&#xA;&#xA;---&#xA;I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting https://100daystooffload.com.&#xA;&#xA;#100DaysToOffload #day32&#xA;&#xA;iComment on this post/i&#xD;&#xA;div id=&#34;cusdisthread&#34;/div]]&gt;</description>
      <content:encoded><![CDATA[<p>In preparation for the <a href="https://thingscon.org/events/upcoming-event/stay-at-home-th-ngs-jam/">Stay-at-home TH/NG Jam</a> next Friday, I thought I&#39;d at least check if I can get the basic tech, including <a href="https://www.chromestatus.com/feature/6261030015467520">Web NFC</a>, working.</p>

<p>Web NFC is an open specification, but so far it is only supported on Chrome for Android behind a feature flag, meaning you&#39;ll have to enable experimental web platform features in <code>chrome://flags</code>. It has also been available <a href="https://developers.chrome.com/origintrials/#/trials/active">as an origin trial</a> since Chrome v81.</p>

<p>First, we need a basic <code>index.html</code> file:</p>

<pre><code class="language-html">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Smart Paper Maps&lt;/title&gt;
    &lt;script src=&#34;main.js&#34;&gt;&lt;/script&gt;
    &lt;link rel=&#34;stylesheet&#34; type=&#34;text/css&#34; href=&#34;main.css&#34;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;button id=&#34;scan&#34; class=&#34;buttons&#34;&gt;Scan&lt;/button&gt;
    &lt;p id=&#34;p1&#34; class=&#34;text&#34;&gt;&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>All we&#39;re doing is linking to our <code>main.js</code> and <code>main.css</code> files, and setting up a button and a <code>&lt;p&gt;</code> section that we can use to display text. Our <code>main.css</code> just makes the button and text a bit bigger to make it easier to tap and read:</p>

<pre><code class="language-css">.buttons{
    height:200px;
    width:200px;
    font-size: xx-large;
}

.text{
  font-size: xx-large;
}
</code></pre>

<p>And finally, the actual code that reads the NFC tags:</p>

<pre><code class="language-js">document.addEventListener(&#39;DOMContentLoaded&#39;, event =&gt; {
  const scanButton = document.getElementById(&#39;scan&#39;);
  const reader = new NDEFReader();
  
  scanButton.addEventListener(&#39;click&#39;, async () =&gt; {
    try {
      console.log(&#39;Scanning..&#39;);
      await reader.scan();
    } catch(e) {
      console.error(&#34;Error: &#34;, e);
    }
  });
  
  reader.onreading = event =&gt; {
    console.log(&#39;Event:&#39;, event);
    document.getElementById(&#34;p1&#34;).innerHTML = event.serialNumber;
  };
});
</code></pre>

<p>We wait for the page to finish loading, initialise the NFC tag reader module <code>NDEFReader()</code>, and wait for the “Scan” button to be clicked. When we click the button, the browser will prompt for permission the first time. Once permission is granted, it will start scanning for NFC tags, and display the serial number when a tag gets read.</p>

<p>Because Web NFC requires an HTTPS Server, you&#39;ll have to set up a basic one to get this working. I&#39;m using Node.js 12, and saved the following as <code>simple-http-server.js</code>:</p>

<pre><code class="language-js">const https = require(&#39;https&#39;);
const fs = require(&#39;fs&#39;);
const express = require(&#39;express&#39;);

const options = {
  key: fs.readFileSync(&#39;newkey.key&#39;),
  cert: fs.readFileSync(&#39;newkey.crt&#39;)
};
const app = express();
app.use(express.static(__dirname));

https.createServer(options, app).listen(8080);
</code></pre>

<p>Do an <code>npm install</code> to install the Express server module, and set up the SSL keys using:</p>

<pre><code>openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout newkey.key -out newkey.crt
</code></pre>

<p>Then run it with <code>node simple-http-server.js</code>. If you don&#39;t see any errors, you did it right. Go to <code>&lt;your computer&#39;s IP address&gt;:8080</code> to access the page. You&#39;ll see some security warnings that you&#39;ll have to acknowledge, since we created our own self-signed keys.</p>

<p>I want to be able to view and debug my phone on my computer. Luckily, Chrom(ium) and Android makes this really easy. First, <a href="https://developer.android.com/studio/debug/dev-options.html">enable developer options on your Android phone</a> and plug in your device via USB. To enable remote debugging, go to <code>chrome://inspect/#devices</code> in Chrom(ium) on your computer. You should be able to see your phone under <em>Remote Target</em>, and can then click on <em>Inspect</em> under the right browser tab to view your phone screen and the Chrome developer tools.</p>

<p>Hopefully, if everything is working, you&#39;ll see the following when you press the scan button and move over an NFC tag:</p>

<p><img src="https://i.snap.as/RVagi2u.png" alt=""/></p>

<hr/>

<p>I’m publishing this as part of 100 Days To Offload. You can join in yourself by visiting <a href="https://100daystooffload.com">https://100daystooffload.com</a>.</p>

<p><a href="https://gerritniezen.com/tag:100DaysToOffload" class="hashtag"><span>#</span><span class="p-category">100DaysToOffload</span></a> <a href="https://gerritniezen.com/tag:day32" class="hashtag"><span>#</span><span class="p-category">day32</span></a></p>

<p><i>Comment on this post</i>
<div id="cusdis_thread" id="cusdis_thread"></div></p>
]]></content:encoded>
      <guid>https://gerritniezen.com/getting-started-with-web-nfc</guid>
      <pubDate>Wed, 27 May 2020 20:11:08 +0000</pubDate>
    </item>
  </channel>
</rss>