bluetooth — low-level Bluetooth¶
This module provides an interface to the on-board Bluetooth controller. It supports Bluetooth Low Energy (BLE) in Central, Peripheral, Broadcaster and Observer roles, as well as GATT Server and Client and L2CAP connection-oriented channels. A device may operate in multiple roles concurrently. Pairing and bonding are also supported.
This API is intended to match the low-level Bluetooth protocol and provide building-blocks for higher-level abstractions such as specific device types.
Tip
For most applications, prefer the higher-level aioble
library, which provides an asyncio-based wrapper around this module.
See aioble — Async BLE.
class BLE¶
- class bluetooth.BLE¶
Returns the singleton BLE object.
Configuration
- active(active: bool | None = None, /) bool¶
Optionally changes the active state of the BLE radio, and returns the current state.
The radio must be made active before using any other methods on this class.
- config(param: str, /) Any¶
- config(*, **kwargs: Any) None
Get or set configuration values of the BLE interface. To get a value the parameter name should be quoted as a string, and just one parameter is queried at a time. To set values use the keyword syntax, and one or more parameters can be set at a time.
Currently supported values are:
'mac': The current address in use, depending on the current address mode. This returns a tuple of(addr_type, addr).See
gap_scanfor details about address type.This may only be queried while the interface is currently active.
'addr_mode': Sets the address mode. Values are:Value
Name
Behaviour
0x00PUBLIC
Use the controller’s public address.
0x01RANDOM
Use a generated static address.
0x02RPA
Use resolvable private addresses.
0x03NRPA
Use non-resolvable private addresses.
By default the interface will use a PUBLIC address if available, otherwise it will use a RANDOM address.
'gap_name': Get/set the GAP device name used by the Generic Access service (UUID0x1800), Device Name characteristic (UUID0x2a00). This can be set at any time and changed multiple times.'rxbuf': Get/set the size in bytes of the internal buffer used to store incoming events. This buffer is global to the entire BLE driver and so handles incoming data for all events, including all characteristics. Increasing this allows better handling of bursty incoming data (for example scan results) and the ability to receive larger characteristic values.'mtu': Get/set the MTU that will be used during an ATT MTU exchange. The resulting MTU will be the minimum of this and the remote device’s MTU. ATT MTU exchange will not happen automatically (unless the remote device initiates it), and must be manually initiated withgattc_exchange_mtu. Use the_IRQ_MTU_EXCHANGEDevent to discover the MTU for a given connection.'bond': Sets whether bonding will be enabled during pairing. When enabled, pairing requests will set the “bond” flag and the keys will be stored by both devices.'mitm': Sets whether MITM-protection is required for pairing.'io': Sets the I/O capabilities of this device.Available options are:
Constant
Value
Capability
_IO_CAPABILITY_DISPLAY_ONLY0
Display only
_IO_CAPABILITY_DISPLAY_YESNO1
Display with yes/no input
_IO_CAPABILITY_KEYBOARD_ONLY2
Keyboard only
_IO_CAPABILITY_NO_INPUT_OUTPUT3
No input or output
_IO_CAPABILITY_KEYBOARD_DISPLAY4
Keyboard and display
'le_secure': Sets whether “LE Secure” pairing is required. Default is false (i.e. allow “Legacy Pairing”).
Event Handling
- irq(handler: Callable[[int, Tuple], Any | None], /) None¶
Registers a callback for events from the BLE stack. The handler takes two arguments,
event(which will be one of the codes below) anddata(which is an event-specific tuple of values).Note: As an optimisation to prevent unnecessary allocations, the
addr,adv_data,char_data,notify_data, anduuidentries in the tuples are read-only memoryview instances pointing tobluetooth’s internal ringbuffer, and are only valid during the invocation of the IRQ handler function. If your program needs to save one of these values to access after the IRQ handler has returned (e.g. by saving it in a class instance or global variable), then it needs to take a copy of the data, either by usingbytes()orbluetooth.UUID(), like this:connected_addr = bytes(addr) # equivalently: adv_data, char_data, or notify_data matched_uuid = bluetooth.UUID(uuid)
For example, the IRQ handler for a scan result might inspect the
adv_datato decide if it’s the correct device, and only then copy the address data to be used elsewhere in the program. And to print data from within the IRQ handler,print(bytes(addr))will be needed.A handler typically dispatches on the event code and unpacks the event-specific payload tuple:
def bt_irq(event, data): if event == _IRQ_CENTRAL_CONNECT: conn_handle, addr_type, addr = data ... elif event == _IRQ_SCAN_RESULT: addr_type, addr, adv_type, rssi, adv_data = data ...
Every event code, the payload it delivers, and a short description are listed below. For events whose
statusfield is mentioned,statusis0on success and an implementation-specific non-zero value on failure.Constant
Value
Event
Payload tuple
_IRQ_CENTRAL_CONNECT1
A central has connected to this peripheral.
(conn_handle, addr_type, addr)_IRQ_CENTRAL_DISCONNECT2
A central has disconnected from this peripheral.
(conn_handle, addr_type, addr)_IRQ_GATTS_WRITE3
A connected client has written to a local characteristic or descriptor. Use
gatts_readto fetch the new value.(conn_handle, attr_handle)_IRQ_GATTS_READ_REQUEST4
A connected client has issued a read. Return a non-zero error code from the table below to deny the read, or
0/Noneto accept it.(conn_handle, attr_handle)_IRQ_SCAN_RESULT5
A single advertising packet was received during an active scan.
(addr_type, addr, adv_type, rssi, adv_data)_IRQ_SCAN_DONE6
The current scan has ended, either because the configured duration elapsed or because
gap_scan(None)was called.()_IRQ_PERIPHERAL_CONNECT7
A previously-issued
gap_connecthas succeeded.(conn_handle, addr_type, addr)_IRQ_PERIPHERAL_DISCONNECT8
A connected peripheral has disconnected.
(conn_handle, addr_type, addr)_IRQ_GATTC_SERVICE_RESULT9
One service was found by
gattc_discover_services.(conn_handle, start_handle, end_handle, uuid)_IRQ_GATTC_SERVICE_DONE10
Service discovery has finished.
(conn_handle, status)_IRQ_GATTC_CHARACTERISTIC_RESULT11
One characteristic was found by
gattc_discover_characteristics.(conn_handle, end_handle, value_handle, properties, uuid)_IRQ_GATTC_CHARACTERISTIC_DONE12
Characteristic discovery has finished.
(conn_handle, status)_IRQ_GATTC_DESCRIPTOR_RESULT13
One descriptor was found by
gattc_discover_descriptors.(conn_handle, dsc_handle, uuid)_IRQ_GATTC_DESCRIPTOR_DONE14
Descriptor discovery has finished.
(conn_handle, status)_IRQ_GATTC_READ_RESULT15
A previously-issued
gattc_readhas returned data.(conn_handle, value_handle, char_data)_IRQ_GATTC_READ_DONE16
A previously-issued
gattc_readhas finished.(conn_handle, value_handle, status)_IRQ_GATTC_WRITE_DONE17
A previously-issued
gattc_writehas been acknowledged.(conn_handle, value_handle, status)_IRQ_GATTC_NOTIFY18
A remote server has sent an (unacknowledged) notification.
(conn_handle, value_handle, notify_data)_IRQ_GATTC_INDICATE19
A remote server has sent an (acknowledged) indication.
(conn_handle, value_handle, notify_data)_IRQ_GATTS_INDICATE_DONE20
A previously-sent indication has been acknowledged by the client (or has timed out).
(conn_handle, value_handle, status)_IRQ_MTU_EXCHANGED21
An ATT MTU exchange has completed (initiated by either side).
(conn_handle, mtu)_IRQ_L2CAP_ACCEPT22
A remote device has requested an L2CAP connection on a PSM this device is listening on. Return a non-zero integer to reject, or
0/Noneto accept.(conn_handle, cid, psm, our_mtu, peer_mtu)_IRQ_L2CAP_CONNECT23
An L2CAP channel is now established, either by accepting an incoming request or by completing an outgoing
l2cap_connect.(conn_handle, cid, psm, our_mtu, peer_mtu)_IRQ_L2CAP_DISCONNECT24
An L2CAP channel has been disconnected.
statusis0for a clean disconnect, or non-zero if an outgoing connection attempt failed.(conn_handle, cid, psm, status)_IRQ_L2CAP_RECV25
Data has arrived on an L2CAP channel. Call
l2cap_recvintoto read it.(conn_handle, cid)_IRQ_L2CAP_SEND_READY26
A previous
l2cap_sendthat returnedFalsehas drained and the channel is ready again. A non-zerostatusmeans the transmit buffer overflowed and the application must re-send the data.(conn_handle, cid, status)_IRQ_CONNECTION_UPDATE27
The remote device has updated the connection parameters (interval, latency, supervision timeout).
(conn_handle, conn_interval, conn_latency, supervision_timeout, status)_IRQ_ENCRYPTION_UPDATE28
The encryption state of a connection has changed, typically after pairing or bonding completes.
(conn_handle, encrypted, authenticated, bonded, key_size)_IRQ_GET_SECRET29
The stack is requesting a stored bonding secret. If
keyisNone, return theindexth stored value ofsec_type; otherwise return the value associated with the given(sec_type, key). ReturnNoneif nothing is stored.(sec_type, index, key)_IRQ_SET_SECRET30
The stack is asking the application to persist a bonding secret. Return
Trueonce stored.(sec_type, key, value)_IRQ_PASSKEY_ACTION31
A passkey action is required as part of pairing. Respond using
gap_passkey; see the passkey-action table below for the possible actions.(conn_handle, action, passkey)For the
_IRQ_GATTS_READ_REQUESTevent, the available return codes are:Constant
Value
Meaning
_GATTS_NO_ERROR0x00Accept the read.
_GATTS_ERROR_READ_NOT_PERMITTED0x02Read not permitted.
_GATTS_ERROR_WRITE_NOT_PERMITTED0x03Write not permitted.
_GATTS_ERROR_INSUFFICIENT_AUTHENTICATION0x05Client is not authenticated.
_GATTS_ERROR_INSUFFICIENT_AUTHORIZATION0x08Client is not authorised.
_GATTS_ERROR_INSUFFICIENT_ENCRYPTION0x0fLink is not encrypted.
For the
_IRQ_PASSKEY_ACTIONevent, the available actions are:Constant
Value
Meaning
_PASSKEY_ACTION_NONE0
No action required.
_PASSKEY_ACTION_INPUT2
Prompt the user to enter the passkey shown on the remote device.
_PASSKEY_ACTION_DISPLAY3
Display a 6-digit passkey for the remote device to enter.
_PASSKEY_ACTION_NUMERIC_COMPARISON4
Confirm that the passkey matches the one shown on the remote device.
In order to save space in the firmware, these constants are not included on the
bluetoothmodule. Add the ones that you need from the lists above to your program.
Broadcaster Role (Advertiser)
- gap_advertise(interval_us: int | None, adv_data: bytes | None = None, *, resp_data: bytes | None = None, connectable: bool = True) None¶
Starts advertising at the specified interval (in microseconds). This interval will be rounded down to the nearest 625us. To stop advertising, set interval_us to
None.adv_data and resp_data can be any type that implements the buffer protocol (e.g.
bytes,bytearray,str). adv_data is included in all broadcasts, and resp_data is sent in reply to an active scan.Note: if adv_data (or resp_data) is
None, then the data passed to the previous call togap_advertisewill be reused. This allows a broadcaster to resume advertising with justgap_advertise(interval_us). To clear the advertising payload pass an emptybytes, i.e.b''.
Observer Role (Scanner)
- gap_scan(duration_ms: int | None, interval_us: int = 1280000, window_us: int = 11250, active: bool = False, /) None¶
Run a scan operation lasting for the specified duration (in milliseconds).
To scan indefinitely, set duration_ms to
0.To stop scanning, set duration_ms to
None.Use interval_us and window_us to optionally configure the duty cycle. The scanner will run for window_us microseconds every interval_us microseconds for a total of duration_ms milliseconds. The default interval and window are 1.28 seconds and 11.25 milliseconds respectively (background scanning).
For each scan result the
_IRQ_SCAN_RESULTevent will be raised, with event data(addr_type, addr, adv_type, rssi, adv_data).addr_typevalues indicate public or random addresses:Value
Name
Meaning
0x00PUBLIC
Public device address.
0x01RANDOM
Random address (either static, RPA, or NRPA; the type is encoded in the address itself).
adv_typevalues correspond to the Bluetooth Specification:Value
Name
Meaning
0x00ADV_IND
Connectable and scannable undirected advertising.
0x01ADV_DIRECT_IND
Connectable directed advertising.
0x02ADV_SCAN_IND
Scannable undirected advertising.
0x03ADV_NONCONN_IND
Non-connectable undirected advertising.
0x04SCAN_RSP
Scan response.
activecan be setTrueif you want to receive scan responses in the results.When scanning is stopped (either due to the duration finishing or when explicitly stopped), the
_IRQ_SCAN_DONEevent will be raised.
Central Role
A central device can connect to peripherals that it has discovered using the observer role (see
gap_scan) or with a known address.- gap_connect(addr_type: int | None, addr: bytes | None = None, scan_duration_ms: int = 2000, min_conn_interval_us: int | None = None, max_conn_interval_us: int | None = None, /) None¶
Connect to a peripheral.
See
gap_scanfor details about address types.To cancel an outstanding connection attempt early, call
gap_connect(None).On success, the
_IRQ_PERIPHERAL_CONNECTevent will be raised. If cancelling a connection attempt, the_IRQ_PERIPHERAL_DISCONNECTevent will be raised.The device will wait up to scan_duration_ms to receive an advertising payload from the device.
The connection interval can be configured in microseconds using either or both of min_conn_interval_us and max_conn_interval_us. Otherwise a default interval will be chosen, typically between 30000 and 50000 microseconds. A shorter interval will increase throughput, at the expense of power usage.
Peripheral Role
A peripheral device is expected to send connectable advertisements (see
gap_advertise). It will usually be acting as a GATT server, having first registered services and characteristics usinggatts_register_services.When a central connects, the
_IRQ_CENTRAL_CONNECTevent will be raised.Central & Peripheral Roles
- gap_disconnect(conn_handle: int, /) bool¶
Disconnect the specified connection handle. This can either be a central that has connected to this device (if acting as a peripheral) or a peripheral that was previously connected to by this device (if acting as a central).
On success, the
_IRQ_PERIPHERAL_DISCONNECTor_IRQ_CENTRAL_DISCONNECTevent will be raised.Returns
Falseif the connection handle wasn’t connected, andTrueotherwise.
GATT Server
A GATT server has a set of registered services. Each service may contain characteristics, which each have a value. Characteristics can also contain descriptors, which themselves have values.
These values are stored locally, and are accessed by their “value handle” which is generated during service registration. They can also be read from or written to by a remote client device. Additionally, a server can “notify” a characteristic to a connected client via a connection handle.
A device in either central or peripheral roles may function as a GATT server, however in most cases it will be more common for a peripheral device to act as the server.
Characteristics and descriptors have a default maximum size of 20 bytes (the default ATT MTU of 23 bytes minus a 3-byte ATT header; a larger negotiated MTU does not by itself raise this limit). Anything written to them by a client will be truncated to this length. However, any local write will increase the maximum size, so if you want to allow larger writes from a client to a given characteristic, use
gatts_writeafter registration. e.g.gatts_write(char_handle, bytes(100)).- gatts_register_services(services_definition: Sequence[Sequence], /) Sequence[Sequence[int]]¶
Configures the server with the specified services, replacing any existing services.
services_definition is a list of services, where each service is a two-element tuple containing a UUID and a list of characteristics.
Each characteristic is a two-or-three-element tuple containing a UUID, a flags value, and optionally a list of descriptors.
Each descriptor is a two-element tuple containing a UUID and a flags value.
The flags are a bitwise-OR combination of the flags defined below. These set both the behaviour of the characteristic (or descriptor) as well as the security and privacy requirements.
The return value is a list (one element per service) of tuples (each element is a value handle). Characteristics and descriptor handles are flattened into the same tuple, in the order that they are defined.
The following example registers two services (Heart Rate, and Nordic UART):
bt = bluetooth.BLE() bt.active(True) # Heart Rate service: one Heart Rate Measurement characteristic. HR_SERVICE = ( bluetooth.UUID(0x180D), ( (bluetooth.UUID(0x2A37), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY), ), ) # Nordic UART service: a TX characteristic the client subscribes # to for notifications, and an RX characteristic it writes to. UART_SERVICE = ( bluetooth.UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E'), ( (bluetooth.UUID('6E400003-B5A3-F393-E0A9-E50E24DCCA9E'), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY), (bluetooth.UUID('6E400002-B5A3-F393-E0A9-E50E24DCCA9E'), bluetooth.FLAG_WRITE), ), ) ((hr,), (tx, rx)) = bt.gatts_register_services( (HR_SERVICE, UART_SERVICE), )
The three value handles (
hr,tx,rx) can be used withgatts_read,gatts_write,gatts_notify, andgatts_indicate.Note: Advertising must be stopped before registering services.
Available flags for characteristics and descriptors are:
Constant
Value
Meaning
_FLAG_BROADCAST0x0001Characteristic may be broadcast.
_FLAG_READ0x0002Client may read the value.
_FLAG_WRITE_NO_RESPONSE0x0004Client may write without expecting a response.
_FLAG_WRITE0x0008Client may write with an acknowledged response.
_FLAG_NOTIFY0x0010Server may send notifications (unacknowledged).
_FLAG_INDICATE0x0020Server may send indications (acknowledged).
_FLAG_AUTHENTICATED_SIGNED_WRITE0x0040Client may issue signed writes.
_FLAG_AUX_WRITE0x0100Extended properties: queued/reliable writes are allowed.
_FLAG_READ_ENCRYPTED0x0200Read requires an encrypted link.
_FLAG_READ_AUTHENTICATED0x0400Read requires an authenticated (MITM-protected) link.
_FLAG_READ_AUTHORIZED0x0800Read requires application-level authorisation.
_FLAG_WRITE_ENCRYPTED0x1000Write requires an encrypted link.
_FLAG_WRITE_AUTHENTICATED0x2000Write requires an authenticated (MITM-protected) link.
_FLAG_WRITE_AUTHORIZED0x4000Write requires application-level authorisation.
As with the event constants above, these flags are not provided by the
bluetoothmodule; copy the ones you need into your program.
- gatts_read(value_handle: int, /) bytes¶
Reads the local value for this handle (which has either been written by
gatts_writeor by a remote client).
- gatts_write(value_handle: int, data: bytes, send_update: bool = False, /) None¶
Writes the local value for this handle, which can be read by a client.
If send_update is
True, then any subscribed clients will be notified (or indicated, depending on what they’re subscribed to and which operations the characteristic supports) about this write.
- gatts_notify(conn_handle: int, value_handle: int, data: bytes | None = None, /) None¶
Sends a notification request to a connected client.
If data is
None(the default), then the current local value (as set withgatts_write) will be sent.Otherwise, if data is not
None, then that value is sent to the client as part of the notification. The local value will not be modified.Note: The notification will be sent regardless of the subscription status of the client to this characteristic.
- gatts_indicate(conn_handle: int, value_handle: int, data: bytes | None = None, /) None¶
Sends an indication request to a connected client.
If data is
None(the default), then the current local value (as set withgatts_write) will be sent.Otherwise, if data is not
None, then that value is sent to the client as part of the indication. The local value will not be modified.On acknowledgment (or failure, e.g. timeout), the
_IRQ_GATTS_INDICATE_DONEevent will be raised.Note: The indication will be sent regardless of the subscription status of the client to this characteristic.
- gatts_set_buffer(value_handle: int, len: int, append: bool = False, /) None¶
Sets the internal buffer size for a value in bytes. This will limit the largest possible write that can be received. The default is 20 bytes (default ATT MTU of 23 minus the 3-byte ATT header).
Setting append to
Truewill make all remote writes append to, rather than replace, the current value. At most len bytes can be buffered in this way. When you usegatts_read, the value will be cleared after reading. This feature is useful when implementing something like the Nordic UART Service.
GATT Client
A GATT client can discover and read/write characteristics on a remote GATT server.
It is more common for a central role device to act as the GATT client, however it’s also possible for a peripheral to act as a client in order to discover information about the central that has connected to it (e.g. to read the device name from the device information service).
- gattc_discover_services(conn_handle: int, uuid: UUID | None = None, /) None¶
Query a connected server for its services.
Optionally specify a service uuid to query for that service only.
For each service discovered, the
_IRQ_GATTC_SERVICE_RESULTevent will be raised, followed by_IRQ_GATTC_SERVICE_DONEon completion.
- gattc_discover_characteristics(conn_handle: int, start_handle: int, end_handle: int, uuid: UUID | None = None, /) None¶
Query a connected server for characteristics in the specified range.
Optionally specify a characteristic uuid to query for that characteristic only.
Passing
start_handle=1andend_handle=0xffffcovers the full GATT attribute-handle range, so this combination effectively searches every service on the remote device.For each characteristic discovered, the
_IRQ_GATTC_CHARACTERISTIC_RESULTevent will be raised, followed by_IRQ_GATTC_CHARACTERISTIC_DONEon completion.
- gattc_discover_descriptors(conn_handle: int, start_handle: int, end_handle: int, /) None¶
Query a connected server for descriptors in the specified range.
For each descriptor discovered, the
_IRQ_GATTC_DESCRIPTOR_RESULTevent will be raised, followed by_IRQ_GATTC_DESCRIPTOR_DONEon completion.
- gattc_read(conn_handle: int, value_handle: int, /) None¶
Issue a remote read to a connected server for the specified characteristic or descriptor handle.
When a value is available, the
_IRQ_GATTC_READ_RESULTevent will be raised, followed by_IRQ_GATTC_READ_DONEon completion.
- gattc_write(conn_handle: int, value_handle: int, data: bytes, mode: int = 0, /) None¶
Issue a remote write to a connected server for the specified characteristic or descriptor handle.
The argument mode specifies the write behaviour, with the currently supported values being:
mode=0(default) is a write-without-response: the write will be sent to the remote server but no confirmation will be returned, and no event will be raised.mode=1is a write-with-response: the remote server is requested to send a response/acknowledgement that it received the data.
If a response is received from the remote server the
_IRQ_GATTC_WRITE_DONEevent will be raised.
- gattc_exchange_mtu(conn_handle: int, /) None¶
Initiate MTU exchange with a connected server, using the preferred MTU set using
BLE.config(mtu=value).The
_IRQ_MTU_EXCHANGEDevent will be raised when MTU exchange completes.MTU exchange is typically initiated by the central; NimBLE supports both roles.
L2CAP Connection-Oriented Channels
This feature allows for socket-like data exchange between two BLE devices. Once the devices are connected via GAP, either device can listen for the other to connect on a numeric PSM (Protocol/Service Multiplexer).
Only one L2CAP channel may be active at a given time (i.e. you cannot connect while listening).
Active L2CAP channels are identified by the connection handle that they were established on and a CID (channel ID).
Connection-oriented channels have built-in credit-based flow control. Unlike ATT, where devices negotiate a shared MTU, both the listening and connecting devices each set an independent MTU which limits the maximum amount of outstanding data that the remote device can send before it is fully consumed in
l2cap_recvinto.- l2cap_listen(psm: int, mtu: int, /) None¶
Start listening for incoming L2CAP channel requests on the specified psm with the local MTU set to mtu.
When a remote device initiates a connection, the
_IRQ_L2CAP_ACCEPTevent will be raised, which gives the listening server a chance to reject the incoming connection (by returning a non-zero integer).Once the connection is accepted, the
_IRQ_L2CAP_CONNECTevent will be raised, allowing the server to obtain the channel ID (CID) and the local and remote MTU.Note: It is not currently possible to stop listening.
- l2cap_connect(conn_handle: int, psm: int, mtu: int, /) None¶
Connect to a listening peer on the specified psm with local MTU set to mtu.
On successful connection, the
_IRQ_L2CAP_CONNECTevent will be raised, allowing the client to obtain the CID and the local and remote (peer) MTU.An unsuccessful connection will raise the
_IRQ_L2CAP_DISCONNECTevent with a non-zero status.
- l2cap_disconnect(conn_handle: int, cid: int, /) None¶
Disconnect an active L2CAP channel with the specified conn_handle and cid.
- l2cap_send(conn_handle: int, cid: int, buf: bytes, /) bool¶
Send the specified buf (which must support the buffer protocol) on the L2CAP channel identified by conn_handle and cid.
The buffer must satisfy both limits: it must not exceed the remote (peer) MTU, and it must not exceed twice the local MTU.
This will return
Falseif the channel is now “stalled”, which means thatl2cap_sendmust not be called again until the_IRQ_L2CAP_SEND_READYevent is received (which will happen when the remote device grants more credits, typically after it has received and processed the data).
- l2cap_recvinto(conn_handle: int, cid: int, buf: Any | None, /) int¶
Receive data from the specified conn_handle and cid into the provided buf (which must support the buffer protocol, e.g. bytearray or memoryview).
Returns the number of bytes read from the channel.
If buf is
None, then returns the number of bytes available.Note: After receiving the
_IRQ_L2CAP_RECVevent, the application should continue callingl2cap_recvintountil no more bytes are available in the receive buffer (typically up to the size of the remote (peer) MTU).Until the receive buffer is empty, the remote device will not be granted more channel credits and will be unable to send any more data.
Pairing and Bonding
Pairing allows a connection to be encrypted and authenticated via exchange of secrets (with optional MITM protection via passkey authentication).
Bonding is the process of storing those secrets into non-volatile storage. When bonded, a device is able to resolve a resolvable private address (RPA) from another device based on the stored identity resolving key (IRK). To support bonding, an application must implement the
_IRQ_GET_SECRETand_IRQ_SET_SECRETevents.- gap_pair(conn_handle: int, /) None¶
Initiate pairing with the remote device.
Before calling this, ensure that the
io,mitm,le_secure, andbondconfiguration options are set (viaconfig).On successful pairing, the
_IRQ_ENCRYPTION_UPDATEevent will be raised.
- gap_passkey(conn_handle: int, action: int, passkey: int, /) None¶
Respond to a
_IRQ_PASSKEY_ACTIONevent for the specified conn_handle and action. The meaning of passkey depends on action (which in turn depends on the configured I/O capability):Action
Required passkey response
_PASSKEY_ACTION_INPUTThe passkey the user reads from the remote device.
_PASSKEY_ACTION_DISPLAYA locally-generated random 6-digit passkey shown to the user.
_PASSKEY_ACTION_NUMERIC_COMPARISON1to accept the passkey shown in the_IRQ_PASSKEY_ACTIONevent, or0to cancel pairing.
class UUID¶
- class bluetooth.UUID(value: int | bytes | str, /)¶
Creates a UUID instance with the specified
value. Bluetooth uses three UUID widths;UUIDaccepts any of them:UUID width
Accepted
valuetypesExample
16-bit
intor a 2-byte buffer (little-endian)UUID(0x2908)orUUID(b'\x08\x29')32-bit
4-byte buffer (little-endian)
UUID(b'\x08\x29\x00\x00')128-bit
16-byte buffer or a hyphenated string
UUID('6E400001-B5A3-F393-E0A9-E50E24DCCA9E')16- and 32-bit UUIDs are typically SIG-allocated identifiers (see the Bluetooth assigned numbers); 128-bit UUIDs are normally vendor-defined.