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:
- adding
cordova plugin add phonegap-nfc
to the Field Kit app (this adds a line topackage.json
which 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
mounted
a Tag listener is created vianfc.addTagDiscoveredListener()
. After this, NFC tags are registered within the app, rather than the Android System Default. - In the NFC view
beforeDestroy
nfc.remoteTagDiscoveredListener()
removes the listener, and NFC events are handled by the system, not Field Kit. - Also, a good reference on Cordova NFC: Writing NFC Apps with Apache Cordova
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.
-
nfc.beginSession(success, failure);
I first tried using the Web NFC standard (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 (Web NFC API) 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 theweb-nfc
standard andphonegap-nfc
library) - We need to brain storm how the NFC
listeners
will be structured in Field Kit core API - perhaps multiplelisteners
can be registered, so any Field Module that registers a listener could be used. Some info from thephonegap-nfc
library:
Multiple listeners can be registered in JavaScript. e.g. addNdefListener, addTagDiscoveredListener, addMimeTypeListener.
…
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
(GitHub - chariotsolutions/phonegap-nfc: PhoneGap NFC Plugin) - Theoretically, tags could be written with a custom mimetime ofapplication/farm_asset
orapplication/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
asset
tag is read, theasset
should 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
asset
orarea
tag is created - Create/read tags for managing
inventories
- 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.