Class: Tinkerforge::BrickletNFC

Inherits:
Device
  • Object
show all
Defined in:
lib/tinkerforge/bricklet_nfc.rb

Overview

NFC tag read/write, NFC P2P and Card Emulation

Constant Summary collapse

DEVICE_IDENTIFIER =

:nodoc:

286
DEVICE_DISPLAY_NAME =

:nodoc:

'NFC Bricklet'
CALLBACK_READER_STATE_CHANGED =

This callback is called if the reader state of the NFC Bricklet changes. See BrickletNFC#reader_get_state for more information about the possible states.

13
CALLBACK_CARDEMU_STATE_CHANGED =

This callback is called if the cardemu state of the NFC Bricklet changes. See BrickletNFC#cardemu_get_state for more information about the possible states.

18
CALLBACK_P2P_STATE_CHANGED =

This callback is called if the P2P state of the NFC Bricklet changes. See BrickletNFC#p2p_get_state for more information about the possible states.

24
FUNCTION_SET_MODE =

:nodoc:

1
FUNCTION_GET_MODE =

:nodoc:

2
FUNCTION_READER_REQUEST_TAG_ID =

:nodoc:

3
FUNCTION_READER_GET_TAG_ID_LOW_LEVEL =

:nodoc:

4
FUNCTION_READER_GET_STATE =

:nodoc:

5
FUNCTION_READER_WRITE_NDEF_LOW_LEVEL =

:nodoc:

6
FUNCTION_READER_REQUEST_NDEF =

:nodoc:

7
FUNCTION_READER_READ_NDEF_LOW_LEVEL =

:nodoc:

8
FUNCTION_READER_AUTHENTICATE_MIFARE_CLASSIC_PAGE =

:nodoc:

9
FUNCTION_READER_WRITE_PAGE_LOW_LEVEL =

:nodoc:

10
FUNCTION_READER_REQUEST_PAGE =

:nodoc:

11
FUNCTION_READER_READ_PAGE_LOW_LEVEL =

:nodoc:

12
FUNCTION_CARDEMU_GET_STATE =

:nodoc:

14
FUNCTION_CARDEMU_START_DISCOVERY =

:nodoc:

15
FUNCTION_CARDEMU_WRITE_NDEF_LOW_LEVEL =

:nodoc:

16
FUNCTION_CARDEMU_START_TRANSFER =

:nodoc:

17
FUNCTION_P2P_GET_STATE =

:nodoc:

19
FUNCTION_P2P_START_DISCOVERY =

:nodoc:

20
FUNCTION_P2P_WRITE_NDEF_LOW_LEVEL =

:nodoc:

21
FUNCTION_P2P_START_TRANSFER =

:nodoc:

22
FUNCTION_P2P_READ_NDEF_LOW_LEVEL =

:nodoc:

23
FUNCTION_SET_DETECTION_LED_CONFIG =

:nodoc:

25
FUNCTION_GET_DETECTION_LED_CONFIG =

:nodoc:

26
FUNCTION_SET_MAXIMUM_TIMEOUT =

:nodoc:

27
FUNCTION_GET_MAXIMUM_TIMEOUT =

:nodoc:

28
FUNCTION_SIMPLE_GET_TAG_ID_LOW_LEVEL =

:nodoc:

29
FUNCTION_GET_SPITFP_ERROR_COUNT =

:nodoc:

234
FUNCTION_SET_BOOTLOADER_MODE =

:nodoc:

235
FUNCTION_GET_BOOTLOADER_MODE =

:nodoc:

236
FUNCTION_SET_WRITE_FIRMWARE_POINTER =

:nodoc:

237
FUNCTION_WRITE_FIRMWARE =

:nodoc:

238
FUNCTION_SET_STATUS_LED_CONFIG =

:nodoc:

239
FUNCTION_GET_STATUS_LED_CONFIG =

:nodoc:

240
FUNCTION_GET_CHIP_TEMPERATURE =

:nodoc:

242
FUNCTION_RESET =

:nodoc:

243
FUNCTION_WRITE_UID =

:nodoc:

248
FUNCTION_READ_UID =

:nodoc:

249
FUNCTION_GET_IDENTITY =

:nodoc:

255
MODE_OFF =

:nodoc:

0
MODE_CARDEMU =

:nodoc:

1
MODE_P2P =

:nodoc:

2
MODE_READER =

:nodoc:

3
MODE_SIMPLE =

:nodoc:

4
TAG_TYPE_MIFARE_CLASSIC =

:nodoc:

0
TAG_TYPE_TYPE1 =

:nodoc:

1
TAG_TYPE_TYPE2 =

:nodoc:

2
TAG_TYPE_TYPE3 =

:nodoc:

3
TAG_TYPE_TYPE4 =

:nodoc:

4
READER_STATE_INITIALIZATION =

:nodoc:

0
READER_STATE_IDLE =

:nodoc:

128
READER_STATE_ERROR =

:nodoc:

192
READER_STATE_REQUEST_TAG_ID =

:nodoc:

2
READER_STATE_REQUEST_TAG_ID_READY =

:nodoc:

130
READER_STATE_REQUEST_TAG_ID_ERROR =

:nodoc:

194
READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE =

:nodoc:

3
READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_READY =

:nodoc:

131
READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_ERROR =

:nodoc:

195
READER_STATE_WRITE_PAGE =

:nodoc:

4
READER_STATE_WRITE_PAGE_READY =

:nodoc:

