Skip to content

Commit

Permalink
read me and nfc value write
Browse files Browse the repository at this point in the history
  • Loading branch information
Patricio Vargas committed Aug 12, 2021
1 parent 34e13d2 commit 1b836e4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 39 deletions.
146 changes: 109 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,142 @@
# Getting Started with Create React App
# REACT NFC Sample App

## About

This is a simple sample app demostrating the usage of the [Web NFC API](https://w3c.github.io/web-nfc/). To get the Web NFC API working you will need an Android Device with Google Chrome and you web app will need to be hosted using https.

This is the [sample app](https://react-nfc-90146.web.app/) in action.

### WTF is NFC?

NFC stands for **_Near-Field Communication_**. NFC is a set of communication protocols for communication between two electronic devices.

Electromagnetic fields can be used to transmit data or induce electrical currents in a receiving device. Passive NFC devices draw power from the fields produced by active devices, but the range is short.

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts
You can buy NFC Tags on [Amazon](https://www.amazon.com/gp/product/B0727NYX3B/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1). These tags can contain up to 540KB of info.

In the project directory, you can run:
## Usages

NFCs can have multiple usages, some of the usages are:

### `yarn start`
- Making contactless payments like Google and Apple Pay
- Opening a door using your badge
- Opening a link
- Produc control in a warehouse

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
To learn about the usages visit [this forum](https://nfc-forum.org/what-is-nfc/).

The page will reload if you make edits.\
You will also see any lint errors in the console.
## Getting Started with the Web NFC API

### `yarn test`
This project uses 4 methods of the Web NFC API

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
1. Scan: Returns a Promise resolved if starting NFC scan was successful.

### `yarn build`
`ndef.scan()`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
2. Reading: An event fired when a new reading is available.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
`ndef.onreading()`

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
3. Reading Error: An event fired when an error happened during reading.

### `yarn eject`
`ndef.onreadingerror()`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
4. Write: Returns a Promise resolved if writing the message (String, ArrayBuffer or NDEF record) with options was successful.
`ndef.write()`

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
## Using the methods

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
### Scan, Reading, Reading Error

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
```javascript
const scan = async() =>
if ("NDEFReader" in window) {
try {
const ndef = new window.NDEFReader();
await ndef.scan();

## Learn More
console.log("Scan started successfully.");
ndef.onreadingerror = () => {
console.log("Cannot read data from the NFC tag. Try another one?");
};

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
ndef.onreading = (event) => {
console.log("NDEF message read.");
onReading(event); //Find function below
};
} catch (error) {
console.log(`Error! Scan failed to start: ${error}.`);
}
}
};
```

To learn React, check out the [React documentation](https://reactjs.org/).
The **onReading** method grabs the message and serial number inside of the NFC tag, the uses the array of reacord inside of the message and decodes the information so its readable to humans.

### Code Splitting
```javascript
const onReading = ({message, serialNumber}) => {
console.log(serialNumber);
for (const record of message.records) {
switch (record.recordType) {
case "text":
const textDecoder = new TextDecoder(record.encoding);
console.log("Message": textDecoder.decode(record.data));
break;
case "url":
// TODO: Read URL record with record data.
break;
default:
// TODO: Handle other records with record data.
}
}
};
```

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Write

### Analyzing the Bundle Size
```javascript
const onWrite = () => {
try {
const ndef = new window.NDEFReader();
await ndef.write({
records: [{ recordType: "text", data: "Hellow World!" }],
});
console.log(`Value Saved!`);
} catch (error) {
console.log(error);
}
};
```

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
## Learn More & Resources

### Making a Progressive Web App
- https://web.dev/nfc/
- https://www.androidauthority.com/what-is-nfc-270730/
- https://nfc-forum.org/what-is-nfc/
- https://whatwebcando.today/nfc.html
- https://caniuse.com/webnfc
- https://w3c.github.io/web-nfc/

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
## Available Scripts

### Advanced Configuration
In the project directory, you can run:

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### `yarn start`

### Deployment
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
The page will reload if you make edits.\
You will also see any lint errors in the console.

### `yarn build` fails to minify
### `yarn build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
10 changes: 10 additions & 0 deletions src/components/Writer/Writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React, { useCallback, useEffect } from 'react';

const Writer = () => {

return (
<></>
);
};

export default Writer;
4 changes: 2 additions & 2 deletions src/containers/Write.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const Write = () => {
const onWrite = useCallback(async() => {
try {
const ndef = new window.NDEFReader();
await ndef.write({records: [{ recordType: "text", data: "18" }]});
alert(`${18} saved!`);
await ndef.write({records: [{ recordType: "text", data: "Hello World!" }]});
alert(`Value Saved!`);
} catch (error) {
console.log(error);
}
Expand Down

0 comments on commit 1b836e4

Please sign in to comment.