View Source LibNFC.Presence behaviour (LibNFC v0.1.0)

Passive tag monitor process.

  • Repeatedly polls for passive targets in range.
  • Emits an event when target enters.
  • Emits another event when target leaves.

Target presence is tracked by polling nfc_initiator_target_is_present with a short debounce.

Usage

defmodule MyApp.NFC do
  use LibNFC.Presence

  @impl true
  def handle_target_in(target, state) do
    # ...
    {:ok, state}
  end

  @impl true
  def handle_target_out(state) do
    # ...
    {:ok, state}
  end
end

defmodule MyApp.Application do
  def start(_, _) do
    children = [
      ...
      {MyApp.NFC, client_state: :foo}
    ]

    Supervisor.start_link(...)
  end
end

Summary

Types

Arbitrary client state held within the presence monitor process.

Server Options

Callbacks

Emitted when a passive target is detected.

Emitted when selected target left.

Called on process initialization to open the NFC reader device.

Functions

Returns a specification to start this module under a supervisor.

Starts the passive tag monitor process.

Types

Link to this type

client_state()

View Source (since 0.1.0)
@type client_state() :: any()

Arbitrary client state held within the presence monitor process.

Link to this type

server_options()

View Source (since 0.1.0)
@type server_options() :: [
  client_state: client_state(),
  mock: boolean(),
  schedule_delay: non_neg_integer(),
  alive_after_last_seen: non_neg_integer()
]

Server Options

  • client_state initial client state (default: nil)
  • mock when set to true, default device will be the mock (default: false)
  • schedule_delay poll interval in ms (default: 300)
  • alive_after_last_seen debounce leave event for a short period as some calls to nfc_initiator_target_is_present come back as false negatives (default: 500)

Callbacks

Link to this callback

handle_target_in(target_info, client_state)

View Source (since 0.1.0)
@callback handle_target_in(LibNFC.target_info(), client_state()) :: {:ok, client_state()}

Emitted when a passive target is detected.

Link to this callback

handle_target_out(client_state)

View Source (since 0.1.0)
@callback handle_target_out(client_state()) :: {:ok, client_state()}

Emitted when selected target left.

Link to this callback

open_device(client_state)

View Source (optional) (since 0.1.0)
@callback open_device(client_state()) :: {:ok, LibNFC.device()}

Called on process initialization to open the NFC reader device.

Optional callback. Defaults to opening the "first" device found by calling nfc_open without a connstring.

Functions

Link to this function

child_spec(init_arg)

View Source (since 0.1.0)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(client, opts)

View Source (since 0.1.0)
@spec start_link(module(), server_options()) :: GenServer.on_start()

Starts the passive tag monitor process.