main.go 1.3 KB
Newer Older
qwelp's avatar
qwelp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/fuzxxl/nfc"
	"github.com/taglme/string2keyboard"
)

func main() {
	var devices []string
	var err error
	var reader nfc.Device
	var information string
	var nfcTarget *nfc.ISO14443aTarget
	var target []nfc.Target
	var uid string

	res := nfc.Version()
	fmt.Printf("Using libnfc version: %s\n", res)
	devices, err = nfc.ListDevices()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Found devices: \n %s", devices[0])
	reader, err = nfc.Open(devices[0])
	if err != nil {
		log.Fatal(err)
	}
	information, err = reader.Information()

	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("\nInformation about reader: %s\n", information)
	modul := nfc.Modulation{nfc.ISO14443a, nfc.Nbr106}

	for {

		fmt.Printf("Wait for tag ...\n")
		for {

			target, err = reader.InitiatorListPassiveTargets(modul)
			if err != nil {
				log.Fatal(err)
			}
			if len(target) == 0 {
				time.Sleep(100 * time.Millisecond)
			} else {
				break
			}

		}
		nfcTarget = target[0].(*nfc.ISO14443aTarget)
		uid = string(nfcTarget.UID[:nfcTarget.UIDLen])
		uidS := fmt.Sprintf("%x", uid)
		fmt.Printf(" Tag UID is: %s\n", uidS)
		fmt.Printf("Writting as keyboard input...")
		string2keyboard.KeyboardWrite(uidS)
		fmt.Printf("Done.\n")

	}

	err = reader.Close()
	if err != nil {
		log.Fatal(err)
	}

}