Skip to content

Commit 7db2fd5

Browse files
authored
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.
1 parent 256a5be commit 7db2fd5

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Contributors
2020
* Jonathan Soto <[email protected]>
2121
* Kyle J. Williams <[email protected]>
2222
* Edward Betts <[email protected]>
23+
* Robbe Gaeremynck <[email protected]>
2324

2425
Sponsors
2526
--------

CHANGELOG.rst

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Changed
2323
* Scanner backends modified to allow multiple advertisement callbacks. Merged #1367.
2424
* Changed default handling of the ``response`` argument in ``BleakClient.write_gatt_char``.
2525
Fixes #909.
26+
* Added missing permissions and requirements in android kivy example. Fixes #1184.
27+
* Bleak recipe now automatically installs bleak from github release.
2628
* Changed `BlueZManager` methods to raise `BleakError` when device is not in BlueZ.
2729

2830
Fixed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1+
import os
2+
13
from pythonforandroid.recipe import PythonRecipe
24
from pythonforandroid.toolchain import shprint, info
35
import sh
46
from os.path import join
57

68

79
class BleakRecipe(PythonRecipe):
8-
version = None
9-
url = None
10+
version = None # Must be none for p4a to correctly clone repo
11+
fix_setup_py_version = "bleak develop branch"
12+
url = "git+https://github.com/hbldh/bleak.git"
1013
name = "bleak"
1114

12-
src_filename = join("..", "..", "..", "..", "..")
13-
1415
depends = ["pyjnius"]
1516
call_hostpython_via_targetpython = False
1617

18+
fix_setup_filename = "fix_setup.py"
19+
1720
def prepare_build_dir(self, arch):
18-
shprint(sh.rm, "-rf", self.get_build_dir(arch))
19-
shprint(
20-
sh.ln,
21-
"-s",
22-
join(self.get_recipe_dir(), self.src_filename),
23-
self.get_build_dir(arch),
24-
)
21+
super().prepare_build_dir(arch) # Unpack the url file to the get_build_dir
22+
build_dir = self.get_build_dir(arch)
23+
24+
setup_py_path = join(build_dir, "setup.py")
25+
if not os.path.exists(setup_py_path):
26+
# Perform the p4a temporary fix
27+
# At the moment, p4a recipe installing requires setup.py to be present
28+
# So, we create a setup.py file only for android
29+
30+
fix_setup_py_path = join(self.get_recipe_dir(), self.fix_setup_filename)
31+
with open(fix_setup_py_path, "r") as f:
32+
contents = f.read()
33+
34+
# Write to the correct location and fill in the version number
35+
with open(setup_py_path, "w") as f:
36+
f.write(contents.replace("[VERSION]", self.fix_setup_py_version))
37+
else:
38+
info("setup.py found in bleak directory, are you installing older version?")
2539

2640
def get_recipe_env(self, arch=None, with_flags_in_cc=True):
2741
env = super().get_recipe_env(arch, with_flags_in_cc)
@@ -33,13 +47,12 @@ def postbuild_arch(self, arch):
3347
super().postbuild_arch(arch)
3448

3549
info("Copying java files")
36-
37-
destdir = self.ctx.javaclass_dir
50+
dest_dir = self.ctx.javaclass_dir
3851
path = join(
3952
self.get_build_dir(arch.arch), "bleak", "backends", "p4android", "java", "."
4053
)
4154

42-
shprint(sh.cp, "-a", path, destdir)
55+
shprint(sh.cp, "-a", path, dest_dir)
4356

4457

4558
recipe = BleakRecipe()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from setuptools import find_packages, setup
2+
3+
VERSION = "[VERSION]" # Version will be filled in by the bleak recipe
4+
NAME = "bleak"
5+
6+
setup(
7+
name=NAME,
8+
version=VERSION,
9+
packages=find_packages(exclude=("tests", "examples", "docs")),
10+
)

examples/kivy/README.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
This is a kivy application that lists scanned devices in a desktop window.
1+
## This is a kivy application that lists scanned devices in a desktop window
2+
3+
- An iOS backend has not been implemented yet.
4+
5+
- This kivy example can also be run on desktop.
6+
7+
The default target architecture is arm64-v8a.
8+
If you have an older device, change it in the buildozer.spec file (android.archs = arch1, arch2, ..).
9+
Multiple targets are allowed (will significantly increase build time).
210

311
It can be run on Android via:
412

@@ -7,11 +15,23 @@ It can be run on Android via:
715
# connect phone with USB and enable USB debugging
816
buildozer android deploy run logcat
917

18+
## To use with local version of bleak source:
19+
20+
Local source path can be specified using the P4A_bleak_DIR environment variable:
21+
22+
P4A_bleak_DIR="path to bleak source" buildozer android debug
23+
24+
25+
1026
Note: changes to `bleak/**` will not be automatically picked up when rebuilding.
1127
Instead the recipe build must be cleaned:
1228

1329
buildozer android p4a -- clean_recipe_build --local-recipes $(pwd)/../../bleak/backends/p4android/recipes bleak
1430

15-
An iOS backend has not been implemented yet.
31+
## To use bleak in your own app:
32+
33+
- Copy the bleak folder under bleak/backends/p4android/recipes into the app recipes folder.
34+
Make sure that 'local_recipes' in buildozer.spec points to the app recipes folder.
35+
The latest version of bleak will be installed automatically.
1636

17-
This kivy example can also be run on desktop.
37+
- Add 'bleak' and it's dependencies to the requirements in your buildozer.spec file.

examples/kivy/buildozer.spec

+15-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ version = 0.1.0
3838

3939
# (list) Application requirements
4040
# comma separated e.g. requirements = sqlite3,kivy
41-
requirements = python3,kivy,bleak,async_to_sync
41+
requirements =
42+
python3,
43+
kivy,
44+
bleak,
45+
async_to_sync,
46+
async-timeout
4247

4348
# (str) Custom source folders for requirements
4449
# Sets custom source for any requirements with recipes
@@ -90,7 +95,14 @@ fullscreen = 0
9095
#android.presplash_lottie = "path/to/lottie/file.json"
9196

9297
# (list) Permissions
93-
android.permissions = BLUETOOTH,BLUETOOTH_ADMIN,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION,ACCESS_BACKGROUND_LOCATION
98+
android.permissions =
99+
BLUETOOTH,
100+
BLUETOOTH_SCAN,
101+
BLUETOOTH_CONNECT,
102+
BLUETOOTH_ADMIN,
103+
ACCESS_FINE_LOCATION,
104+
ACCESS_COARSE_LOCATION,
105+
ACCESS_BACKGROUND_LOCATION
94106

95107
# (list) features (adds uses-feature -tags to manifest)
96108
#android.features = android.hardware.usb.host
@@ -225,7 +237,7 @@ android.accept_sdk_license = True
225237
#android.copy_libs = 1
226238

227239
# (str) The Android arch to build for, choices: armeabi-v7a, arm64-v8a, x86, x86_64
228-
android.arch = armeabi-v7a
240+
android.archs = arm64-v8a
229241

230242
# (int) overrides automatic versionCode computation (used in build.gradle)
231243
# 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
243255

244256
# (str) python-for-android fork to use, defaults to upstream (kivy)
245257
#p4a.fork = kivy
246-
#p4a.fork = xloem
247258

248259
# (str) python-for-android branch to use, defaults to master
249260
#p4a.branch = master

0 commit comments

Comments
 (0)