Skip to content
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

Update bonding v2 merged with main and tested. #768

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

CristianMG
Copy link

@CristianMG CristianMG commented Oct 4, 2024

This PR includes the following key changes:

Merged with main branch
Replaced the value of NOT_EXPORTED with EXPORTED.
Motivation: Although the Android developer documentation doesn't mention this explicitly, through my testing and by finding examples in other codebases, I realized that this is necessary for proper functionality.

For example, the Nordic Semiconductor BLE library includes this in their implementation: Example from Nordic Semiconductor BLE Library

Executing on the main thread: Broadcasting should be registered and executed on the main thread, as this is a requirement for correct operation.

Another thing, added waitForBonding to connect constructor function I think that's can be usefull because you can control bonding at the connect which in some cases is a must for all operations. Maybe including in the main call can be little aggresive to the api be free to indicate another place to put this functionality.

I will work on make a little piece of software to execute in raspberry or similar to be able to test by your part. I'll keep on touch with you through this pull request.

…State Changes

Update: Register broadcast receiver in the main thread and add support for Bluetooth bond state changes  Update: Register broadcast receiver in the main thread and add support for Bluetooth bond state changes
@twyatt
Copy link
Member

twyatt commented Oct 7, 2024

Thanks for this! It is great to have a branch that is tested and known working.

I've also updated #494 with the changes that will be going into the upcoming Kable release. Although I have not been able to test my branch (I'm very excited for being able to test against custom Arduino or Raspberry Pi code though, when that becomes available).

@twyatt twyatt added minor Changes that should bump the MINOR version number android labels Oct 7, 2024
@CristianMG
Copy link
Author

Hi @twyatt ,

I’ve just finished developing a Python script for Raspberry and an example app in Jetpack Compose, both aimed at testing the pairing process using the library you created. You can find the script at this link: kable-ble-test.

In particular, in "Just Works" mode on devices running Android 13 and later, pairing is always requested due to a change in the Bluetooth stack. However, in Android 12 and earlier, the device connects without requiring authentication, and when a feature that requires bonding is executed, it initiates the authentication process. I’ve also observed that if bonding has already been performed on Android 12 and the keys are then deleted from the Android device, bonding is requested again during the next connection attempt.

This raises doubts for me about the waitBonding part in the connect method, as it seems there is no predictable behavior across different Android versions. Perhaps it would be better to remove this part and revert to the original implementation, which waits on the read or write methods.

I’ve been observing these behaviors for a while, as I have extensive experience working with Android and the BLE stack, which seems to have quite a few bugs in the latest versions due to the stack refactoring.

I’d love to hear if you’ve received similar reports from other users or if you have any recommendations on how to handle these variations. I truly appreciate your work and hope to contribute to the community with my findings.

Thank you!

Image of python script log:
image

Image of example application:

Screenshot_20241009_154733

Bonding process:
Screenshot_2024-10-09-15-52-49-397_com android settings
Screenshot_2024-10-09-15-52-36-395_com kable androidsample

@twyatt
Copy link
Member

twyatt commented Oct 9, 2024

Wow, thanks so much! This is great!
I'll try to find time this weekend to spin up the Raspberry Pi script and try it out.

This is exactly what I needed to unblock progress on bonding support.

I'll be in touch if I run into any issues or have any questions.

Thanks again, this is so incredibly helpful!

@twyatt

This comment was marked as outdated.

@twyatt

This comment was marked as resolved.

@CristianMG
Copy link
Author

I made some changes in requirements.txt, could you try again?

@twyatt
Copy link
Member

twyatt commented Oct 21, 2024

I made some changes in requirements.txt, could you try again?

Thanks for the update, @CristianMG!

I was able to get everything setup per your instructions (and some additional dependency installations):

$ pip install virtualenv
$ virtualenv kable-env
$ source kable-env/bin/activate

# https://stackoverflow.com/a/62510708
$ sudo apt install build-essential libdbus-glib-1-dev libgirepository1.0-dev
$ pip install dbus-python

# https://stackoverflow.com/a/23649012
$ sudo apt-get install libcairo2-dev libjpeg-dev libgif-dev

$ pip install -r requirements.txt

Unfortunately, now it is failing with:

$ python cputemp.py

Traceback (most recent call last):
  File "/home/travis/kable-ble-test/cputemp.py", line 27, in <module>
    from ble_agent import Agent, register_agent
  File "/home/travis/kable-ble-test/ble_agent.py", line 4, in <module>
    from gi.repository import GLib
  File "/home/travis/kable-ble-test/kable-env/lib/python3.11/site-packages/gi/__init__.py", line 39
    print url
    ^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

Perhaps I'm using the wrong Python version?

$ python --version

Python 3.11.2

@CristianMG
Copy link
Author

I hate that error I can't fix properly it's seems related with this libraries
PyGObject==3.50.0
gi==1.2
Sometimes I simply fix playing with the version of this libraries and re-installing after requirerements

pip install PyGObject==3.48.2
I know that´s weird as you say some people says it´s related with the interpreter version. If you can´t fix I will investigate further if this error is related with version interpreter

@twyatt
Copy link
Member

twyatt commented Oct 21, 2024

Thanks for the tips! Got it working.

I ended up getting it running by deleting the gi=1.2 line from the requirements.txt file and re-spinning up the virtual environment.

I'm assuming it worked because I have a system version of the gi package installed that is Python3 compatible, whereas perhaps gi 1.2 is Python 2 only?

@CristianMG
Copy link
Author

CristianMG commented Oct 22, 2024

Maybe, but in my experience if you delete all gi references threw error I mean delete from pi, that´s the reason I think this error is weird. Whatever if this script become a standard to test the library would improve with more test cases and eventually I´ll fix this bug the reason that I have to involved in the improvement or fixing library errors is that the company I work use this library in some projects related with hardware.

@twyatt
Copy link
Member

twyatt commented Oct 27, 2024

@CristianMG I'm going to continue the conversation in #111.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android minor Changes that should bump the MINOR version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants