Peripheral role

WHAD provides a specific connector to create a BLE device, whad.ble.connector.Peripheral. This connector implements a GATT server and hosts a GATT profile, allowing remote BLE devices to connect to it and query its services, characteristics, and descriptors.

The connector provides some callbacks such as whad.ble.connector.Peripheral.on_connected() to react on specific events.

Bluetooth Low Energy Peripheral connector

class whad.ble.connector.Peripheral(device, existing_connection=None, profile=None, adv_data=None, scan_data=None, bd_address=None, public=True, stack=<class 'whad.ble.stack.BleStack'>, gatt=<class 'whad.ble.stack.gatt.GattServer'>, pairing=<whad.ble.stack.smp.parameters.Pairing object>, security_database=None)[source]

This BLE connector provides a way to create a peripheral device.

A peripheral device exposes some services and characteristics that may be accessed by a central device. These services and characteristics are defined by a specific profile.

get_pairing_parameters()[source]

Returns the provided pairing parameters, if any.

is_connected() bool[source]

Determine if the peripheral has an active connection from a GATT client.

on_connected(connection_data)[source]

A device has just connected to this peripheral.

Parameters:

connection_data (whad.protocol.ble_pb2.Connected) – Connection data

on_ctl_pdu(pdu)[source]

This method is called whenever a control PDU is received. This PDU is then forwarded to the BLE stack to handle it.

Peripheral devices act as a slave, so we only forward master to slave messages to the stack.

Parameters:

pdu (scapy.layers.bluetooth4LE.BTLE) – BLE PDU

on_data_pdu(pdu)[source]

This method is called whenever a data PDU is received. This PDU is then forwarded to the BLE stack to handle it.

Parameters:

pdu (scapy.layers.bluetooth4LE.BTLE_DATA) – BLE PDU

on_disconnected(disconnection_data)[source]

A device has just disconnected from this peripheral.

Parameters:

connection_data (whad.protocol.ble_pb2.Disconnected) – Connection data

on_new_connection(connection)[source]

On new connection, discover primary services

Parameters:

connection (whad.protocol.ble_pb2.Connected) – Connection data

send_ctrl_pdu(pdu, conn_handle=1, direction=2, access_address=2391391958, encrypt=None) bool[source]

Send a PDU to the central device this peripheral device is connected to.

Sending direction is set to ̀ BleDirection.SLAVE_TO_MASTER` as we need to send PDUs to a central device.

Parameters:
  • pdu (scapy.layers.bluetooth4LE.BTLE) – PDU to send

  • conn_handle (int) – Connection handle

  • direction (whad.protocol.ble_pb2.BleDirection, optional) – Sending direction (to master or slave)

  • access_address (int, optional) – Target access address

Returns:

PDU transmission result.

Return type:

bool

send_data_pdu(pdu, conn_handle=1, direction=2, access_address=2391391958, encrypt=None) bool[source]

Send a PDU to the central device this peripheral device is connected to.

Sending direction is set to ̀ BleDirection.SLAVE_TO_MASTER` as we need to send PDUs to a central device.

Parameters:
  • pdu (scapy.layers.bluetooth4LE.BTLE) – PDU to send

  • conn_handle (int) – Connection handle

  • direction (whad.protocol.ble_pb2.BleDirection, optional) – Sending direction (to master or slave)

  • access_address (int, optional) – Target access address

Returns:

PDU transmission result.

Return type:

bool

use_stack(clazz=<class 'whad.ble.stack.BleStack'>)[source]

Specify a stack class to use for BLE. By default, our own stack (BleStack) is used.

Parameters:

clazz (whad.ble.stack.BleStack) – BLE stack to use.

wait_connection()[source]

Wait for a GATT client to connect to the peripheral. If a connection is already active, returns immediately.