Class: NFC::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-nfc/tags/tag.rb

Direct Known Subclasses

IsoDep::Tag, Mifare::Tag

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, reader) ⇒ Tag

Returns a new instance of Tag.



3
4
5
6
7
# File 'lib/ruby-nfc/tags/tag.rb', line 3

def initialize(target, reader)
	@target = target
	@reader = reader
	@processed = false
end

Class Method Details

.match?(target) ⇒ Boolean

Matches any NFC tag

Returns:

  • (Boolean)


58
59
60
# File 'lib/ruby-nfc/tags/tag.rb', line 58

def self.match?(target)
	true
end

Instance Method Details

#connect(&block) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/ruby-nfc/tags/tag.rb', line 9

def connect(&block)
	if block_given?
		begin
			self.instance_eval(&block)
		ensure
			disconnect
		end
	end
end

#disconnectObject



42
# File 'lib/ruby-nfc/tags/tag.rb', line 42

def disconnect; end

#present?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby-nfc/tags/tag.rb', line 27

def present?
  modulation = LibNFC::Modulation.new
			modulation[:nmt] = :NMT_ISO14443A 
			modulation[:nbr] = :NBR_106

  ptr = FFI::MemoryPointer.new(:char, @target[:nti][:nai][:szUidLen])
  ptr.put_bytes(0, uid)

 res = LibNFC.nfc_initiator_select_passive_target(@reader.ptr, modulation, ptr,
                                                  @target[:nti][:nai][:szUidLen],
                                                  FFI::Pointer::NULL)

 return res >= 1
end

#processed!Object



19
20
21
# File 'lib/ruby-nfc/tags/tag.rb', line 19

def processed!
	@target.processed!
end

#processed?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ruby-nfc/tags/tag.rb', line 23

def processed?
	@target.processed?
end

#to_sObject



53
54
55
# File 'lib/ruby-nfc/tags/tag.rb', line 53

def to_s
	uid_hex
end

#uidObject



44
45
46
47
# File 'lib/ruby-nfc/tags/tag.rb', line 44

def uid
	uid_size = @target[:nti][:nai][:szUidLen]
	@target[:nti][:nai][:abtUid].to_s[0...uid_size]
end

#uid_hexObject



49
50
51
# File 'lib/ruby-nfc/tags/tag.rb', line 49

def uid_hex
	uid.unpack('H*').pop
end