132
READER_STATE_WRITE_PAGE_ERROR =

:nodoc:

196
READER_STATE_REQUEST_PAGE =

:nodoc:

5
READER_STATE_REQUEST_PAGE_READY =

:nodoc:

133
READER_STATE_REQUEST_PAGE_ERROR =

:nodoc:

197
READER_STATE_WRITE_NDEF =

:nodoc:

6
READER_STATE_WRITE_NDEF_READY =

:nodoc:

134
READER_STATE_WRITE_NDEF_ERROR =

:nodoc:

198
READER_STATE_REQUEST_NDEF =

:nodoc:

7
READER_STATE_REQUEST_NDEF_READY =

:nodoc:

135
READER_STATE_REQUEST_NDEF_ERROR =

:nodoc:

199
KEY_A =

:nodoc:

0
KEY_B =

:nodoc:

1
READER_WRITE_TYPE4_CAPABILITY_CONTAINER =

:nodoc:

3
READER_WRITE_TYPE4_NDEF =

:nodoc:

4
READER_REQUEST_TYPE4_CAPABILITY_CONTAINER =

:nodoc:

3
READER_REQUEST_TYPE4_NDEF =

:nodoc:

4
CARDEMU_STATE_INITIALIZATION =

:nodoc:

0
CARDEMU_STATE_IDLE =

:nodoc:

128
CARDEMU_STATE_ERROR =

:nodoc:

192
CARDEMU_STATE_DISCOVER =

:nodoc:

2
CARDEMU_STATE_DISCOVER_READY =

:nodoc:

130
CARDEMU_STATE_DISCOVER_ERROR =

:nodoc:

194
CARDEMU_STATE_TRANSFER_NDEF =

:nodoc:

3
CARDEMU_STATE_TRANSFER_NDEF_READY =

:nodoc:

131
CARDEMU_STATE_TRANSFER_NDEF_ERROR =

:nodoc:

195
CARDEMU_TRANSFER_ABORT =

:nodoc:

0
CARDEMU_TRANSFER_WRITE =

:nodoc:

1
P2P_STATE_INITIALIZATION =

:nodoc:

0
P2P_STATE_IDLE =

:nodoc:

128
P2P_STATE_ERROR =

:nodoc:

192
P2P_STATE_DISCOVER =

:nodoc:

2
P2P_STATE_DISCOVER_READY =

:nodoc:

130
P2P_STATE_DISCOVER_ERROR =

:nodoc:

194
P2P_STATE_TRANSFER_NDEF =

:nodoc:

3
P2P_STATE_TRANSFER_NDEF_READY =

:nodoc:

131
P2P_STATE_TRANSFER_NDEF_ERROR =

:nodoc:

195
P2P_TRANSFER_ABORT =

:nodoc:

0
P2P_TRANSFER_WRITE =

:nodoc:

1
P2P_TRANSFER_READ =

:nodoc:

2
DETECTION_LED_CONFIG_OFF =

:nodoc:

0
DETECTION_LED_CONFIG_ON =

:nodoc:

1
DETECTION_LED_CONFIG_SHOW_HEARTBEAT =

:nodoc:

2
DETECTION_LED_CONFIG_SHOW_DETECTION =

:nodoc:

3
BOOTLOADER_MODE_BOOTLOADER =

:nodoc:

0
BOOTLOADER_MODE_FIRMWARE =

:nodoc:

1
BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT =

:nodoc:

2
BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT =

:nodoc:

3
BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT =

:nodoc:

4
BOOTLOADER_STATUS_OK =

:nodoc:

0
BOOTLOADER_STATUS_INVALID_MODE =

:nodoc:

1
BOOTLOADER_STATUS_NO_CHANGE =

:nodoc:

2
BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT =

:nodoc:

3
BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT =

:nodoc:

4
BOOTLOADER_STATUS_CRC_MISMATCH =

:nodoc:

5
STATUS_LED_CONFIG_OFF =

:nodoc:

0
STATUS_LED_CONFIG_ON =

:nodoc:

1
STATUS_LED_CONFIG_SHOW_HEARTBEAT =

:nodoc:

2
STATUS_LED_CONFIG_SHOW_STATUS =

:nodoc:

3

Constants inherited from Device

Device::DEVICE_IDENTIFIER_CHECK_MATCH, Device::DEVICE_IDENTIFIER_CHECK_MISMATCH, Device::DEVICE_IDENTIFIER_CHECK_PENDING, Device::RESPONSE_EXPECTED_ALWAYS_TRUE, Device::RESPONSE_EXPECTED_FALSE, Device::RESPONSE_EXPECTED_INVALID_FUNCTION_ID, Device::RESPONSE_EXPECTED_TRUE

Instance Attribute Summary

Attributes inherited from Device

#callback_formats, #expected_response_function_id, #expected_response_sequence_number, #high_level_callbacks, #registered_callbacks, #replaced, #uid

Instance Method Summary collapse

Methods inherited from Device

#check_validity, #dequeue_response, #enqueue_response, #get_api_version, #get_response_expected, #send_request, #set_response_expected, #set_response_expected_all

Constructor Details

#initialize(uid, ipcon) ⇒ BrickletNFC

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/tinkerforge/bricklet_nfc.rb', line 153

