Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module exports all you should need to build a Bluetooth Low Energy (BLE) peripheral.
The core concepts involved are:
Application
- This contains the entirety of your application, and is
composed of zero or more
Service
s. Service
- A set of zero or more conceptually related
Characteristic
s. Identified by it'sUUID
. Characteristic
Characteristic
s represent the actual data of your application. They may allow reading, writing, and subscribing. Also identified by it'sUUID
.Advertisement
- This describes how an application will advertise itself to other BLE devices.
All three have IsString
instances and lens field accessors. The
recommended way of using this library is by using the OverloadedStrings
pragma and lenses. A complete example can be found
here.
{-# LANGUAGE OverloadedStrings #-} import Bluetooth import Control.Concurrent (threadDelay) app :: Application app = "/com/turingjump/example" & services .~ [aService] aService :: Service aService = "d0bc6707-e9a5-4c85-8d22-d73d33f0330c" & characteristics .~ [aCharacteristic] aCharacteristic :: CharacteristicBS aCharacteristic = "b3170df6-1770-4d60-86db-a487534cbcc3" & readValue ?~ encodeRead (return (32::Int)) & properties .~ [CPRead] main :: IO () main = do conn <- connect runBluetoothM (registerAndAdverstiseApplication app) conn threadDelay maxBound
- registerApplication :: Application -> BluetoothM ()
- registerAndAdvertiseApplication :: Application -> BluetoothM ()
- advertise :: WithObjectPath Advertisement -> BluetoothM ()
- advertisementFor :: Application -> WithObjectPath Advertisement
- connect :: IO Connection
- runBluetoothM :: BluetoothM a -> Connection -> IO (Either MethodError a)
- uuid :: HasUuid s a => Lens' s a
- properties :: HasProperties s a => Lens' s a
- readValue :: HasReadValue s a => Lens' s a
- writeValue :: HasWriteValue s a => Lens' s a
- notifying :: HasNotifying s a => Lens' s a
- characteristics :: HasCharacteristics s a => Lens' s a
- services :: HasServices s a => Lens' s a
- path :: HasPath s a => Lens' s a
- type_ :: HasType_ s a => Lens' s a
- value :: HasValue s a => Lens' s a
- solicitUUIDs :: HasSolicitUUIDs s a => Lens' s a
- serviceUUIDs :: HasServiceUUIDs s a => Lens' s a
- manufacturerData :: HasManufacturerData s a => Lens' s a
- serviceData :: HasServiceData s a => Lens' s a
- includeTxPower :: HasIncludeTxPower s a => Lens' s a
- writeChrc :: Serialize x => WithObjectPath CharacteristicBS -> x -> BluetoothM Bool
- data Connection
- data Application
- data Service
- data UUID = UUID UUID
- data CharacteristicProperty
- data Characteristic typ
- type CharacteristicBS = Characteristic ByteString
- data Advertisement
- data WithObjectPath a
- encodeRead :: Serialize a => ReadValueM a -> ReadValueM ByteString
- encodeWrite :: Serialize a => (a -> WriteValueM Bool) -> ByteString -> WriteValueM Bool
- data Handler errs a
- type ReadValueM a = Handler `[ThrowsFailed, ThrowsInProgress, ThrowsNotPermitted, ThrowsNotAuthorized, ThrowsNotSupported]` a
- type WriteValueM a = Handler `[ThrowsFailed, ThrowsInProgress, ThrowsNotPermitted, ThrowsInvalidValueLength, ThrowsNotAuthorized, ThrowsNotSupported]` a
- class ThrowsFailed m where
- errFailed :: m a
- class ThrowsInProgress m where
- errInProgress :: m a
- class ThrowsNotPermitted m where
- errNotPermitted :: m a
- class ThrowsNotAuthorized m where
- errNotAuthorized :: m a
- class ThrowsNotSupported m where
- errNotSupported :: m a
- class ThrowsInvalidValueLength m where
- errInvalidValueLength :: m a
- module Lens.Micro
- module Lens.Micro.GHC
Documentation
registerApplication :: Application -> BluetoothM () Source
Registers an application (set of services) with Bluez.
registerAndAdvertiseApplication :: Application -> BluetoothM () Source
Registers an application and advertises it. If you would like to have
finer-grained control of the advertisement, use registerApplication
and
advertise
.
advertise :: WithObjectPath Advertisement -> BluetoothM () Source
Advertise a set of services.
advertisementFor :: Application -> WithObjectPath Advertisement Source
Create an advertisement for all of an application's services. The advertisement will be for peripheral (not broadcast) by default.
connect :: IO Connection Source
Creates a connection to DBus. This does *not* represent Bluetooth connection.
runBluetoothM :: BluetoothM a -> Connection -> IO (Either MethodError a) Source
Field lenses
properties :: HasProperties s a => Lens' s a Source
readValue :: HasReadValue s a => Lens' s a Source
writeValue :: HasWriteValue s a => Lens' s a Source
notifying :: HasNotifying s a => Lens' s a Source
characteristics :: HasCharacteristics s a => Lens' s a Source
services :: HasServices s a => Lens' s a Source
solicitUUIDs :: HasSolicitUUIDs s a => Lens' s a Source
serviceUUIDs :: HasServiceUUIDs s a => Lens' s a Source
manufacturerData :: HasManufacturerData s a => Lens' s a Source
serviceData :: HasServiceData s a => Lens' s a Source
includeTxPower :: HasIncludeTxPower s a => Lens' s a Source
Updating values with notification
writeChrc :: Serialize x => WithObjectPath CharacteristicBS -> x -> BluetoothM Bool Source
Write a characteristic (if possible). Returns True if characterstic was successfully written.
BLE Types
Types representing components of a BLE application.
data Application Source
An application. Can be created from it's IsString
instance.
The string (application path) is used only for the DBus API, and will not
have relevance within Bluetooth.
UUIDs, used for services and characteristics.
Unofficial UUIDs will have 128-bits, and will look this:
d45e83fb-c772-459e-91a8-43cbf1443af4
Official UUIDs will have either 32 or 16 bits.
See ITU-T Rec. X.677 for more information on the format and generation of these UUIDs. You can use the Online UUID Generator to generate UUIDs.
data CharacteristicProperty Source
data Characteristic typ Source
HasCharacteristics Service [CharacteristicBS] Source | |
IsString (Characteristic a) Source | |
Generic (Characteristic typ) Source | |
Representable (WithObjectPath (Characteristic a)) Source | |
HasUuid (Characteristic typ) UUID Source | |
HasInterface (WithObjectPath CharacteristicBS) GattCharacteristic Source | |
HasInterface (WithObjectPath CharacteristicBS) Properties Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source | |
HasProperties (Characteristic typ) [CharacteristicProperty] Source | |
HasNotifying (Characteristic typ) (Maybe (IORef Bool)) Source | |
type Rep (Characteristic typ) Source | |
type RepType (WithObjectPath (Characteristic a)) = AnyDBusDict Source |
data Advertisement Source
data WithObjectPath a Source
Encoding and decoding
Helpers for readValue
and writeValue
.
encodeRead :: Serialize a => ReadValueM a -> ReadValueM ByteString Source
encodeWrite :: Serialize a => (a -> WriteValueM Bool) -> ByteString -> WriteValueM Bool Source
Handler
Handler err
is a monad that allows the errors in the type-level list
err
.
IsElem ((* -> *) -> Constraint) (ThrowsInvalidValueLength *) errs => ThrowsInvalidValueLength * (Handler errs) Source | |
IsElem ((* -> *) -> Constraint) (ThrowsNotSupported *) errs => ThrowsNotSupported * (Handler errs) Source | |
IsElem ((* -> *) -> Constraint) (ThrowsNotAuthorized *) errs => ThrowsNotAuthorized * (Handler errs) Source | |
IsElem ((* -> *) -> Constraint) (ThrowsNotPermitted *) errs => ThrowsNotPermitted * (Handler errs) Source | |
IsElem ((* -> *) -> Constraint) (ThrowsInProgress *) errs => ThrowsInProgress * (Handler errs) Source | |
IsElem ((* -> *) -> Constraint) (ThrowsFailed *) errs => ThrowsFailed * (Handler errs) Source | |
Monad (Handler errs) Source | |
Functor (Handler errs) Source | |
Applicative (Handler errs) Source | |
MonadIO (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
type ReadValueM a = Handler `[ThrowsFailed, ThrowsInProgress, ThrowsNotPermitted, ThrowsNotAuthorized, ThrowsNotSupported]` a Source
type WriteValueM a = Handler `[ThrowsFailed, ThrowsInProgress, ThrowsNotPermitted, ThrowsInvalidValueLength, ThrowsNotAuthorized, ThrowsNotSupported]` a Source
Handler error classes
class ThrowsFailed m where Source
IsElem ((* -> *) -> Constraint) (ThrowsFailed *) errs => ThrowsFailed * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
class ThrowsInProgress m where Source
errInProgress :: m a Source
IsElem ((* -> *) -> Constraint) (ThrowsInProgress *) errs => ThrowsInProgress * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
class ThrowsNotPermitted m where Source
errNotPermitted :: m a Source
IsElem ((* -> *) -> Constraint) (ThrowsNotPermitted *) errs => ThrowsNotPermitted * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
class ThrowsNotAuthorized m where Source
errNotAuthorized :: m a Source
IsElem ((* -> *) -> Constraint) (ThrowsNotAuthorized *) errs => ThrowsNotAuthorized * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
class ThrowsNotSupported m where Source
errNotSupported :: m a Source
IsElem ((* -> *) -> Constraint) (ThrowsNotSupported *) errs => ThrowsNotSupported * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source | |
HasReadValue (Characteristic typ) (Maybe (ReadValueM typ)) Source |
class ThrowsInvalidValueLength m where Source
errInvalidValueLength :: m a Source
IsElem ((* -> *) -> Constraint) (ThrowsInvalidValueLength *) errs => ThrowsInvalidValueLength * (Handler errs) Source | |
HasWriteValue (Characteristic typ) (Maybe (typ -> WriteValueM Bool)) Source |
Re-exports
module Lens.Micro
module Lens.Micro.GHC