class Tinkerforge::BrickletNFCRFID

Reads and writes NFC and RFID tags

Constants

CALLBACK_STATE_CHANGED

This callback is called if the state of the NFC/RFID Bricklet changes. See BrickletNFCRFID#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_rfid.rb, line 55
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_REQUEST_TAG_ID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_TAG_ID] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_STATE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_REQUEST_PAGE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_PAGE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_STATE_CHANGED] = [10, 'C ?']

  @ipcon.add_device self
end

Public Instance Methods

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 BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

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

  4. Call BrickletNFCRFID#authenticate_mifare_classic_page with page and key for the page

  5. Wait for state to change to AuthenticatingMifareClassicPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  6. Call BrickletNFCRFID#request_page or BrickletNFCRFID#write_page to read/write page

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 163
def authenticate_mifare_classic_page(page, key_number, key)
  check_validity

  send_request FUNCTION_AUTHENTICATE_MIFARE_CLASSIC_PAGE, [page, key_number, key], 'S C C6', 8, ''
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_rfid.rb, line 242
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_page() click to toggle source

Returns 16 bytes of data from an internal buffer. To fill the buffer with specific pages you have to call BrickletNFCRFID#request_page beforehand.

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 226
def get_page
  check_validity

  send_request FUNCTION_GET_PAGE, [], '', 24, 'C16'
end
get_state() click to toggle source

Returns the current state of the NFC/RFID Bricklet.

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

The functions of this Bricklet can be called in the Idle state and all of the Ready and Error states.

Example: If you call BrickletNFCRFID#request_page, the state will change to RequestPage until the reading of the page is finished. Then it will change to either RequestPageReady if it worked or to RequestPageError if it didn't. If the request worked you can get the page by calling BrickletNFCRFID#get_page.

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

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 139
def get_state
  check_validity

  send_request FUNCTION_GET_STATE, [], '', 10, 'C ?'
end
get_tag_id() click to toggle source

Returns the tag type, tag ID and the length of the tag ID (4 or 7 bytes are possible length). This function can only be called if the NFC/RFID is currently in one of the Ready states. The returned ID is the ID that was saved through the last call of BrickletNFCRFID#request_tag_id.

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

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  3. Call BrickletNFCRFID#get_tag_id

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 119
def get_tag_id
  check_validity

  send_request FUNCTION_GET_TAG_ID, [], '', 17, 'C C C7'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 247
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
request_page(page) click to toggle source

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

  • Mifare Classic page size: 16 byte (one page is read)

  • NFC Forum Type 1 page size: 8 byte (two pages are read)

  • NFC Forum Type 2 page size: 4 byte (four pages are read)

The general approach for reading a tag is as follows:

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

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

  4. Call BrickletNFCRFID#request_page with page number

  5. Wait for state to change to RequestPageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

  6. Call BrickletNFCRFID#get_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 BrickletNFCRFID#authenticate_mifare_classic_page.

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 218
def request_page(page)
  check_validity

  send_request FUNCTION_REQUEST_PAGE, [page], 'S', 8, ''
end
request_tag_id(tag_type) click to toggle source

To read or write a tag that is in proximity of the NFC/RFID Bricklet you first have to call this function with the expected tag type as parameter. It is no problem if you don't know the tag type. You can cycle through the available tag types until the tag gives an answer to the request.

Currently the following tag types are supported:

  • Mifare Classic

  • NFC Forum Type 1

  • NFC Forum Type 2

After you call BrickletNFCRFID#request_tag_id the NFC/RFID 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_STATE_CHANGED callback or you can poll BrickletNFCRFID#get_state to find out about the state change.

If the state changes to RequestTagIDError it means that either there was no tag present or that the tag is of an incompatible type. If the state changes to RequestTagIDReady it means that a compatible tag was found and that the tag ID could be read out. You can now get the tag ID by calling BrickletNFCRFID#get_tag_id.

If two tags are in the proximity of the NFC/RFID Bricklet, this function will cycle through the tags. To select a specific tag you have to call BrickletNFCRFID#request_tag_id until the correct tag id is found.

In case of any Error state the selection is lost and you have to start again by calling BrickletNFCRFID#request_tag_id.

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 102
def request_tag_id(tag_type)
  check_validity

  send_request FUNCTION_REQUEST_TAG_ID, [tag_type], 'C', 8, ''
end
write_page(page, data) click to toggle source

Writes 16 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 (one page is written)

  • NFC Forum Type 1 page size: 8 byte (two pages are written)

  • NFC Forum Type 2 page size: 4 byte (four pages are written)

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

  1. Call BrickletNFCRFID#request_tag_id

  2. Wait for state to change to RequestTagIDReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

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

  4. Call BrickletNFCRFID#write_page with page number and data

  5. Wait for state to change to WritePageReady (see BrickletNFCRFID#get_state or CALLBACK_STATE_CHANGED callback)

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

# File lib/tinkerforge/bricklet_nfc_rfid.rb, line 189
def write_page(page, data)
  check_validity

  send_request FUNCTION_WRITE_PAGE, [page, data], 'S C16', 8, ''
end