def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 2]

  @response_expected[FUNCTION_SET_MODE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_READER_REQUEST_TAG_ID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READER_GET_TAG_ID_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_READER_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_READER_WRITE_NDEF_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_READER_REQUEST_NDEF] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READER_READ_NDEF_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_READER_AUTHENTICATE_MIFARE_CLASSIC_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READER_WRITE_PAGE_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_READER_REQUEST_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READER_READ_PAGE_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_CARDEMU_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_CARDEMU_START_DISCOVERY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_CARDEMU_WRITE_NDEF_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_CARDEMU_START_TRANSFER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_P2P_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_P2P_START_DISCOVERY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_P2P_WRITE_NDEF_LOW_LEVEL] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_P2P_START_TRANSFER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_P2P_READ_NDEF_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_DETECTION_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_DETECTION_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_MAXIMUM_TIMEOUT] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_MAXIMUM_TIMEOUT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SIMPLE_GET_TAG_ID_LOW_LEVEL] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_SPITFP_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_WRITE_FIRMWARE_POINTER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_FIRMWARE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_CHIP_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_RESET] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_UID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READ_UID] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_READER_STATE_CHANGED] = [10, 'C ?']
  @callback_formats[CALLBACK_CARDEMU_STATE_CHANGED] = [10, 'C ?']
  @callback_formats[CALLBACK_P2P_STATE_CHANGED] = [10, 'C ?']

  @ipcon.add_device self
end

Instance Method Details

#cardemu_get_stateObject

Returns the current cardemu state of the NFC Bricklet.

On startup the Bricklet will be in the CardemuInitialization state. The initialization will only take about 20ms. After that it changes to CardemuIdle.

The Bricklet is also reinitialized if the mode is changed, see BrickletNFC#set_mode.

The functions of this Bricklet can be called in the CardemuIdle state and all of the CardemuReady and CardemuError states.

Example: If you call BrickletNFC#cardemu_start_discovery, the state will change to CardemuDiscover until the discovery is finished. Then it will change to either CardemuDiscoverReady if it worked or to CardemuDiscoverError if it didn’t.

The same approach is used analogously for the other API functions.



457
458
459
460
461
# File 'lib/tinkerforge/bricklet_nfc.rb', line 457

def cardemu_get_state
  check_validity

  send_request FUNCTION_CARDEMU_GET_STATE, [], '', 10, 'C ?'
end

#cardemu_start_discoveryObject

Starts the discovery process. If you call this function while a NFC reader device is near to the NFC Bricklet the state will change from CardemuDiscovery to CardemuDiscoveryReady.

If no NFC reader device can be found or if there is an error during discovery the cardemu state will change to CardemuDiscoveryError. In this case you have to restart the discovery process.

If the cardemu state changes to CardemuDiscoveryReady you can start the NDEF message transfer with BrickletNFC#cardemu_write_ndef and BrickletNFC#cardemu_start_transfer.



473
474
475
476
477
# File 'lib/tinkerforge/bricklet_nfc.rb', line 473

def cardemu_start_discovery
  check_validity

  send_request FUNCTION_CARDEMU_START_DISCOVERY, [], '', 8, ''
end

#cardemu_start_transfer(transfer) ⇒ Object

You can start the transfer of a NDEF message if the cardemu state is CardemuDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via BrickletNFC#cardemu_write_ndef first.

After you call this function the state will change to CardemuTransferNDEF. It will change to CardemuTransferNDEFReady if the transfer was successful or CardemuTransferNDEFError if it wasn’t.



500
501
502
503
504
# File 'lib/tinkerforge/bricklet_nfc.rb', line 500

def cardemu_start_transfer(transfer)
  check_validity

  send_request FUNCTION_CARDEMU_START_TRANSFER, [transfer], 'C', 8, ''
end

#cardemu_write_ndef(ndef) ⇒ Object

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size in Cardemu mode is 255 byte.

You can call this function at any time in Cardemu mode. The internal buffer will not be overwritten until you call this function again or change the mode.



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
# File 'lib/tinkerforge/bricklet_nfc.rb', line 983

def cardemu_write_ndef(ndef)
  if ndef.length > 65535
    raise ArgumentError, 'NDEF can be at most 65535 items long'
  end

  ndef_length = ndef.length
  ndef_chunk_offset = 0

  if ndef_length == 0
    ndef_chunk_data = [0] * 60
    ret = cardemu_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
  else
    ret = nil # assigned in block

    @stream_mutex.synchronize {
      while ndef_chunk_offset < ndef_length
        ndef_chunk_data = ndef[ndef_chunk_offset, 60]

        if ndef_chunk_data.length < 60
          ndef_chunk_data += [0] * (60 - ndef_chunk_data.length)
        end

        ret = cardemu_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
        ndef_chunk_offset += 60
      end
    }
  end

  ret
end

#cardemu_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data) ⇒ Object

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size in Cardemu mode is 255 byte.

You can call this function at any time in Cardemu mode. The internal buffer will not be overwritten until you call this function again or change the mode.



486
487
488
489
490
# File 'lib/tinkerforge/bricklet_nfc.rb', line 486

def cardemu_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data)
  check_validity

  send_request FUNCTION_CARDEMU_WRITE_NDEF_LOW_LEVEL, [ndef_length, ndef_chunk_offset, ndef_chunk_data], 'S S C60', 8, ''
end

#get_bootloader_modeObject

Returns the current bootloader mode, see BrickletNFC#set_bootloader_mode.



678
679
680
681
682
# File 'lib/tinkerforge/bricklet_nfc.rb', line 678

def get_bootloader_mode
  check_validity

  send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C'
end

#get_chip_temperatureObject

