Last active
November 2, 2017 22:58
-
-
Save henrycjc/c1632b2d1f210ae0ff33d860c7c2eb8f to your computer and use it in GitHub Desktop.
NFC bulk encoder script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nfc | |
import ndef | |
tags = set() | |
rec = ndef.UriRecord("https://google.com") | |
def on_connect(tag): | |
if tag.identifier not in tags: | |
tags.add(tag.identifier) | |
fmt = tag.format() | |
if fmt is None: | |
print("Tag cannot be formatted (not supported).") | |
elif fmt is False: | |
print("Tag failed to be formatted (for some reason).") | |
else: | |
tag.ndef.records = [rec] | |
if __name__ == "__main__": | |
clf = nfc.ContactlessFrontend() | |
if not clf.open('usb'): | |
raise RuntimeError("Failed to open NFC device.") | |
while True: | |
config = { | |
'interval': 0.35, | |
'on-connect': on_connect | |
} | |
ret = clf.connect(rdwr=config) | |
if ret is None: | |
pass | |
elif not ret: | |
print ("NFC connection terminated due to an exception.") | |
break | |
else: | |
pass | |
clf.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment