Device – BLE device

IGNORE ``Device`` and all its documentation. It is being replaced by `Peripheral` and other classes.

Provides access a to BLE device, either in a Peripheral or Central role. When a device is created without any parameter passed to the constructor, it will be set to the Peripheral role. If a address is passed, the device will be a Central. For a Peripheral you can set the name, add services via add_service and then start and stop advertising via bleio.Device.start_advertising and bleio.Device.stop_advertising. For the Central, you can bleio.Device.connect and bleio.Device.disconnect to the device, once a connection is established, the device’s services can be accessed using bleio.Device.services.

Usage:

import bleio

# Peripheral
periph = bleio.Device()

serv = bleio.Service(bleio.UUID(0x180f))
p.add_service(serv)

chara = bleio.Characteristic(bleio.UUID(0x2919))
chara.read = True
chara.notify = True
serv.add_characteristic(chara)

periph.start_advertising()

# Central
scanner = bleio.Scanner()
entries = scanner.scan(2500)

my_entry = None
for entry in entries:
    if entry.name is not None and entry.name == 'MyDevice':
        my_entry = entry
        break

central = bleio.Device(my_entry.address)
central.connect()
class bleio.Device(address=None, scan_entry=None)

Create a new Device object. If the address or scan_entry parameters are not None, the role is set to Central, otherwise it’s set to Peripheral.

Parameters:
  • address (bleio.Address) – The address of the device to connect to
  • scan_entry (bleio.ScanEntry) – The scan entry returned from bleio.Scanner
name

For the Peripheral role, this property can be used to read and write the device’s name. For the Central role, this property will equal the name of the remote device, if one was advertised by the device. In the Central role this property is read-only.

services

A list of bleio.Service that are offered by this device. (read-only) For a Peripheral device, this list will contain services added using add_service, for a Central, this list will be empty until a connection is established, at which point it will be filled with the remote device’s services.

add_service(service)

Appends the service to the list of this devices’s services. This method can only be called for Peripheral devices.

Parameters:service (bleio.Service) – the service to append
connect()

Attempts a connection to the remote device. If the connection is successful, the device’s services are available via services. This method can only be called for Central devices.

disconnect()

Disconnects from the remote device. This method can only be called for Central devices.

start_advertising(connectable=True)

Starts advertising the device. The device’s name and services are put into the advertisement packets. If connectable is True then other devices are allowed to conncet to this device. This method can only be called for Peripheral devices.

stop_advertising()

Disconnects from the remote device. This method can only be called for Peripheral devices.