Class: NFC

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nfc.rb,
lib/nfc/device.rb,
lib/nfc/felica.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, Felica, ISO14443A

Constant Summary collapse

VERSION =
'2.1.0'

Instance Method Summary collapse

Constructor Details

#initializeNFC

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

NFC.instance


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

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

Instance Method Details

#activate_fieldObject

Activate the detection field



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

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

#crc=(value) ⇒ Object

Do CRC checks



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

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

#deactivate_fieldObject

Deactivate the detection field



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

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

#deselectObject

Deselect a tag



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

def deselect
  device.deselect
end

#deviceObject

Get the device



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

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)


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

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



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

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

#parity=(v) ⇒ Object

Parity checks



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

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

#selectObject Also known as: detect

Select a tag



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

def select
  device.select Device::IM_ISO14443A_106
end