View Source BlueHeron.GATT.Server (blue_heron v0.5.1)
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