BlueHeron.GATT.Server behaviour (blue_heron v0.4.1) View Source

A behaviour module for implementing a GATT server.

This module handles all generic aspects of the GATT protocol, including MTU exchange and service discovery. The callback module is invoked for a description of the GATT profile (services and characteristics), as well as reading and writing characteristic values.

Example callback module:

defmodule MyApp.MyGattServer do
  @behaviour BlueHeron.GATT.Server

  @impl BlueHeron.GATT.Server
  def profile() do
    [
      BlueHeron.GATT.Service.new(%{
        id: :gap,
        type: 0x1800,
        characteristics: [
          BlueHeron.GATT.Characteristic.new(%{
            id: {:gap, :device_name},
            type: 0x2A00,
            properties: 0b0000010
          }),
          BlueHeron.GATT.Characteristic.new(%{
            id: {:gap, :appearance},
            type: 0x2A01,
            properties: 0b0000010
          })
        ]
      }),
      BlueHeron.GATT.Service.new(%{
        id: :my_custom_service,
        type: 0xBB5D5975D8E4853998F51335CDFFE9A,
        characteristics: [
          BlueHeron.GATT.Characteristic.new(%{
            id: {:my_custom_service, :my_custom_characteristic},
            type: 0x1234,
            properties: 0b0001010
          }),
          BlueHeron.GATT.Characteristic.new(%{
            id: {:my_custom_service, :another_custom_characteristic},
            type: 0xF018E00E0ECE45B09617B744833D89BA,
            properties: 0b0001010
          })
        ]
      })
    ]
  end

  @impl BlueHeron.GATT.Server
  def read({:gap, :device_name}) do
    "my-device-name"
  end

  @impl BlueHeron.GATT.Server
  def write({:my_custom_serivce, :my_custom_characteristic}, value) do
    MyApp.DB.insert(:my_custom_characteristic, value)
  end
end

Link to this section Summary

Callbacks

Return the list of services that make up the GATT profile of the device.

Return the value of the characteristic given by id. The value must be serialized as a binary.

Handle a write to the characteristic given by id.

Link to this section Types

Link to this section Callbacks

Specs

profile() :: [BlueHeron.GATT.Service.t()]

Return the list of services that make up the GATT profile of the device.

This callback is only invoked when the GATT server is started, as the profile is assumed to be static.

To comply with the Bluetooth specification, the profile must include a "GAP" service (type UUID 0x1800), which must have characteristics for "Device Name" (type UUID 0x2A00) and "Appearance" (type UUID 0x2A01).

Specs

read(id :: any()) :: binary()

Return the value of the characteristic given by id. The value must be serialized as a binary.

Specs

write(id :: any(), value :: binary()) :: :ok

Handle a write to the characteristic given by id.

Link to this section Functions