# Android backend

For an example of building an android bluetooth app, see the example folder and its accompanying README file.

There are a handful of ways to run Python on Android. Presently some code has been written for the Python-for-Android build tool, and the code has only been tested using the Kivy Framework. The Kivy framework provides a way to make graphical applications using bluetooth that run on both android and desktop.

An alternative framework is BeeWare. An implementation for BeeWare would likely be very similar to Python-for-Android, if anybody is interested in contributing one. As of 2020, the major task to tackle is making a custom template to embed Java subclasses of the Bluetooth Android interfaces, for forwarding callbacks.

The Python-for-Android backend classes are found in the `bleak.backends.p4android` package and are automatically selected when building with python-for-android or Buildozer, Kivy’s automated build tool.

## Considerations on Android

For one thing, the python-for-android backend has not been fully tested. Please run applications with `adb logcat` or `buildozer android logcat` and file issues that include the output, so that any compatibility concerns with devices the developer did not own can be eventually addressed. This backend was originally authored by @xloem for a project that has mostly wrapped up now, so it would be good to tag him in the issues.

When fixing issues, often the Android documentation is lacking, and other resources may need to be consulted to find information on various device quirks, such as community developer forums.

Sometimes device drivers will give off new, undocumented error codes. There is a developing list of these at `bleak.backends.p4android.defs.GATT_STATUS_NAMES`. Please add to the list if you find new status codes, which is indicated by a number being reported instead of a name.

Additionally a few small features are missing. Please file an issue if you need a missing feature, and ideally contribute code, so that soon they will all be implemented.

Two missing features include scanning filters and indications (notifications without replies).

Additionally reading from a characteristic has not been tested at all, as xloem’s test device did not provide for this.

On Android, Bluetooth needs permissions for access. These permissions need to be added to the android application in the buildozer.spec file, and are also requested from the user at runtime. This means that enabling bluetooth may not succeed if the user does not accept permissions.

## API

### Scanner

class bleak.backends.p4android.scanner.BleakScannerP4Android(detection_callback: Callable[[BLEDevice, AdvertisementData], Coroutine[Any, Any, None] | None] | None, service_uuids: list[str] | None, scanning_mode: Literal['active', 'passive'], **kwargs)

  The python-for-android Bleak BLE Scanner.

  Parameters:

    - detection_callback – Optional function that will be called each time a device is discovered or advertising data has changed.

    - service_uuids – Optional list of service UUIDs to filter on. Only advertisements containing this advertising data will be received. Specifying this also enables scanning while the screen is off on Android.

    - scanning_mode – Set to `"passive"` to avoid the `"active"` scanning mode.

  async start() → None

    Start scanning for devices

  async stop() → None

    Stop scanning for devices

### Client

BLE Client for python-for-android

class bleak.backends.p4android.client.BleakClientP4Android(address_or_ble_device: BLEDevice | str, services: set[UUID] | None, **kwargs)

  A python-for-android Bleak Client

  Parameters:

    - address_or_ble_device – The Bluetooth address of the BLE peripheral to connect to or the `BLEDevice` object representing it.

    - services – Optional set of services UUIDs to filter.

  async connect(pair: bool, **kwargs) → None

    Connect to the specified GATT server.

  async disconnect() → None

    Disconnect from the specified GATT server.

  property is_connected: bool

    Check connection status between this client and the server.

    Returns:

      Boolean representing connection status.

  property mtu_size: int

    Gets the negotiated MTU.

  async pair(*args, **kwargs) → None

    Pair with the peripheral.

    You can use ConnectDevice method if you already know the MAC address of the device. Else you need to StartDiscovery, Trust, Pair and Connect in sequence.

  async read_gatt_char(characteristic: BleakGATTCharacteristic, *, use_cached: bool = False, **kwargs: Any) → bytearray

    Perform read operation on the specified GATT characteristic.

    Parameters:

      characteristic (BleakGATTCharacteristic) – The characteristic to read from.

    Returns:

      (bytearray) The read data.

  async read_gatt_descriptor(descriptor: BleakGATTDescriptor, *, use_cached: bool = False, **kwargs: Any) → bytearray

    Perform read operation on the specified GATT descriptor.

    Parameters:

      - descriptor – The descriptor to read from.

      - use_cached – Whether to use cached value.

    Returns:

      The read data.

  async start_notify(characteristic: BleakGATTCharacteristic, callback: Callable[[bytearray], None], **kwargs) → None

    Activate notifications/indications on a characteristic.

  async stop_notify(characteristic: BleakGATTCharacteristic) → None

    Deactivate notification/indication on a specified characteristic.

    Parameters:

      characteristic (BleakGATTCharacteristic) – The characteristic to deactivate notification/indication on,.

  async unpair() → None

    Unpair with the peripheral.

  async write_gatt_char(characteristic: BleakGATTCharacteristic, data: bytearray, response: bool) → None

    Perform a write operation on the specified GATT characteristic.

    Parameters:

      - characteristic – The characteristic to write to.

      - data – The data to send.

      - response – If write-with-response operation should be done.

  async write_gatt_descriptor(desc_specifier: BleakGATTDescriptor | str | UUID, data: bytearray) → None

    Perform a write operation on the specified GATT descriptor.

    Parameters:

      - desc_specifier (BleakGATTDescriptor, str or UUID) – The descriptor to write to, specified by either UUID or directly by the BleakGATTDescriptor object representing it.

      - data (bytes or bytearray) – The data to send.