Returns the temperature as measured inside the microcontroller. The value returned is not the ambient temperature!

The temperature is only proportional to the real temperature and it has bad accuracy. Practically it is only useful as an indicator for temperature changes.



736
737
738
739
740
# File 'lib/tinkerforge/bricklet_nfc.rb', line 736

def get_chip_temperature
  check_validity

  send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's'
end

#get_detection_led_configObject

Returns the configuration as set by BrickletNFC#set_detection_led_config



598
599
600
601
602
# File 'lib/tinkerforge/bricklet_nfc.rb', line 598

def get_detection_led_config
  check_validity

  send_request FUNCTION_GET_DETECTION_LED_CONFIG, [], '', 9, 'C'
end

#get_identityObject

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’ or ‘h’ (Bricklet Port). A Bricklet connected to an :ref:‘Isolator Bricklet <isolator_bricklet>` is always at position ’z’.

The device identifier numbers can be found :ref:‘here <device_identifier>`. |device_identifier_constant|



783
784
785
# File 'lib/tinkerforge/bricklet_nfc.rb', line 783

def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end

#get_maximum_timeoutObject

Returns the timeout as set by BrickletNFC#set_maximum_timeout

.. versionadded

2.0.1$nbsp;(Plugin)



632
633
634
635
636
# File 'lib/tinkerforge/bricklet_nfc.rb', line 632

def get_maximum_timeout
  check_validity

  send_request FUNCTION_GET_MAXIMUM_TIMEOUT, [], '', 10, 'S'
end

#get_modeObject

Returns the mode as set by BrickletNFC#set_mode.



222
223
224
225
226
# File 'lib/tinkerforge/bricklet_nfc.rb', line 222

def get_mode
  check_validity

  send_request FUNCTION_GET_MODE, [], '', 9, 'C'
end

#get_spitfp_error_countObject

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,

  • message checksum errors,

  • framing errors and

  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.



656
657
658
659
660
# File 'lib/tinkerforge/bricklet_nfc.rb', line 656

def get_spitfp_error_count
  check_validity

  send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L'
end

#get_status_led_configObject

Returns the configuration as set by BrickletNFC#set_status_led_config



724
725
726
727
728
# File 'lib/tinkerforge/bricklet_nfc.rb', line 724

def get_status_led_config
  check_validity

  send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C'
end

#p2p_get_stateObject

Returns the current P2P state of the NFC Bricklet.

On startup the Bricklet will be in the P2PInitialization state. The initialization will only take about 20ms. After that it changes to P2PIdle.

The Bricklet is also reinitialized if the mode is changed, see BrickletNFC#set_mode.

The functions of this Bricklet can be called in the P2PIdle state and all of the P2PReady and P2PError states.

Example: If you call BrickletNFC#p2p_start_discovery, the state will change to P2PDiscover until the discovery is finished. Then it will change to either P2PDiscoverReady* if it worked or to P2PDiscoverError if it didn’t.

The same approach is used analogously for the other API functions.



522
523
524
525
526
# File 'lib/tinkerforge/bricklet_nfc.rb', line 522

def p2p_get_state
  check_validity

  send_request FUNCTION_P2P_GET_STATE, [], '', 10, 'C ?'
end

#p2p_read_ndefObject

Returns the NDEF message that was written by a NFC peer in NFC P2P mode.

The NDEF message is ready if you called BrickletNFC#p2p_start_transfer with a read transfer and the P2P state changed to P2PTransferNDEFReady.



1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
# File 'lib/tinkerforge/bricklet_nfc.rb', line 1056

def p2p_read_ndef
  ndef_length = nil # assigned in block
  ndef_data = nil # assigned in block

  @stream_mutex.synchronize {
    ret = p2p_read_ndef_low_level
    ndef_length = ret[0]
    ndef_chunk_offset = ret[1]
    ndef_out_of_sync = ndef_chunk_offset != 0
    ndef_data = ret[2]

    while not ndef_out_of_sync and ndef_data.length < ndef_length
      ret = p2p_read_ndef_low_level
      ndef_length = ret[0]
      ndef_chunk_offset = ret[1]
      ndef_out_of_sync = ndef_chunk_offset != ndef_data.length
      ndef_data += ret[2]
    end

    if ndef_out_of_sync # discard remaining stream to bring it back in-sync
      while ndef_chunk_offset + 60 < ndef_length
        ret = p2p_read_ndef_low_level
        ndef_length = ret[0]
        ndef_chunk_offset = ret[1]
      end

      raise StreamOutOfSyncException, 'NDEF stream is out-of-sync'
    end
  }

  ndef_data[0, ndef_length]
end

#p2p_read_ndef_low_levelObject

Returns the NDEF message that was written by a NFC peer in NFC P2P mode.

The NDEF message is ready if you called BrickletNFC#p2p_start_transfer with a read transfer and the P2P state changed to P2PTransferNDEFReady.



579
580
581
582
583
# File 'lib/tinkerforge/bricklet_nfc.rb', line 579

def p2p_read_ndef_low_level
  check_validity

  send_request FUNCTION_P2P_READ_NDEF_LOW_LEVEL, [], '', 72, 'S S C60'
end

#p2p_start_discoveryObject

Starts the discovery process. If you call this function while another NFC P2P enabled device is near to the NFC Bricklet the state will change from P2PDiscovery to P2PDiscoveryReady.

If no NFC P2P enabled device can be found or if there is an error during discovery the P2P state will change to P2PDiscoveryError. In this case you have to restart the discovery process.

