Class: NFC
- Inherits:
-
Object
- Object
- NFC
- 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
Constant Summary collapse
- VERSION =
'2.0.1'
Instance Method Summary collapse
-
#activate_field ⇒ Object
Activate the detection field.
-
#crc=(value) ⇒ Object
Do CRC checks.
-
#deactivate_field ⇒ Object
Deactivate the detection field.
-
#deselect ⇒ Object
Deselect a tag.
-
#device ⇒ Object
Get the device.
-
#find {|tag| ... } ⇒ Object
Read your tag and print the info.
-
#infinite_list_passive=(v) ⇒ Object
Block until a passive tag is detected.
-
#initialize ⇒ NFC
constructor
Create a new NFC class.
-
#parity=(v) ⇒ Object
Parity checks.
-
#select ⇒ Object
(also: #detect)
Select a tag.
Constructor Details
Instance Method Details
#activate_field ⇒ Object
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_field ⇒ Object
Deactivate the detection field
26 27 28 |
# File 'lib/nfc.rb', line 26 def deactivate_field device.configure Device::DCO_ACTIVATE_FIELD, 0 end |
#deselect ⇒ Object
Deselect a tag
69 70 71 |
# File 'lib/nfc.rb', line 69 def deselect device.deselect end |
#device ⇒ Object
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
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 |
#select ⇒ Object 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 |