Meshcore.Transport.BLE.Adapter behaviour (Meshcore v0.1.0)

Copy Markdown View Source

Behaviour for the platform-specific half of the BLE transport.

Implement it on top of whatever BLE stack you have (BlueZ over D-Bus, a Port talking to a helper such as bleak, a Rustler NIF around btleplug, Nerves' blue_heron, …) and pass the module as :adapter to Meshcore.create_ble/1.

Contract

connect/2 receives the pid of the Meshcore.Transport.BLE process and the options listed below. After a successful connect the adapter must:

  • subscribe to notifications on the TX characteristic and forward each notification payload as send(transport, {:ble_data, binary});
  • send send(transport, {:ble_disconnected, reason}) when the link drops.

Each notification is one complete protocol frame – BLE needs no extra framing.

Options passed to connect/2: :address (may be nil), :name_prefix ("MeshCore"; when no usable address is given, scan for a device whose advertised name starts with it), :service_uuid, :rx_char_uuid, :tx_char_uuid, plus everything from the transport's :adapter_opts.

Summary

Types

Opaque adapter state/handle, passed back to the other callbacks.

Callbacks

Disconnects, best effort.

Pairs with the device using pin.

Writes one frame to the RX characteristic (with response).

Types

handle()

@type handle() :: term()

Opaque adapter state/handle, passed back to the other callbacks.

Callbacks

connect(transport, opts)

@callback connect(transport :: pid(), opts :: keyword()) ::
  {:ok, handle(), address :: String.t()} | {:error, term()}

disconnect(handle)

@callback disconnect(handle()) :: :ok

Disconnects, best effort.

pair(handle, pin)

@callback pair(handle(), pin :: String.t()) :: :ok | {:error, term()}

Pairs with the device using pin.

write(handle, frame)

@callback write(handle(), frame :: binary()) :: :ok | {:error, term()}

Writes one frame to the RX characteristic (with response).