If the P2P state changes to P2PDiscoveryReady you can start the NDEF message transfer with BrickletNFC#p2p_start_transfer.



538
539
540
541
542
# File 'lib/tinkerforge/bricklet_nfc.rb', line 538

def p2p_start_discovery
  check_validity

  send_request FUNCTION_P2P_START_DISCOVERY, [], '', 8, ''
end

#p2p_start_transfer(transfer) ⇒ Object

You can start the transfer of a NDEF message if the P2P state is P2PDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via BrickletNFC#p2p_write_ndef first.

After you call this function the P2P state will change to P2PTransferNDEF. It will change to P2PTransferNDEFReady if the transfer was successfull or P2PTransferNDEFError if it wasn’t.

If you started a write transfer you are now done. If you started a read transfer you can now use BrickletNFC#p2p_read_ndef to read the NDEF message that was written by the NFC peer.



569
570
571
572
573
# File 'lib/tinkerforge/bricklet_nfc.rb', line 569

def p2p_start_transfer(transfer)
  check_validity

  send_request FUNCTION_P2P_START_TRANSFER, [transfer], 'C', 8, ''
end

#p2p_write_ndef(ndef) ⇒ Object

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size for P2P transfer is 255 byte.

You can call this function at any time in P2P mode. The internal buffer will not be overwritten until you call this function again, change the mode or use P2P to read an NDEF messages.



1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
# File 'lib/tinkerforge/bricklet_nfc.rb', line 1021

def p2p_write_ndef(ndef)
  if ndef.length > 65535
    raise ArgumentError, 'NDEF can be at most 65535 items long'
  end

  ndef_length = ndef.length
  ndef_chunk_offset = 0

  if ndef_length == 0
    ndef_chunk_data = [0] * 60
    ret = p2p_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
  else
    ret = nil # assigned in block

    @stream_mutex.synchronize {
      while ndef_chunk_offset < ndef_length
        ndef_chunk_data = ndef[ndef_chunk_offset, 60]

        if ndef_chunk_data.length < 60
          ndef_chunk_data += [0] * (60 - ndef_chunk_data.length)
        end

        ret = p2p_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
        ndef_chunk_offset += 60
      end
    }
  end

  ret
end

#p2p_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data) ⇒ Object

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size for P2P transfer is 255 byte.

You can call this function at any time in P2P mode. The internal buffer will not be overwritten until you call this function again, change the mode or use P2P to read an NDEF messages.



551
552
553
554
555
# File 'lib/tinkerforge/bricklet_nfc.rb', line 551

def p2p_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data)
  check_validity

  send_request FUNCTION_P2P_WRITE_NDEF_LOW_LEVEL, [ndef_length, ndef_chunk_offset, ndef_chunk_data], 'S S C60', 8, ''
end

#read_uidObject

Returns the current UID as an integer. Encode as Base58 to get the usual string version.



767
768
769
770
771
# File 'lib/tinkerforge/bricklet_nfc.rb', line 767

def read_uid
  check_validity

  send_request FUNCTION_READ_UID, [], '', 12, 'L'
end

#reader_authenticate_mifare_classic_page(page, key_number, key) ⇒ Object

Mifare Classic tags use authentication. If you want to read from or write to a Mifare Classic page you have to authenticate it beforehand. Each page can be authenticated with two keys: A (“key_number“ = 0) and B (“key_number“ = 1). A new Mifare Classic tag that has not yet been written to can be accessed with key A and the default key “[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]“.

The approach to read or write a Mifare Classic page is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_authenticate_mifare_classic_page with page and key for the page

  5. Wait for state to change to ReaderAuthenticatingMifareClassicPageReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  6. Call BrickletNFC#reader_request_page or BrickletNFC#reader_write_page to read/write page

The authentication will always work for one whole sector (4 pages).



358
359
360
361
362
# File 'lib/tinkerforge/bricklet_nfc.rb', line 358

def reader_authenticate_mifare_classic_page(page, key_number, key)
  check_validity

  send_request FUNCTION_READER_AUTHENTICATE_MIFARE_CLASSIC_PAGE, [page, key_number, key], 'S C C6', 8, ''
end

#reader_get_stateObject

Returns the current reader state of the NFC Bricklet.

On startup the Bricklet will be in the ReaderInitialization state. The initialization will only take about 20ms. After that it changes to ReaderIdle.

The Bricklet is also reinitialized if the mode is changed, see BrickletNFC#set_mode.

The functions of this Bricklet can be called in the ReaderIdle state and all of the ReaderReady and ReaderError states.

Example: If you call BrickletNFC#reader_request_page, the state will change to ReaderRequestPage until the reading of the page is finished. Then it will change to either ReaderRequestPageReady if it worked or to ReaderRequestPageError if it didn’t. If the request worked you can get the page by calling BrickletNFC#reader_read_page.

The same approach is used analogously for the other API functions.



283
284
285
286
287
# File 'lib/tinkerforge/bricklet_nfc.rb', line 283

def reader_get_state
  check_validity

  send_request FUNCTION_READER_GET_STATE, [], '', 10, 'C ?'
end

#reader_get_tag_idObject

Returns the tag type and the tag ID. This function can only be called if the NFC Bricklet is currently in one of the ReaderReady states. The returned tag ID is the tag ID that was saved through the last call of BrickletNFC#reader_request_tag_id.

To get the tag ID of a tag the approach is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. Call BrickletNFC#reader_get_tag_id



