Wanted to start a forum post for talking about NFC in Field Kit.
See other relevant issues:
- @mstenta you said there are some other issues you could link here?
After some quick hacking with Field Kit, I’m excited to say I got some initial support for reading NFC tags in the native farmOS field kit app! I accomplished this with the following:
cordova plugin add phonegap-nfcto the Field Kit app (this adds a line to
package.jsonwhich includes the phonegap-nfc library
- Created an “NFC” Field Module" with a main “view” that largely resembles this Cordova NFC Sample: https://github.com/c4software/vuejs-cordova-sample/blob/master/src/views/Nfc.vue
- When the NFC view is
mounteda Tag listener is created via
nfc.addTagDiscoveredListener(). After this, NFC tags are registered within the app, rather than the Android System Default.
- In the NFC view
nfc.remoteTagDiscoveredListener()removes the listener, and NFC events are handled by the system, not Field Kit.
- Also, a good reference on Cordova NFC: https://don.github.io/slides/2014-04-07-apachecon-nfc/
The phonegap-nfc library should support Android and iOS, although I haven’t tested on iOS. Notably, it looks like one additional step is required in the code: (
iOS requires you to begin a session before scanning a NFC tag.
I first tried using the Web NFC standard (https://w3c.github.io/web-nfc/) but didn’t have any luck. As far as I can tell, this is only supported in Chrome and requires the
#enable-webnfc flag which is
disabled by default. Originally I thought the NFC web standard was not being supported (https://www.w3.org/TR/nfc/) but the link above and the Web-NFC GitHub show activity as of yesterday. This is promising and should be revisited to potentially bring support for NFC in the PWA version of Field Kit.
Some considerations for further development of NFC for Field Kit:
- NFC support in Field Kit core would be idea. With this, tags could be “read” at anytime the app is open (and potentially trigger the opening of the Field Kit native app).
- Creating a Field Kit NFC API could allow Field Modules to interface with NFC to read/write to tags themselves. Having an internal NFC API might simplify support for future
web-nfc, too, because Field Kit core could detect 1) if it is running as PWA or Native and 2) detect if the device hardware supports NFC (via the
- We need to brain storm how the NFC
listenerswill be structured in Field Kit core API - perhaps multiple
listenerscan be registered, so any Field Module that registers a listener could be used. Some info from the
On Android, only the most specific event will fire. If a Mime Media Tag is scanned, only the addMimeTypeListener callback is called and not the callback defined in addNdefListener. You can use the same event handler for multiple listeners.
- This brings up the
nfc.addMimeTypeListener(https://github.com/chariotsolutions/phonegap-nfc#nfcaddmimetypelistener) - Theoretically, tags could be written with a custom mimetime of
application/farm_area, and be used to trigger specific mime-type listeners. This seems promising to me!
There also needs to be some thought into the UI
- @jgaehring had an interesting idea where, when an NFC tag is read, a popup could display that allows the user to select how to handle the event - i.e. “View Asset”, “Create Observation for ___”, etc… and Field Modules could register these actions.
- Other use cases might be more “background” where, when editing a log, if an
assettag is read, the
assetshould automatically be added to the log. Something similar could be done with areas.
- There will need to be an interface for writing to NFC tags. Perhaps a core interface that allows writing standard farmOS mimetypes, as well as allowing Field Modules to write custom tags.
Use cases (feel free to add!!)
- Create/read tags for farmOS assets (animals, equipment, etc)
- Auto-add assets to logs
- Create/read tags for farmOS areas
- Auto-add areas to logs
- An “action” to “Create Observation Log” when an
areatag is created
- Create/read tags for managing
- An “action” to auto increment/decrement an inventory when a tag is read
- An “action” to “Clock In” on an employee timesheet when a tag is read
- … (please, add other ideas!)
In the meantime, I’m going to continue working on a draft NFC Field Module to explore this more. We specifically need to play with writing NFC tags and see if the mime-types will work as expected. I’ll publish my fork once I’m further along for development reference.