Skip to content

Commit

Permalink
Update of bleak on android and the android kivy example (hbldh#1398)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
robgar2001 authored Aug 29, 2023
1 parent 256a5be commit 7db2fd5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 21 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Contributors
* Jonathan Soto <[email protected]>
* Kyle J. Williams <[email protected]>
* Edward Betts <[email protected]>
* Robbe Gaeremynck <[email protected]>

Sponsors
--------
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 27 additions & 14 deletions bleak/backends/p4android/recipes/bleak/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import os

from pythonforandroid.recipe import PythonRecipe
from pythonforandroid.toolchain import shprint, info
import sh
from os.path import join


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)
Expand All @@ -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()
10 changes: 10 additions & 0 deletions bleak/backends/p4android/recipes/bleak/fix_setup.py
Original file line number Diff line number Diff line change
@@ -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")),
)
26 changes: 23 additions & 3 deletions examples/kivy/README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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.
19 changes: 15 additions & 4 deletions examples/kivy/buildozer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7db2fd5

Please sign in to comment.