ABleCentral

The ABleCentral is the partner to the ABlePeripheral and is what is returned by the accept() method of the peripheral, this class serves as an abstraction of the functionality the peripheral has in communicating with the central. ABleCentral’s functions are modeled after the Berkeley Socket API. There aren’t many occasions ABleCentral instance should be initialized outside of a peripheral creating one after a connection as all of the core functionality relies on the connection to the peripheral and its underlying application.

Class Definition

class able.central.ABleCentral(application, identifier, mtu, adapter_path=None, central_obj=None)[source]

A platform agnostic BLE central class. This wrapper surrounds methods within the peripheral server using a berkeley-socket-like API.

Class attribute _app will be the ABleApplication that created this central and is required to interact with the central itself.

Parameters
  • application (BluezApplication) – the application that created this central

  • identifier (str) – the identifier used by the application which uniquely identifies this central

Variables
  • identifier (str) – the identifier of the central, this is the mac address on Ubuntu and a uuid on MacOS

  • disconnected_event (asyncio.Event) – an event that will only be set when the we get a callback indicating a disconnect from the hardware

  • is_closing (bool) – a boolean indicator if the peripheral has begun to tear down the connection, if this is True all IO will be blocked

  • mtu (int) – the maximum transmission unit of the central, if any data is attempted to be sent larger than this value an exception will be raised,

Methods

Communication

async able.central.ABleCentral.send(self, data, characteristic=None)

Berkeley socket style send function. Takes in data and by default notifies the central through the comms characteristic but an alternate characteristic can be provided.

Raises

ValueError – if the data is longer than the MTU of the device

Parameters
  • data (bytes) – the data to send to the characteristic

  • characteristic (BluezCharacteristic) – an alternate characteristic to send the data on via notifying the device

Return type

None

Returns

None

async able.central.ABleCentral.recv(self, characteristic=None, timeout=None)

Berkeley socket recv function, waits until there is data on the recv queue and returns data once it is populated, an optional timeout can be passed in.

Raises

TimeoutError – if the queue is empty after the timout passed in

Return type

bytes

Parameters
  • timeout (int) – an optional timeout, defaults to None

  • characteristic (BluezCharacteristic) – the characteristic that you want to receive data from

Returns

returns data from this centrals data queue which stores writes the central did to the communications characteristic

able.central.ABleCentral.flush_buffers(self)

Clears all of the recv queues for the central, this is useful if a previous communication timed out and you require a fresh buffer before starting a new interaction.

Return type

None

Returns

None

State

async able.central.ABleCentral.close(self)

Calls the disconnect command in the ABleApplication that owns this central. This will also set the is closing member to true to indicate that the process of tearing down the connection has begun and no more operations should happen.

Return type

None

Returns

None