797
798
799
800
801
# File 'lib/tinkerforge/bricklet_nfc.rb', line 797

def reader_get_tag_id
  ret = reader_get_tag_id_low_level

  [ret[0], ret[2][0, ret[1]]]
end

#reader_get_tag_id_low_levelObject

Returns the tag type and the tag ID. This function can only be called if the NFC Bricklet is currently in one of the ReaderReady states. The returned tag ID is the tag ID that was saved through the last call of BrickletNFC#reader_request_tag_id.

To get the tag ID of a tag the approach is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. Call BrickletNFC#reader_get_tag_id



261
262
263
264
265
# File 'lib/tinkerforge/bricklet_nfc.rb', line 261

def reader_get_tag_id_low_level
  check_validity

  send_request FUNCTION_READER_GET_TAG_ID_LOW_LEVEL, [], '', 42, 'C C C32'
end

#reader_read_ndefObject

Returns the NDEF data from an internal buffer. To fill the buffer with a NDEF message you have to call BrickletNFC#reader_request_ndef beforehand.



850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/tinkerforge/bricklet_nfc.rb', line 850

def reader_read_ndef
  ndef_length = nil # assigned in block
  ndef_data = nil # assigned in block

  @stream_mutex.synchronize {
    ret = reader_read_ndef_low_level
    ndef_length = ret[0]
    ndef_chunk_offset = ret[1]
    ndef_out_of_sync = ndef_chunk_offset != 0
    ndef_data = ret[2]

    while not ndef_out_of_sync and ndef_data.length < ndef_length
      ret = reader_read_ndef_low_level
      ndef_length = ret[0]
      ndef_chunk_offset = ret[1]
      ndef_out_of_sync = ndef_chunk_offset != ndef_data.length
      ndef_data += ret[2]
    end

    if ndef_out_of_sync # discard remaining stream to bring it back in-sync
      while ndef_chunk_offset + 60 < ndef_length
        ret = reader_read_ndef_low_level
        ndef_length = ret[0]
        ndef_chunk_offset = ret[1]
      end

      raise StreamOutOfSyncException, 'NDEF stream is out-of-sync'
    end
  }

  ndef_data[0, ndef_length]
end

#reader_read_ndef_low_levelObject

Returns the NDEF data from an internal buffer. To fill the buffer with a NDEF message you have to call BrickletNFC#reader_request_ndef beforehand.



332
333
334
335
336
# File 'lib/tinkerforge/bricklet_nfc.rb', line 332

def reader_read_ndef_low_level
  check_validity

  send_request FUNCTION_READER_READ_NDEF_LOW_LEVEL, [], '', 72, 'S S C60'
end

#reader_read_pageObject

Returns the page data from an internal buffer. To fill the buffer with specific pages you have to call BrickletNFC#reader_request_page beforehand.



943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/tinkerforge/bricklet_nfc.rb', line 943

def reader_read_page
  data_length = nil # assigned in block
  data_data = nil # assigned in block

  @stream_mutex.synchronize {
    ret = reader_read_page_low_level
    data_length = ret[0]
    data_chunk_offset = ret[1]
    data_out_of_sync = data_chunk_offset != 0
    data_data = ret[2]

    while not data_out_of_sync and data_data.length < data_length
      ret = reader_read_page_low_level
      data_length = ret[0]
      data_chunk_offset = ret[1]
      data_out_of_sync = data_chunk_offset != data_data.length
      data_data += ret[2]
    end

    if data_out_of_sync # discard remaining stream to bring it back in-sync
      while data_chunk_offset + 60 < data_length
        ret = reader_read_page_low_level
        data_length = ret[0]
        data_chunk_offset = ret[1]
      end

      raise StreamOutOfSyncException, 'Data stream is out-of-sync'
    end
  }

  data_data[0, data_length]
end

#reader_read_page_low_levelObject

Returns the page data from an internal buffer. To fill the buffer with specific pages you have to call BrickletNFC#reader_request_page beforehand.



435
436
437
438
439
# File 'lib/tinkerforge/bricklet_nfc.rb', line 435

def reader_read_page_low_level
  check_validity

  send_request FUNCTION_READER_READ_PAGE_LOW_LEVEL, [], '', 72, 'S S C60'
end

#reader_request_ndefObject

Reads NDEF formated data from a tag.

This function currently supports NFC Forum Type 1, 2, 3 and 4.

The general approach for reading a NDEF message is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_request_ndef

  5. Wait for state to change to ReaderRequestNDEFReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  6. Call BrickletNFC#reader_read_ndef to retrieve the NDEF message from the buffer



324
325
326
327
328
# File 'lib/tinkerforge/bricklet_nfc.rb', line 324

def reader_request_ndef
  check_validity

  send_request FUNCTION_READER_REQUEST_NDEF, [], '', 8, ''
end

#reader_request_page(page, length) ⇒ Object

Reads a maximum of 8192 bytes starting from the given page and stores them into a buffer. The buffer can then be read out with BrickletNFC#reader_read_page. How many pages are read depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte

  • NFC Forum Type 1 page size: 8 byte

  • NFC Forum Type 2 page size: 4 byte

  • NFC Forum Type 3 page size: 16 byte

  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)

The general approach for reading a tag is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_request_page with page number

  5. Wait for state to change to ReaderRequestPageReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  6. Call BrickletNFC#reader_read_page to retrieve the page from the buffer

