_bleio
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- _bleio.set_adapter(new_adapter: Adapter | None) None ¶
Set the adapter to use for BLE, such as when using an HCI adapter. Raises
NotImplementedError
when the adapter is a singleton and cannot be set.
_bleio.common
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- class _bleio.common.Adapter¶
Singleton _bleio.adapter is defined after class Adapter.
- await_bleak(coro, timeout: float | None = None)¶
Call an async routine in the bleak thread from sync code, and await its result.
- delete_connection(connection: Connection) None ¶
Remove the specified connection of the list of connections held by the adapter. (Blinka _bleio only).
- start_scan(prefixes: bytes | bytearray | memoryview = b'', *, buffer_size: int = 512, extended: bool = False, timeout: float | None = None, interval: float = 0.1, window: float = 0.1, minimum_rssi: int = -80, active: bool = True) Iterable[ScanEntry] ¶
Starts a BLE scan and returns an iterator of results. Advertisements and scan responses are filtered and returned separately.
- Parameters:
prefixes (sequence) – Sequence of byte string prefixes to filter advertising packets with. A packet without an advertising structure that matches one of the prefixes is ignored. Format is one byte for length (n) and n bytes of prefix and can be repeated.
buffer_size (int) – the maximum number of advertising bytes to buffer.
extended (bool) – When True, support extended advertising packets. Increasing buffer_size is recommended when this is set.
timeout (float) – the scan timeout in seconds. If None, will scan until
stop_scan
is called.interval (float) – the interval (in seconds) between the start of two consecutive scan windows. Must be in the range 0.0025 - 40.959375 seconds.
window (float) – the duration (in seconds) to scan a single BLE channel. window must be <= interval.
minimum_rssi (int) – the minimum rssi of entries to return.
active (bool) – retrieve scan responses for scannable advertisements.
- Returns:
an iterable of
ScanEntry
objects- Return type:
iterable
- class _bleio.common.Characteristic(*, uuid: UUID, properties: int = 0, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length: int = 20, fixed_length: bool = False, initial_value: bytes | bytearray | memoryview | None = None)¶
Stores information about a BLE service characteristic and allows reading and writing of the characteristic’s value.
There is no regular constructor for a Characteristic. A new local Characteristic can be created and attached to a Service by calling
add_to_service()
. Remote Characteristic objects are created by_bleio.Connection.discover_remote_services
as part of remote Services.- INDICATE = 2¶
server will indicate to the client when the value is set and wait for a response
- Type:
- WRITE_NO_RESPONSE = 32¶
clients may write this characteristic; no response will be sent back
- Type:
- classmethod add_to_service(service: Service, uuid: UUID, *, properties: int = 0, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length: int = 20, fixed_length: bool = False, initial_value: bytes | bytearray | memoryview | None = None) Characteristic ¶
Create a new Characteristic object, and add it to this Service.
- Parameters:
service (Service) – The service that will provide this characteristic
uuid (UUID) – The uuid of the characteristic
properties (int) – The properties of the characteristic, specified as a bitmask of these values bitwise-or’d together:
BROADCAST
,INDICATE
,NOTIFY
,READ
,WRITE
,WRITE_NO_RESPONSE
.read_perm (int) – Specifies whether the characteristic can be read by a client, and if so, which security mode is required. Must be one of the integer values
_bleio.Attribute.NO_ACCESS
,_bleio.Attribute.OPEN
,_bleio.Attribute.ENCRYPT_NO_MITM
,_bleio.Attribute.ENCRYPT_WITH_MITM
,_bleio.Attribute.LESC_ENCRYPT_WITH_MITM
,_bleio.Attribute.SIGNED_NO_MITM
, or_bleio.Attribute.SIGNED_WITH_MITM
.write_perm (int) – Specifies whether the characteristic can be written by a client, and if so, which security mode is required. Values allowed are the same as
read_perm
.max_length (int) – Maximum length in bytes of the characteristic value. The maximum allowed is is 512, or possibly 510 if
fixed_length
is False. The default, 20, is the maximum number of data bytes that fit in a single BLE 4.x ATT packet.fixed_length (bool) – True if the characteristic value is of fixed length.
initial_value (buf) – The initial value for this characteristic. If not given, will be filled with zeros.
- Returns:
the new Characteristic.
- property descriptors: Tuple['Descriptor']¶
py:class:~`Descriptor` that describe this characteristic. (read-only)
- Type:
A tuple of
- property properties: int¶
An int bitmask representing which properties are set, specified as bitwise or’ing of of these possible values.
BROADCAST
,INDICATE
,NOTIFY
,READ
,WRITE
,WRITE_NO_RESPONSE
.
- set_cccd(*, notify: bool = False, indicate: bool = False) None ¶
Set the remote characteristic’s CCCD to enable or disable notification and indication.
- property uuid: UUID¶
The UUID of this characteristic. (read-only) Will be
None
if the 128-bit UUID for this characteristic is not known.
- class _bleio.common.Connection(address: Address)¶
A BLE connection to another device. Used to discover and interact with services on the other device.
Usage:
import _bleio my_entry = None for entry in _bleio.adapter.scan(2.5): if entry.name is not None and entry.name == 'InterestingPeripheral': my_entry = entry break if not my_entry: raise Exception("'InterestingPeripheral' not found") connection = _bleio.adapter.connect(my_entry.address, timeout=10)
Connections should not be created directly. Instead, to initiate a connection use
_bleio.Adapter.connect
. Connections may also be made when another device initiates a connection. To use a Connection created by a peer, read the_bleio.Adapter.connections
property.- Parameters:
address (Address) – Address of device to connect to
- property connection_interval: float¶
Time between transmissions in milliseconds. Will be multiple of 1.25ms. Lower numbers increase speed and decrease latency but increase power consumption.
When setting connection_interval, the peer may reject the new interval and
connection_interval
will then remain the same.Apple has additional guidelines that dictate should be a multiple of 15ms except if HID is available. When HID is available Apple devices may accept 11.25ms intervals.
- property max_packet_length: int¶
The maximum number of data bytes that can be sent in a single transmission, not including overhead bytes.
This is the maximum number of bytes that can be sent in a notification, which must be sent in a single packet. But for a regular characteristic read or write, may be sent in multiple packets, so this limit does not apply.
- class _bleio.common.Service(uuid: UUID, *, secondary: bool = False, remote: bool = False)¶
Stores information about a BLE service and its characteristics.
Create a new Service identified by the specified UUID. It can be accessed by all connections. This is known as a Service server. Client Service objects are created via
_bleio.Connection.discover_remote_services
.To mark the Service as secondary, pass
True
assecondary
.- Parameters:
uuid (UUID) – The uuid of the service
secondary (bool) – If the service is a secondary one
- Returns:
the new Service
- property characteristics: Tuple[Characteristic]¶
A tuple of
Characteristic
designating the characteristics that are offered by this service. (read-only)
- property connection: Connection¶
Connection associated with this service, if any.
_bleio.address
¶
_bleio
for Blinka based on bleak
Author(s): Dan Halbert
_bleio.attribute
¶
_bleio
for Blinka based on bleak
Author(s): Dan Halbert
- class _bleio.attribute.Attribute(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)¶
_bleio.characteristic_buffer
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- class _bleio.characteristic_buffer.CharacteristicBuffer(characteristic: Characteristic, *, timeout: float = 1, buffer_size: int = 64)¶
Accumulates a Characteristic’s incoming values in a FIFO buffer.
Monitor the given Characteristic. Each time a new value is written to the Characteristic add the newly-written bytes to a FIFO buffer.
- Parameters:
characteristic (Characteristic) – The Characteristic to monitor. It may be a local Characteristic provided by a Peripheral Service, or a remote Characteristic in a remote Service that a Central has connected to.
timeout (int) – the timeout in seconds to wait for the first character and between subsequent characters.
buffer_size (int) – Size of ring buffer that stores incoming data coming from client. Must be >= 1.
- read(nbytes: int | None = None) bytes | bytearray | memoryview | None ¶
Read characters. If
nbytes
is specified then read at most that many bytes. Otherwise, read everything that arrives until the connection times out. Providing the number of bytes expected is highly recommended because it will be faster.- Returns:
Data read
- readinto(buf: bytes | bytearray | memoryview) int | None ¶
Read bytes into the
buf
. Read at mostlen(buf)
bytes.- Returns:
number of bytes read and stored into
buf
- readline() bytes | bytearray | memoryview ¶
Read a line, ending in a newline character.
- Returns:
the line read
_bleio.descriptor
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- class _bleio.descriptor.Descriptor(*, uuid: UUID, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length: int = 20, fixed_length: bool = False, initial_value: bytes | bytearray | memoryview = b'')¶
Stores information about a BLE descriptor.
Descriptors are attached to BLE characteristics and provide contextual information about the characteristic.
There is no regular constructor for a Descriptor. A new local Descriptor can be created and attached to a Characteristic by calling
add_to_characteristic()
. Remote Descriptor objects are created by_bleio.Connection.discover_remote_services
as part of remote Characteristics in the remote Services that are discovered.- classmethod add_to_characteristic(characteristic: Characteristic, uuid: UUID, *, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length: int = 20, fixed_length: bool = False, initial_value: bytes | bytearray | memoryview = b'')¶
Create a new Descriptor object, and add it to this Service.
- Parameters:
characteristic (Characteristic) – The characteristic that will hold this descriptor
uuid (UUID) – The uuid of the descriptor
read_perm (int) – Specifies whether the descriptor can be read by a client, and if so, which security mode is required. Must be one of the integer values
_bleio.Attribute.NO_ACCESS
,_bleio.Attribute.OPEN
,_bleio.Attribute.ENCRYPT_NO_MITM
,_bleio.Attribute.ENCRYPT_WITH_MITM
,_bleio.Attribute.LESC_ENCRYPT_WITH_MITM
,_bleio.Attribute.SIGNED_NO_MITM
, or_bleio.Attribute.SIGNED_WITH_MITM
.write_perm (int) – Specifies whether the descriptor can be written by a client, and if so, which security mode is required. Values allowed are the same as
read_perm
.max_length (int) – Maximum length in bytes of the descriptor value. The maximum allowed is 512, or possibly 510 if
fixed_length
is False. The default, 20, is the maximum number of data bytes that fit in a single BLE 4.x ATT packet.fixed_length (bool) – True if the descriptor value is of fixed length.
initial_value (buf) – The initial value for this descriptor.
- Returns:
the new Descriptor.
- property characteristic: Characteristic¶
The Characteristic this Descriptor is a part of.
- property uuid: UUID¶
The descriptor uuid. (read-only)
_bleio.exceptions
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- exception _bleio.exceptions.BluetoothError¶
Catch-all exception for Bluetooth related errors.
- exception _bleio.exceptions.ConnectionError¶
Raised when a connection is unavailable.
- exception _bleio.exceptions.RoleError¶
Raised when a resource is used as the mismatched role. For example, if a local CCCD is attempted to be set but it can only be set when remote.
- exception _bleio.exceptions.SecurityError¶
Raised when a security related error occurs.
_bleio.packet_buffer
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
- class _bleio.packet_buffer.PacketBuffer(characteristic: Characteristic, *, buffer_size: int)¶
Accumulates a Characteristic’s incoming packets in a FIFO buffer and facilitates packet aware outgoing writes. A packet’s size is either the characteristic length or the maximum transmission unit (MTU) minus overhead, whichever is smaller. The MTU can change so check
incoming_packet_length
andoutgoing_packet_length
before creating a buffer to store data.When we’re the server, we ignore all connections besides the first to subscribe to notifications.
Monitor the given Characteristic. Each time a new value is written to the Characteristic add the newly-written bytes to a FIFO buffer.
- Parameters:
characteristic (Characteristic) – The Characteristic to monitor. It may be a local Characteristic provided by a Peripheral Service, or a remote Characteristic in a remote Service that a Central has connected to.
buffer_size (int) – Size of ring buffer (in packets of the Characteristic’s maximum length) that stores incoming packets coming from the peer.
- property outgoing_packet_length¶
Maximum length in bytes of a packet we are writing.
- property packet_size: int¶
packet_size
is the same asincoming_packet_length
. The namepacket_size
is deprecated and will be removed in CircuitPython 6.0.0.
- readinto(buf: bytes | bytearray | memoryview) int ¶
Reads a single BLE packet into the
buf
. Raises an exception if the next packet is longer than the given buffer. Usepacket_size
to read the maximum length of a single packet.- Returns:
number of bytes read and stored into
buf
- Return type:
- write(data: bytes | bytearray | memoryview, *, header: bytes | bytearray | memoryview | None = None) int ¶
Writes all bytes from data into the same outgoing packet. The bytes from header are included before data when the pending packet is currently empty.
This does not block until the data is sent. It only blocks until the data is pending.
- Returns:
number of bytes written. May include header bytes when packet is empty.
- Return type:
_bleio.scan_entry
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries
_bleio.uuid_
¶
_bleio implementation for Adafruit_Blinka_bleio
Author(s): Dan Halbert for Adafruit Industries