Skip to content

Commit

Permalink
Merge pull request #422 from xloem/p4a-beta
Browse files Browse the repository at this point in the history
python-for-android backend
  • Loading branch information
dlech authored Oct 17, 2021
2 parents 198aad1 + 9598990 commit 87773f2
Show file tree
Hide file tree
Showing 25 changed files with 1,941 additions and 3 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [ master, develop ]

jobs:
build_linux:
build_desktop:
name: "Build and test"
runs-on: ${{ matrix.os }}
strategy:
Expand Down Expand Up @@ -36,3 +36,27 @@ jobs:
path: junit/test-results-${{ matrix.os }}-${{ matrix.python-version }}.xml
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}
build_android:
name: "Build Android"
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Upgrade pip. setuptools and wheel
run: python -m pip install --upgrade pip setuptools wheel
- name: Install dependencies
run: pip install buildozer cython
- name: Cache buildozer files
uses: actions/cache@v2
id: buildozer-cache
with:
path: |
~/.buildozer
examples/kivy/.buildozer
key: build-cache-buildozer
- name: Clean bleak recipe for cache
if: steps.buildozer-cache.outputs.cache-hit == 'true'
working-directory: examples/kivy
run: buildozer android p4a -- clean-recipe-build --local-recipes $(pwd)/../../bleak/backends/p4android/recipes bleak
- name: Build Kivy example
working-directory: examples/kivy
run: buildozer android debug
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Added
* Added ``register_uuids()`` to augment the uuid-to-description mapping.
* Added support for Python 3.10.
* Added ``force_indicate`` keyword argument for WinRT backend client's ``start_notify`` method. Fixes #526.
* Added python-for-android backend.

Changed
-------
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Features
* Supports Windows 10, version 16299 (Fall Creators Update) or greater
* Supports Linux distributions with BlueZ >= 5.43
* OS X/macOS support via Core Bluetooth API, from at least OS X version 10.11
* Android backend compatible with python-for-android

Bleak supports reading, writing and getting notifications from
GATT servers, as well as a function for discovering BLE devices.
Expand Down
7 changes: 7 additions & 0 deletions bleak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@

if _on_rtd:
pass
elif os.environ.get("P4A_BOOTSTRAP") is not None:
from bleak.backends.p4android.scanner import (
BleakScannerP4Android as BleakScanner,
) # noqa: F401
from bleak.backends.p4android.client import (
BleakClientP4Android as BleakClient,
) # noqa: F401
elif platform.system() == "Linux":
if not _on_ci and not check_bluez_version(5, 43):
raise BleakError("Bleak requires BlueZ >= 5.43.")
Expand Down
Empty file.
90 changes: 90 additions & 0 deletions bleak/backends/p4android/characteristic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from uuid import UUID
from typing import Union, List

from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak.backends.descriptor import BleakGATTDescriptor
from bleak.exc import BleakError


from . import defs


class BleakGATTCharacteristicP4Android(BleakGATTCharacteristic):
"""GATT Characteristic implementation for the python-for-android backend"""

def __init__(self, java, service_uuid: str, service_handle: int):
super(BleakGATTCharacteristicP4Android, self).__init__(java)
self.__uuid = self.obj.getUuid().toString()
self.__handle = self.obj.getInstanceId()
self.__service_uuid = service_uuid
self.__service_handle = service_handle
self.__descriptors = []
self.__notification_descriptor = None

self.__properties = [
name
for flag, name in defs.CHARACTERISTIC_PROPERTY_DBUS_NAMES.items()
if flag & self.obj.getProperties()
]

@property
def service_uuid(self) -> str:
"""The uuid of the Service containing this characteristic"""
return self.__service_uuid

@property
def service_handle(self) -> int:
"""The integer handle of the Service containing this characteristic"""
return int(self.__service_handle)

@property
def handle(self) -> int:
"""The handle of this characteristic"""
return self.__handle

@property
def uuid(self) -> str:
"""The uuid of this characteristic"""
return self.__uuid

@property
def properties(self) -> List:
"""Properties of this characteristic"""
return self.__properties

@property
def descriptors(self) -> List:
"""List of descriptors for this service"""
return self.__descriptors

def get_descriptor(
self, specifier: Union[str, UUID]
) -> Union[BleakGATTDescriptor, None]:
"""Get a descriptor by UUID (str or uuid.UUID)"""
if isinstance(specifier, int):
raise BleakError(
"The Android Bluetooth API does not provide access to descriptor handles."
)

matches = [
descriptor
for descriptor in self.descriptors
if descriptor.uuid == str(specifier)
]
if len(matches) == 0:
return None
return matches[0]

def add_descriptor(self, descriptor: BleakGATTDescriptor):
"""Add a :py:class:`~BleakGATTDescriptor` to the characteristic.
Should not be used by end user, but rather by `bleak` itself.
"""
self.__descriptors.append(descriptor)
if descriptor.uuid == defs.CLIENT_CHARACTERISTIC_CONFIGURATION_UUID:
self.__notification_descriptor = descriptor

@property
def notification_descriptor(self) -> BleakGATTDescriptor:
"""The notification descriptor. Mostly needed by `bleak`, not by end user"""
return self.__notification_descriptor
Loading

0 comments on commit 87773f2

Please sign in to comment.