platypush.plugins.bluetooth

class platypush.plugins.bluetooth.BluetoothPlugin(device_id: int = - 1, **kwargs)[source]

Bluetooth plugin

Requires:

  • pybluez (pip install pybluez)

  • pyobex (pip install git+https://github.com/BlackLight/PyOBEX)

__init__(device_id: int = - 1, **kwargs)[source]
Parameters

device_id – Default adapter device_id to be used (default: -1, auto)

close(device: str = None, port: int = None, service_uuid: str = None, service_name: str = None)[source]

Close an active bluetooth connection

Parameters
  • device – Device address or name

  • port – Port number

  • service_uuid – Service UUID

  • service_name – Service name

connect(protocol=None, device: str = None, port: int = None, service_uuid: str = None, service_name: str = None)[source]

Connect to a bluetooth device. You can query the advertised services through find_service.

Parameters
  • protocol – Supported values: either ‘RFCOMM’/’L2CAP’ (str) or bluetooth.RFCOMM/bluetooth.L2CAP int constants (int)

  • device – Device address or name

  • port – Port number

  • service_uuid – Service UUID

  • service_name – Service name

find_service(name: str = None, addr: str = None, uuid: str = None)platypush.message.response.bluetooth.BluetoothLookupServiceResponse[source]

Look up for a service published by a nearby bluetooth device. If all the parameters are null then all the published services on the nearby devices will be returned. See :class:platypush.message.response.bluetoothBluetoothLookupServiceResponse for response structure reference.

Parameters
  • name – Service name

  • addr – Service/device address

  • uuid – Service UUID

get_measurement(device_id: Optional[int] = None, duration: Optional[int] = 10, *args, **kwargs)Dict[str, dict][source]

Wrapper for scan that returns bluetooth devices in a format usable by sensor backends.

Parameters
  • device_id – Bluetooth adapter ID to use (default configured if None)

  • duration – Scan duration in seconds

Returns

Device address -> info map.

lookup_address(name: str, timeout: int = 10)platypush.message.response.bluetooth.BluetoothLookupNameResponse[source]

Look up the address of a nearby bluetooth device given the name

Parameters
  • name – Device name

  • timeout – Lookup timeout (default: 10 seconds)

lookup_name(addr: str, timeout: int = 10)platypush.message.response.bluetooth.BluetoothLookupNameResponse[source]

Look up the name of a nearby bluetooth device given the address

Parameters
  • addr – Device address

  • timeout – Lookup timeout (default: 10 seconds)

recv(device: str, port: int, service_uuid: str = None, service_name: str = None, size: int = 1024, binary: bool = False)platypush.message.response.bluetooth.BluetoothResponse[source]

Send data to an active bluetooth connection

Parameters
  • device – Device address or name

  • port – Port number

  • service_uuid – Service UUID

  • service_name – Service name

  • size – Maximum number of bytes to be read

  • binary – Set to true to return a base64-encoded binary string

scan(device_id: Optional[int] = None, duration: int = 10)platypush.message.response.bluetooth.BluetoothScanResponse[source]

Scan for nearby bluetooth devices

Parameters
  • device_id – Bluetooth adapter ID to use (default configured if None)

  • duration – Scan duration in seconds

send(data, device: str = None, port: int = None, service_uuid: str = None, service_name: str = None, binary: bool = False)[source]

Send data to an active bluetooth connection

Parameters
  • data – Data to be sent

  • device – Device address or name

  • service_uuid – Service UUID

  • service_name – Service name

  • port – Port number

  • binary – Set to true if msg is a base64-encoded binary string

send_file(filename: str, device: str, port: int = None, data=None, service_name='OBEX Object Push', binary: bool = False)[source]

Send a local file to a device that exposes an OBEX Object Push service

Parameters
  • filename – Path of the file to be sent

  • data – Alternatively to a file on disk you can send raw (string or binary) content

  • device – Device address or name

  • port – Port number

  • service_name – Service name

  • binary – Set to true if data is a base64-encoded binary string

set_l2cap_mtu(mtu: int, device: str = None, port: int = None, service_name: str = None, service_uuid: str = None)[source]

Set the L2CAP MTU (Maximum Transmission Unit) value for a connected bluetooth device. Both the devices usually use the same MTU value over a connection.

Parameters
  • device – Device address or name

  • port – Port number

  • service_uuid – Service UUID

  • service_name – Service name

  • mtu – New MTU value