Re: [web-nfc] NDEF Read triggered when writting an NDEF (#551)

> > Here is a screencast (in a zip file).
> > I have clicked on "Write NFC Tag", then on "Write URI", then I have taped the NFC Tag.
> > The URI is written successfully. As you can see, I immediately gets a notification to open the URI in Chrome.
> 
> Based on the video I would say things work as they are designed to work (*) and also according to your [preference](https://github.com/w3c/web-nfc/issues/551#issuecomment-601024359) to not interfere with other apps on reading.

I confirm. I have started a scan, then moved to another Chrome Tab and then taped a tag. The native processing is executed so that's fine.

> 
> (*) But IIUC you would prefer that Web NFC implementation would create and "own" a sort of "tag writing session" in a way that re-reading that tag would get dispatched to Web NFC and discarded there?

Yes and I was proposing to make it the default behavior.


> Of course the app could do that by:
> 
> * setting up a scan
> * write with `ignoreRead=true`
> * ignoring the read event.
>   But I understood that was not your preference, right?

This is not easy to do. For the moment I haven't succeed to get a version working reliably.
Here is my code. I had to use ignoreRead=false in order to be sure that the NFC event get hooked by my application and doesn't get processed natively.


```
function writeUriNdef(uriTxt) {
    
  console.log("writeUriNdef: " + uriTxt);
  
  if (!('NDEFWriter' in window)) {
    showWebNfcWarning();
    return;
  }

  if (!window.isSecureContext) {
    showSecureContextWarning();
    return;
  }

  var message = {
    records : [
      {
        recordType: "url",
        data: uriTxt
      }
    ]
  };

 console.log("Please tap the NFC tag to write...");
  showDialog("Please tap the NFC tag to write...", "", cancelPushRequest);

  writerAbortController = new AbortController();
  writerAbortController.signal.onabort = event => {
    console.log("All NFC Write operations have been aborted.");
  };  

  readerAbortController = new AbortController();
  readerAbortController.signal.onabort = event => {
    console.log("All NFC Read operations have been aborted.");
  }; 

  const reader = new NDEFReader();  
  const writer = new NDEFWriter();
 
  reader.scan({ signal: readerAbortController.signal })
 .then(() => {  
    reader.onreading = event => {
      console.log("NFC read event ignored (write in progress)");    
    };
  }).catch(error => {
    console.log("Read failed: " + error.name);
  });

 writer.write(message, { signal: writerAbortController.signal , ignoreRead: false })
  .then(() => {
  console.log("NDEF written successfully.");
    showDialog("NDEF written successfully.");
  readerAbortController.abort();
  })
  .catch((error) => {
  console.log("error: " + error.name);
  
    switch(error.name) {
      case "NotSupportedError":
        showDialog('Please turn NFC ON');
        break;
      case "AbortError":
        //showDialog('Abort error');
        break;
      default:
        showDialog('Push Failed, please try again: ' + error.name);
        break;
    }
  }); 

}

function cancelPushRequest() {
  console.log("cancelPushRequest");
  writerAbortController.abort();
 readerAbortController.abort();
}
```

Here is the console:
script.js:413 writeUriNdef: http://www.st.com/st25
script.js:434 Please tap the NFC tag to write...
script.js:453 NFC read event ignored (write in progress)
script.js:461 NDEF written successfully.
script.js:484 cancelPushRequest
script.js:439 All NFC Write operations have been aborted.
script.js:444 All NFC Read operations have been aborted.

"NFC read event ignored" has been called, which is fine but I still get the native processing.
> Now IIUC @kenchris, the intent with `ignoreRead` was actually that behaviour (in which case we need to update the write algorithm and don't need another write option).

2 years ago, I was using the previous WebNFC API and I didn't have this problem. I was using ignoreRead=true and I think that it was discarding the read event.


-- 
GitHub Notification of comment by OlivierGrenoble
Please view or discuss this issue at https://github.com/w3c/web-nfc/issues/551#issuecomment-601240977 using your GitHub account

Received on Thursday, 19 March 2020 15:23:33 UTC