-
Notifications
You must be signed in to change notification settings - Fork 294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python-for-android backend #422
Conversation
and an example for kivy buildozer on android
Good work! This is interesting, but it needs some additional handling first. It needs some documentation and some clear descriptions on how to use. Other questions:
|
Is it pretty obvious what docs need updating here, for when I come back to commit changes? EDITS to self:
|
Thanks for the answers; I would very much like to make Bleak compatible with Beeware solutions eventually, so I wanted to hear your thoughts on that! The docs that need updating are here and then adding api-references here. In combination with the example and README that you have already written, could you please include some links or references on where to start reading about how to get python-for-android onto the phone? Also, |
@hbldh I threw something together for docs. The p4a backend uses the |
@xloem Sorry for taking time to merge this. I am interested in adding this, but there are several other things needing triage and subsequent release. I am planning on making a 0.11.0 release with a lot of potentially problematic code, and I want to wait with this until after that release to merge this to develop. Just so you know. |
I have only glanced at the code at this point, so I can't comment on its technical correctness. Having Here are some references I found (I was unable to confirm if it is one per CUUID, device, or stack before abandoning the third-party code base I was working with.)
To help prevent others' frustrations, this should at least be documented. |
@jeffsf Thank you for your comments. I will take this under consideration; I have not tested this Android solution myself yet and need to confirm that it does what it needs to be included in Bleak. |
i have been testing his commits and they seem to be working well i was able to write a app that talks over nordic ble uart |
Is there a way to get this up and running in 15 minutes or less so we can test it easily? I've looked a few times but I don't see a way to just run a simple command line python file on Android. |
It takes longer than 15 minutes for python4android or buildozer to install. I might be able to find time to set up a docker image or something, do you need that? Otherwise, see https://github.com/hbldh/bleak/blob/7aa7532fe28f4341746762a4ff94980833f5baa0/examples/kivy/README |
I didn't realize that buildozer magically does most of the work for you. I think that should be good enough to get me started. |
The arch argument of get_site_packages_dir() is no longer optional. Comes from changes in kivy/python-for-android#2467
How do you get buildozer to rebuild bleak when changes are made? |
I'm so sorry, I don't remember, you'd have to ask in python4android communities. But I think it's pretty reliable to delete the buildozer build folder, and I think there are some specific subfolders inside it you can delete instead to speed the rebuild up. I'm not sure what they are, I'm afraid. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've had a fairly good look at this now and it looks really good. I pushed a bunch of changes to bring things up to date with the develop branch and get it working again and fix a few bugs. I still have a few questions (see inline comments).
Also see the CI results for some issues with the docs.
Building from this directory as suggested in the README creates local folders that should be ignored.
This was converted from a method to a property in hbldh#495
This change was made in other backends in 0f56dab.
This was checking disconnected instead of connected
This was implemented for other backends in 62f45b0. Since Android doesn't automatically negotiate MTU, we have to do that when connecting to the device.
If there is a failure after connecting but before the connect method returns, we need to disconnect, otherwise we end up in a bad state.
returning a bool from the disconnect() method is for backwards compatilbity only and should always be True. If disconnecting failed, an exception should be raised.
asyncio.run() does the same thing, but probably better.
Using a context manager simplifes the code.
per dlech suggestion Co-authored-by: David Lechner <david@lechnology.com>
had a marathon today of addressing most of these things. haven't figured out the recipe building thing yet at this time. |
.github/workflows/build_and_test.yml
Outdated
with: | ||
path: | | ||
~/.buildozer | ||
examples/kivy/.buildozer | ||
key: build-cache-buildozer | ||
- name: Build Kivy example | ||
working-directory: examples/kivy | ||
run: buildozer android debug | ||
- name: Clean bleak recipe for cache | ||
working-directory: examples/kivy | ||
run: buildozer android p4a -- clean_recipe_build --local-recipes $(pwd)/../../bleak/backends/p4android/recipes bleak |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the clean be before the build so that bleak is rebuilt? We can also add a check to avoid the clean if there is no cache.
with: | |
path: | | |
~/.buildozer | |
examples/kivy/.buildozer | |
key: build-cache-buildozer | |
- name: Build Kivy example | |
working-directory: examples/kivy | |
run: buildozer android debug | |
- name: Clean bleak recipe for cache | |
working-directory: examples/kivy | |
run: buildozer android p4a -- clean_recipe_build --local-recipes $(pwd)/../../bleak/backends/p4android/recipes bleak | |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine, but it does make the cache slightly bigger which slightly increases the time to download it. I imagine the difference is negligible. The end result is the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see what you mean. Fine to leave it as-is then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: move recipe cleaning to after compilation, so that the recipe is not included in the cache. Toggle the condition so it only runs if the cache was missed.
… PR recommendation Co-authored-by: David Lechner <david@lechnology.com>
bleak/backends/p4android/defs.py
Outdated
SCAN_FAILED_NAMES = { | ||
ScanCallback.SCAN_FAILED_ALREADY_STARTED: "SCAN_FAILED_ALREADY_STARTED", | ||
ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED", | ||
ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED: "SCAN_FAILED_FEATURE_UNSUPPORTED", | ||
ScanCallback.SCAN_FAILED_INTERNAL_ERROR: "SCAN_FAILED_INTERNAL_ERROR", | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize before that the java code doesn't have enums. So instead of the string lookup like this, another option could be to make a Python enum. Then we can just cast the values returned from java code to this type. Then the enum value can be passed directly to format strings and magically be converted to a string without having to manually do the conversion.
SCAN_FAILED_NAMES = { | |
ScanCallback.SCAN_FAILED_ALREADY_STARTED: "SCAN_FAILED_ALREADY_STARTED", | |
ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED: "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED", | |
ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED: "SCAN_FAILED_FEATURE_UNSUPPORTED", | |
ScanCallback.SCAN_FAILED_INTERNAL_ERROR: "SCAN_FAILED_INTERNAL_ERROR", | |
} | |
class ScanFailed(IntEnum): | |
ALREADY_STARTED = ScanCallback.SCAN_FAILED_ALREADY_STARTED | |
APPLICATION_REGISTRATION_FAILED = ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED | |
FEATURE_UNSUPPORTED = ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED | |
INTERNAL_ERROR = ScanCallback.SCAN_FAILED_INTERNAL_ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: convert this to an enum. the other change to suppor tthe suggestion is changing the dictionary lookup in SCAN_FAILED_NAMES to a constructor call to ScanFailed in bleak/backends/p4android/scanner.py
TODO: it might make sense to bring back the other name dictionary and turn it into an enum, to keep code maintenance more parallel and improve debuggability, but it is not necessary
This reverts commit 6c1e5e9.
The values in CONTROLLER_ERROR_CODES overlap with PROTOCOL_ERROR_CODES so we will need a separate lookup table if these error codes are actually needed.
To be like other backends, use bytes instead of bytearray for service and manufacturer data. Also simplify code a bit while we are touching this.
Merged! Thanks again for all of the amazing work! |
I also forgot to say that you should feel free to add yourself to the AUTHORS file if you like. |
This is my past week or so of work on a python-for-android backend. Python-for-android is primarily used by kivy, a python gui toolkit.
This code is working for me, but I haven't tested features my test device doesn't use, such as pairing. There's value to merging this code, because I'm not aware of any other python-for-android bluetooth libraries, maybe with a comment to report to me any bugs encountered, with logcat output, or somesuch.
If this isn't a good addition to this project, the python-for-android project might consider it as a pull request, too, as part of their android interface library.
I'd appreciate criticism on my implementation choices. Most of my energy was spent just making it work.
My development history on this PR is at https://github.com/xloem/bleak/tree/p4a .
EDIT: lint failures have been addressed