From 7db2fd5ef35fb6fdb26bcc3d3628f5b76880632f Mon Sep 17 00:00:00 2001 From: Robbe Gaeremynck Date: Tue, 29 Aug 2023 17:49:18 +0200 Subject: [PATCH] Update of bleak on android and the android kivy example (#1398) * Android : updated requirements, permissions and archs in buildozer.spec * Fixed bleak recipe for android The current develop branch of p4a uses the setup.py to install the recipe and doesn't seem compliant with PEP517 yet. The temporary fix is to provide a setup.py during the build stage for android. Also, the recipe is now more generic, so it can easily be used in your own projects. --- AUTHORS.rst | 1 + CHANGELOG.rst | 2 + .../p4android/recipes/bleak/__init__.py | 41 ++++++++++++------- .../p4android/recipes/bleak/fix_setup.py | 10 +++++ examples/kivy/README.md | 26 ++++++++++-- examples/kivy/buildozer.spec | 19 +++++++-- 6 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 bleak/backends/p4android/recipes/bleak/fix_setup.py diff --git a/AUTHORS.rst b/AUTHORS.rst index 503a5757..d4e73d8e 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -20,6 +20,7 @@ Contributors * Jonathan Soto * Kyle J. Williams * Edward Betts +* Robbe Gaeremynck Sponsors -------- diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7fcc2cb1..6702d584 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -23,6 +23,8 @@ Changed * Scanner backends modified to allow multiple advertisement callbacks. Merged #1367. * Changed default handling of the ``response`` argument in ``BleakClient.write_gatt_char``. Fixes #909. +* Added missing permissions and requirements in android kivy example. Fixes #1184. +* Bleak recipe now automatically installs bleak from github release. * Changed `BlueZManager` methods to raise `BleakError` when device is not in BlueZ. Fixed diff --git a/bleak/backends/p4android/recipes/bleak/__init__.py b/bleak/backends/p4android/recipes/bleak/__init__.py index c8e2b30c..1e9c17d3 100644 --- a/bleak/backends/p4android/recipes/bleak/__init__.py +++ b/bleak/backends/p4android/recipes/bleak/__init__.py @@ -1,3 +1,5 @@ +import os + from pythonforandroid.recipe import PythonRecipe from pythonforandroid.toolchain import shprint, info import sh @@ -5,23 +7,35 @@ class BleakRecipe(PythonRecipe): - version = None - url = None + version = None # Must be none for p4a to correctly clone repo + fix_setup_py_version = "bleak develop branch" + url = "git+https://github.com/hbldh/bleak.git" name = "bleak" - src_filename = join("..", "..", "..", "..", "..") - depends = ["pyjnius"] call_hostpython_via_targetpython = False + fix_setup_filename = "fix_setup.py" + def prepare_build_dir(self, arch): - shprint(sh.rm, "-rf", self.get_build_dir(arch)) - shprint( - sh.ln, - "-s", - join(self.get_recipe_dir(), self.src_filename), - self.get_build_dir(arch), - ) + super().prepare_build_dir(arch) # Unpack the url file to the get_build_dir + build_dir = self.get_build_dir(arch) + + setup_py_path = join(build_dir, "setup.py") + if not os.path.exists(setup_py_path): + # Perform the p4a temporary fix + # At the moment, p4a recipe installing requires setup.py to be present + # So, we create a setup.py file only for android + + fix_setup_py_path = join(self.get_recipe_dir(), self.fix_setup_filename) + with open(fix_setup_py_path, "r") as f: + contents = f.read() + + # Write to the correct location and fill in the version number + with open(setup_py_path, "w") as f: + f.write(contents.replace("[VERSION]", self.fix_setup_py_version)) + else: + info("setup.py found in bleak directory, are you installing older version?") def get_recipe_env(self, arch=None, with_flags_in_cc=True): env = super().get_recipe_env(arch, with_flags_in_cc) @@ -33,13 +47,12 @@ def postbuild_arch(self, arch): super().postbuild_arch(arch) info("Copying java files") - - destdir = self.ctx.javaclass_dir + dest_dir = self.ctx.javaclass_dir path = join( self.get_build_dir(arch.arch), "bleak", "backends", "p4android", "java", "." ) - shprint(sh.cp, "-a", path, destdir) + shprint(sh.cp, "-a", path, dest_dir) recipe = BleakRecipe() diff --git a/bleak/backends/p4android/recipes/bleak/fix_setup.py b/bleak/backends/p4android/recipes/bleak/fix_setup.py new file mode 100644 index 00000000..b43d2c13 --- /dev/null +++ b/bleak/backends/p4android/recipes/bleak/fix_setup.py @@ -0,0 +1,10 @@ +from setuptools import find_packages, setup + +VERSION = "[VERSION]" # Version will be filled in by the bleak recipe +NAME = "bleak" + +setup( + name=NAME, + version=VERSION, + packages=find_packages(exclude=("tests", "examples", "docs")), +) diff --git a/examples/kivy/README.md b/examples/kivy/README.md index d4854d98..f9c0c02c 100644 --- a/examples/kivy/README.md +++ b/examples/kivy/README.md @@ -1,4 +1,12 @@ -This is a kivy application that lists scanned devices in a desktop window. +## This is a kivy application that lists scanned devices in a desktop window + +- An iOS backend has not been implemented yet. + +- This kivy example can also be run on desktop. + +The default target architecture is arm64-v8a. +If you have an older device, change it in the buildozer.spec file (android.archs = arch1, arch2, ..). +Multiple targets are allowed (will significantly increase build time). It can be run on Android via: @@ -7,11 +15,23 @@ It can be run on Android via: # connect phone with USB and enable USB debugging buildozer android deploy run logcat +## To use with local version of bleak source: + +Local source path can be specified using the P4A_bleak_DIR environment variable: + + P4A_bleak_DIR="path to bleak source" buildozer android debug + + + Note: changes to `bleak/**` will not be automatically picked up when rebuilding. Instead the recipe build must be cleaned: buildozer android p4a -- clean_recipe_build --local-recipes $(pwd)/../../bleak/backends/p4android/recipes bleak -An iOS backend has not been implemented yet. +## To use bleak in your own app: + +- Copy the bleak folder under bleak/backends/p4android/recipes into the app recipes folder. +Make sure that 'local_recipes' in buildozer.spec points to the app recipes folder. +The latest version of bleak will be installed automatically. -This kivy example can also be run on desktop. +- Add 'bleak' and it's dependencies to the requirements in your buildozer.spec file. diff --git a/examples/kivy/buildozer.spec b/examples/kivy/buildozer.spec index d5796c32..d6bbd253 100644 --- a/examples/kivy/buildozer.spec +++ b/examples/kivy/buildozer.spec @@ -38,7 +38,12 @@ version = 0.1.0 # (list) Application requirements # comma separated e.g. requirements = sqlite3,kivy -requirements = python3,kivy,bleak,async_to_sync +requirements = + python3, + kivy, + bleak, + async_to_sync, + async-timeout # (str) Custom source folders for requirements # Sets custom source for any requirements with recipes @@ -90,7 +95,14 @@ fullscreen = 0 #android.presplash_lottie = "path/to/lottie/file.json" # (list) Permissions -android.permissions = BLUETOOTH,BLUETOOTH_ADMIN,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION,ACCESS_BACKGROUND_LOCATION +android.permissions = + BLUETOOTH, + BLUETOOTH_SCAN, + BLUETOOTH_CONNECT, + BLUETOOTH_ADMIN, + ACCESS_FINE_LOCATION, + ACCESS_COARSE_LOCATION, + ACCESS_BACKGROUND_LOCATION # (list) features (adds uses-feature -tags to manifest) #android.features = android.hardware.usb.host @@ -225,7 +237,7 @@ android.accept_sdk_license = True #android.copy_libs = 1 # (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64 -android.arch = armeabi-v7a +android.archs = arm64-v8a # (int) overrides automatic versionCode computation (used in build.gradle) # this is not the same as app version and should only be edited if you know what you're doing @@ -243,7 +255,6 @@ android.allow_backup = True # (str) python-for-android fork to use, defaults to upstream (kivy) #p4a.fork = kivy -#p4a.fork = xloem # (str) python-for-android branch to use, defaults to master #p4a.branch = master