Class: NFC

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nfc.rb,
lib/nfc/device.rb,
lib/nfc/iso14443a.rb,
ext/nfc/nfc.c

Overview

NFC is a class for dealing with Near Field Communication systems. This library will read RFID tags from an RFID reader. You should start by reading NFC#find

Defined Under Namespace

Classes: Device, ISO14443A

Constant Summary collapse

VERSION =
'2.0.1'

Instance Method Summary collapse

Constructor Details

#initializeNFC

Create a new NFC class. This is private, do this instead:

NFC.instance


19
20
21
22
# File 'lib/nfc.rb', line 19

def initialize
  @device = nil
  @mutex = Mutex.new
end

Instance Method Details

#activate_fieldObject

Activate the detection field



32
33
34
# File 'lib/nfc.rb', line 32

def activate_field
  device.configure Device::DCO_ACTIVATE_FIELD, 1
end

#crc=(value) ⇒ Object

Do CRC checks



38
39
40
# File 'lib/nfc.rb', line 38

def crc= value
  device.configure Device::DCO_HANDLE_CRC, value ? 1 : 0
end

#deactivate_fieldObject

Deactivate the detection field



26
27
28
# File 'lib/nfc.rb', line 26

def deactivate_field
  device.configure Device::DCO_ACTIVATE_FIELD, 0
end

#deselectObject

Deselect a tag



69
70
71
# File 'lib/nfc.rb', line 69

def deselect
  device.deselect
end

#deviceObject

Get the device



50
51
52
# File 'lib/nfc.rb', line 50

def device
  @device ||= NFC::Device.connect
end

#find {|tag| ... } ⇒ Object

Read your tag and print the info.

p NFC.instance.find

NFC#find will return immidiately, which means you should have a tag sitting on the reader when running it. If you’d like it to block until it detects a tag, give find a block like so:

NFC.instance.find do |tag|
  p tag
end

You can even run in an infinite loop if you’d like to continually find tags:

loop do
  NFC.instance.find do |tag|
    p tag
  end
end

Yields:

  • (tag)


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nfc.rb', line 93

def find
  @mutex.lock
  deactivate_field
  self.infinite_list_passive = block_given?
  self.crc = true
  self.parity = true
  activate_field
  tag = detect
  deselect
  @mutex.unlock
  yield tag if block_given?
  tag
end

#infinite_list_passive=(v) ⇒ Object

Block until a passive tag is detected



56
57
58
# File 'lib/nfc.rb', line 56

def infinite_list_passive= v
  device.configure Device::DCO_INFINITE_LIST_PASSIVE, v ? 1 : 0
end

#parity=(v) ⇒ Object

Parity checks



44
45
46
# File 'lib/nfc.rb', line 44

def parity= v
  device.configure Device::DCO_HANDLE_PARITY, v ? 1 : 0
end

#selectObject Also known as: detect

Select a tag



62
63
64
# File 'lib/nfc.rb', line 62

def select
  device.select Device::IM_ISO14443A_106
end