If you use a Mifare Classic tag you have to authenticate a page before you can read it. See BrickletNFC#reader_authenticate_mifare_classic_page.

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.



427
428
429
430
431
# File 'lib/tinkerforge/bricklet_nfc.rb', line 427

def reader_request_page(page, length)
  check_validity

  send_request FUNCTION_READER_REQUEST_PAGE, [page, length], 'S S', 8, ''
end

#reader_request_tag_idObject

After you call BrickletNFC#reader_request_tag_id the NFC Bricklet will try to read the tag ID from the tag. After this process is done the state will change. You can either register the CALLBACK_READER_STATE_CHANGED callback or you can poll BrickletNFC#reader_get_state to find out about the state change.

If the state changes to ReaderRequestTagIDError it means that either there was no tag present or that the tag has an incompatible type. If the state changes to ReaderRequestTagIDReady it means that a compatible tag was found and that the tag ID has been saved. You can now read out the tag ID by calling BrickletNFC#reader_get_tag_id.

If two tags are in the proximity of the NFC Bricklet, this function will cycle through the tags. To select a specific tag you have to call BrickletNFC#reader_request_tag_id until the correct tag ID is found.

In case of any ReaderError state the selection is lost and you have to start again by calling BrickletNFC#reader_request_tag_id.



245
246
247
248
249
# File 'lib/tinkerforge/bricklet_nfc.rb', line 245

def reader_request_tag_id
  check_validity

  send_request FUNCTION_READER_REQUEST_TAG_ID, [], '', 8, ''
end

#reader_write_ndef(ndef) ⇒ Object

Writes NDEF formated data.

This function currently supports NFC Forum Type 2 and 4.

The general approach for writing a NDEF message is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_write_ndef with the NDEF message that you want to write

  5. Wait for state to change to ReaderWriteNDEFReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/tinkerforge/bricklet_nfc.rb', line 817

def reader_write_ndef(ndef)
  if ndef.length > 65535
    raise ArgumentError, 'NDEF can be at most 65535 items long'
  end

  ndef_length = ndef.length
  ndef_chunk_offset = 0

  if ndef_length == 0
    ndef_chunk_data = [0] * 60
    ret = reader_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
  else
    ret = nil # assigned in block

    @stream_mutex.synchronize {
      while ndef_chunk_offset < ndef_length
        ndef_chunk_data = ndef[ndef_chunk_offset, 60]

        if ndef_chunk_data.length < 60
          ndef_chunk_data += [0] * (60 - ndef_chunk_data.length)
        end

        ret = reader_write_ndef_low_level ndef_length, ndef_chunk_offset, ndef_chunk_data
        ndef_chunk_offset += 60
      end
    }
  end

  ret
end

#reader_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data) ⇒ Object

Writes NDEF formated data.

This function currently supports NFC Forum Type 2 and 4.

The general approach for writing a NDEF message is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_write_ndef with the NDEF message that you want to write

  5. Wait for state to change to ReaderWriteNDEFReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)



303
304
305
306
307
# File 'lib/tinkerforge/bricklet_nfc.rb', line 303

def reader_write_ndef_low_level(ndef_length, ndef_chunk_offset, ndef_chunk_data)
  check_validity

  send_request FUNCTION_READER_WRITE_NDEF_LOW_LEVEL, [ndef_length, ndef_chunk_offset, ndef_chunk_data], 'S S C60', 8, ''
end

#reader_write_page(page, data) ⇒ Object

Writes a maximum of 8192 bytes starting from the given page. How many pages are written depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte

  • NFC Forum Type 1 page size: 8 byte

  • NFC Forum Type 2 page size: 4 byte

  • NFC Forum Type 3 page size: 16 byte

  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)

The general approach for writing to a tag is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_write_page with page number and data

  5. Wait for state to change to ReaderWritePageReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See BrickletNFC#reader_authenticate_mifare_classic_page.

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.



910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
# File 'lib/tinkerforge/bricklet_nfc.rb', line 910

def reader_write_page(page, data)
  if data.length > 65535
    raise ArgumentError, 'Data can be at most 65535 items long'
  end

  data_length = data.length
  data_chunk_offset = 0

  if data_length == 0
    data_chunk_data = [0] * 58
    ret = reader_write_page_low_level page, data_length, data_chunk_offset, data_chunk_data
  else
    ret = nil # assigned in block

    @stream_mutex.synchronize {
      while data_chunk_offset < data_length
        data_chunk_data = data[data_chunk_offset, 58]

        if data_chunk_data.length < 58
          data_chunk_data += [0] * (58 - data_chunk_data.length)
        end

        ret = reader_write_page_low_level page, data_length, data_chunk_offset, data_chunk_data
        data_chunk_offset += 58
      end
    }
  end

  ret
end

#reader_write_page_low_level(page, data_length, data_chunk_offset, data_chunk_data) ⇒ Object

Writes a maximum of 8192 bytes starting from the given page. How many pages are written depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte

  • NFC Forum Type 1 page size: 8 byte

  • NFC Forum Type 2 page size: 4 byte

  • NFC Forum Type 3 page size: 16 byte

  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)

