class Tinkerforge::BrickletNFC

NFC tag read/write, NFC P2P and Card Emulation

Constants

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.

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.

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.

Public Class Methods

new(uid, ipcon) click to toggle source

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

Calls superclass method
# File lib/tinkerforge/bricklet_nfc.rb, line 150
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 1]

  @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_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

Public Instance Methods

cardemu_get_state() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 452
def cardemu_get_state
  check_validity

  send_request FUNCTION_CARDEMU_GET_STATE, [], '', 10, 'C ?'
end
cardemu_start_discovery() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 468
def cardemu_start_discovery
  check_validity

  send_request FUNCTION_CARDEMU_START_DISCOVERY, [], '', 8, ''
end
cardemu_start_transfer(transfer) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 495
def cardemu_start_transfer(transfer)
  check_validity

  send_request FUNCTION_CARDEMU_START_TRANSFER, [transfer], 'C', 8, ''
end
cardemu_write_ndef(ndef) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 971
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) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 481
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_mode() click to toggle source

Returns the current bootloader mode, see BrickletNFC#set_bootloader_mode.

# File lib/tinkerforge/bricklet_nfc.rb, line 666
def get_bootloader_mode
  check_validity

  send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C'
end
get_chip_temperature() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 724
def get_chip_temperature
  check_validity

  send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's'
end
get_detection_led_config() click to toggle source

Returns the configuration as set by BrickletNFC#set_detection_led_config

# File lib/tinkerforge/bricklet_nfc.rb, line 593
def get_detection_led_config
  check_validity

  send_request FUNCTION_GET_DETECTION_LED_CONFIG, [], '', 9, 'C'
end
get_identity() click to toggle source

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|

# File lib/tinkerforge/bricklet_nfc.rb, line 771
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_maximum_timeout() click to toggle source

Returns the timeout as set by BrickletNFC#set_maximum_timeout

.. versionadded

2.0.1$nbsp;(Plugin)

# File lib/tinkerforge/bricklet_nfc.rb, line 627
def get_maximum_timeout
  check_validity

  send_request FUNCTION_GET_MAXIMUM_TIMEOUT, [], '', 10, 'S'
end
get_mode() click to toggle source

Returns the mode as set by BrickletNFC#set_mode.

# File lib/tinkerforge/bricklet_nfc.rb, line 217
def get_mode
  check_validity

  send_request FUNCTION_GET_MODE, [], '', 9, 'C'
end
get_spitfp_error_count() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 644
def get_spitfp_error_count
  check_validity

  send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L'
end
get_status_led_config() click to toggle source

Returns the configuration as set by BrickletNFC#set_status_led_config

# File lib/tinkerforge/bricklet_nfc.rb, line 712
def get_status_led_config
  check_validity

  send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C'
end
p2p_get_state() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 517
def p2p_get_state
  check_validity

  send_request FUNCTION_P2P_GET_STATE, [], '', 10, 'C ?'
end
p2p_read_ndef() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 1044
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_level() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 574
def p2p_read_ndef_low_level
  check_validity

  send_request FUNCTION_P2P_READ_NDEF_LOW_LEVEL, [], '', 72, 'S S C60'
end
p2p_start_discovery() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 533
def p2p_start_discovery
  check_validity

  send_request FUNCTION_P2P_START_DISCOVERY, [], '', 8, ''
end
p2p_start_transfer(transfer) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 564
def p2p_start_transfer(transfer)
  check_validity

  send_request FUNCTION_P2P_START_TRANSFER, [transfer], 'C', 8, ''
end
p2p_write_ndef(ndef) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 1009
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) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 546
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_uid() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 755
def read_uid
  check_validity

  send_request FUNCTION_READ_UID, [], '', 12, 'L'
end
reader_authenticate_mifare_classic_page(page, key_number, key) click to toggle source

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).

# File lib/tinkerforge/bricklet_nfc.rb, line 353
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_state() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 278
def reader_get_state
  check_validity

  send_request FUNCTION_READER_GET_STATE, [], '', 10, 'C ?'
end
reader_get_tag_id() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 785
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_level() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 256
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_ndef() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 838
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_level() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 327
def reader_read_ndef_low_level
  check_validity

  send_request FUNCTION_READER_READ_NDEF_LOW_LEVEL, [], '', 72, 'S S C60'
end
reader_read_page() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 931
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_level() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 430
def reader_read_page_low_level
  check_validity

  send_request FUNCTION_READER_READ_PAGE_LOW_LEVEL, [], '', 72, 'S S C60'
end
reader_request_ndef() click to toggle source

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

# File lib/tinkerforge/bricklet_nfc.rb, line 319
def reader_request_ndef
  check_validity

  send_request FUNCTION_READER_REQUEST_NDEF, [], '', 8, ''
end
reader_request_page(page, length) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 422
def reader_request_page(page, length)
  check_validity

  send_request FUNCTION_READER_REQUEST_PAGE, [page, length], 'S S', 8, ''
end
reader_request_tag_id() click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 240
def reader_request_tag_id
  check_validity

  send_request FUNCTION_READER_REQUEST_TAG_ID, [], '', 8, ''
end
reader_write_ndef(ndef) click to toggle source

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)

# File lib/tinkerforge/bricklet_nfc.rb, line 805
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) click to toggle source

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)

# File lib/tinkerforge/bricklet_nfc.rb, line 298
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) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 898
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) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 386
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) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_nfc.rb, line 1078
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
reset() click to toggle source

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!

# File lib/tinkerforge/bricklet_nfc.rb, line 736
def reset
  check_validity

  send_request FUNCTION_RESET, [], '', 8, ''
end
set_bootloader_mode(mode) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 659
def set_bootloader_mode(mode)
  check_validity

  send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C'
end
set_detection_led_config(config) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 586
def set_detection_led_config(config)
  check_validity

  send_request FUNCTION_SET_DETECTION_LED_CONFIG, [config], 'C', 8, ''
end
set_maximum_timeout(timeout) click to toggle source

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)

# File lib/tinkerforge/bricklet_nfc.rb, line 618
def set_maximum_timeout(timeout)
  check_validity

  send_request FUNCTION_SET_MAXIMUM_TIMEOUT, [timeout], 'S', 8, ''
end
set_mode(mode) click to toggle source

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

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 210
def set_mode(mode)
  check_validity

  send_request FUNCTION_SET_MODE, [mode], 'C', 8, ''
end
set_status_led_config(config) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 705
def set_status_led_config(config)
  check_validity

  send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, ''
end
set_write_firmware_pointer(pointer) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 678
def set_write_firmware_pointer(pointer)
  check_validity

  send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, ''
end
write_firmware(data) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 692
def write_firmware(data)
  check_validity

  send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C'
end
write_uid(uid) click to toggle source

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.

# File lib/tinkerforge/bricklet_nfc.rb, line 747
def write_uid(uid)
  check_validity

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