The general approach for writing to a tag is as follows:

  1. Call BrickletNFC#reader_request_tag_id

  2. Wait for state to change to ReaderRequestTagIDReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

  3. If looking for a specific tag then call BrickletNFC#reader_get_tag_id and check if the expected tag was found, if it was not found got back to step 1

  4. Call BrickletNFC#reader_write_page with page number and data

  5. Wait for state to change to ReaderWritePageReady (see BrickletNFC#reader_get_state or CALLBACK_READER_STATE_CHANGED callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See BrickletNFC#reader_authenticate_mifare_classic_page.

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.



391
392
393
394
395
# File 'lib/tinkerforge/bricklet_nfc.rb', line 391

def reader_write_page_low_level(page, data_length, data_chunk_offset, data_chunk_data)
  check_validity

  send_request FUNCTION_READER_WRITE_PAGE_LOW_LEVEL, [page, data_length, data_chunk_offset, data_chunk_data], 'S S S C58', 8, ''
end

#register_callback(id, &block) ⇒ Object

Registers a callback with ID id to the block block.



1097
1098
1099
1100
# File 'lib/tinkerforge/bricklet_nfc.rb', line 1097

def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end

#resetObject

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!



748
749
750
751
752
# File 'lib/tinkerforge/bricklet_nfc.rb', line 748

def reset
  check_validity

  send_request FUNCTION_RESET, [], '', 8, ''
end

#set_bootloader_mode(mode) ⇒ Object

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.



671
672
673
674
675
# File 'lib/tinkerforge/bricklet_nfc.rb', line 671

def set_bootloader_mode(mode)
  check_validity

  send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C'
end

#set_detection_led_config(config) ⇒ Object

Sets the detection LED configuration. By default the LED shows if a card/reader is detected.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is off.



591
592
593
594
595
# File 'lib/tinkerforge/bricklet_nfc.rb', line 591

def set_detection_led_config(config)
  check_validity

  send_request FUNCTION_SET_DETECTION_LED_CONFIG, [config], 'C', 8, ''
end

#set_maximum_timeout(timeout) ⇒ Object

Sets the maximum timeout.

This is a global maximum used for all internal state timeouts. The timeouts depend heavily on the used tags etc. For example: If you use a Type 2 tag and you want to detect if it is present, you have to use BrickletNFC#reader_request_tag_id and wait for the state to change to either the error state or the ready state.

With the default configuration this takes 2-3 seconds. By setting the maximum timeout to 100ms you can reduce this time to ~150-200ms. For Type 2 this would also still work with a 20ms timeout (a Type 2 tag answers usually within 10ms). A type 4 tag can take up to 500ms in our tests.

If you need a fast response time to discover if a tag is present or not you can find a good timeout value by trial and error for your specific tag.

By default we use a very conservative timeout, to be sure that any tag can always answer in time.

.. versionadded

2.0.1$nbsp;(Plugin)



623
624
625
626
627
# File 'lib/tinkerforge/bricklet_nfc.rb', line 623

def set_maximum_timeout(timeout)
  check_validity

  send_request FUNCTION_SET_MAXIMUM_TIMEOUT, [timeout], 'S', 8, ''
end

#set_mode(mode) ⇒ Object

Sets the mode. The NFC Bricklet supports four modes:

  • Off

  • Card Emulation (Cardemu): Emulates a tag for other readers

  • Peer to Peer (P2P): Exchange data with other readers

  • Reader: Reads and writes tags

  • Simple: Automatically reads tag IDs

If you change a mode, the Bricklet will reconfigure the hardware for this mode. Therefore, you can only use functions corresponding to the current mode. For example, in Reader mode you can only use Reader functions.



215
216
217
218
219
# File 'lib/tinkerforge/bricklet_nfc.rb', line 215

def set_mode(mode)
  check_validity

  send_request FUNCTION_SET_MODE, [mode], 'C', 8, ''
end

#set_status_led_config(config) ⇒ Object

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.



717
718
719
720
721
# File 'lib/tinkerforge/bricklet_nfc.rb', line 717

def set_status_led_config(config)
  check_validity

  send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, ''
end

#set_write_firmware_pointer(pointer) ⇒ Object

Sets the firmware pointer for BrickletNFC#write_firmware. The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.



690
691
692
693
694
# File 'lib/tinkerforge/bricklet_nfc.rb', line 690

def set_write_firmware_pointer(pointer)
  check_validity

  send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, ''
end

#simple_get_tag_id(index) ⇒ Object

.. versionadded

2.0.6$nbsp;(Plugin)



1090
1091
1092
1093
1094
# File 'lib/tinkerforge/bricklet_nfc.rb', line 1090

def simple_get_tag_id(index)
  ret = simple_get_tag_id_low_level index

  [ret[0], ret[2][0, ret[1]], ret[3]]
end

#simple_get_tag_id_low_level(index) ⇒ Object

.. versionadded

2.0.6$nbsp;(Plugin)



639
640
641
642
643
# File 'lib/tinkerforge/bricklet_nfc.rb', line 639

def simple_get_tag_id_low_level(index)
  check_validity

  send_request FUNCTION_SIMPLE_GET_TAG_ID_LOW_LEVEL, [index], 'C', 24, 'C C C10 L'
end

#write_firmware(data) ⇒ Object

Writes 64 Bytes of firmware at the position as written by BrickletNFC#set_write_firmware_pointer before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.



704
705
706
707
708
# File 'lib/tinkerforge/bricklet_nfc.rb', line 704

def write_firmware(data)
  check_validity

  send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C'
end

#write_uid(uid) ⇒ Object

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.



759
760
761
762
763
# File 'lib/tinkerforge/bricklet_nfc.rb', line 759

def write_uid(uid)
  check_validity

  send_request FUNCTION_WRITE_UID, [uid], 'L', 8, ''
end