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

Android example permissions broken #1363

Open
Jayy001 opened this issue Jul 13, 2023 · 12 comments
Open

Android example permissions broken #1363

Jayy001 opened this issue Jul 13, 2023 · 12 comments
Labels
Backend: Android more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback

Comments

@Jayy001
Copy link

Jayy001 commented Jul 13, 2023

  • bleak version: 0.20.2
  • Python version: 3.10
  • Operating System: Linux z370n 5.19.0-46-generic 47~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 21 15:35:31 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • BlueZ version (bluetoothctl -v) in case of Linux:
  • android version: 11

Description

Trying to run the basic example given for android
https://github.com/hbldh/bleak/tree/develop/examples/kivy

What I Did

buildozer android debug
# connect phone with USB and enable USB debugging
buildozer android deploy run logcat

Added Java files from https://github.com/hbldh/bleak/tree/develop/bleak/backends/p4android/java/com/github/hbldh/bleak to local folder java and in buildozer.spec modifed android.add_src = java

Logs

bleak.exc.BleakError: User denied access to ['android.permission.ACCESS_FINE_LOCATION', 'android.permission.ACCESS_COARSE_LOCATION', 'android.permission.ACCESS_BACKGROUND_LOCATION']
07-13 16:36:52.948 14378 14660 I python  : Python for android ended
@dlech
Copy link
Collaborator

dlech commented Jul 19, 2023

Does it work if you change the permissions for the app on the Android device?

@dlech dlech added the more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback label Jul 19, 2023
@robgar2001
Copy link
Contributor

Hi @Jayy001

Try to install the app via: 'adb install -g app.apk', the -g flag should grant all runtime permissions, see if this fixes the error.
If the permissions where not asked during startup of the app (without the -g flag), see the android package, it provides a simple function to request permissions from the user.

@dlech
Copy link
Collaborator

dlech commented Aug 29, 2023

Should be fixed by #1398

@dlech dlech closed this as completed Aug 29, 2023
@robgar2001
Copy link
Contributor

robgar2001 commented Aug 29, 2023

@dlech
I don't think this will be fixed by #1398.
The permissions mentioned in the log were already present in buildozer.spec.
The error is most likely caused by the kivy app not asking for the permissions on startup.

This can be quite easily done, I have a fix for this ready, but it still needs testing.
I will create a PR when it's tested.

As mentioned above, the issue can be temporarily fixed by installing the apk using 'adb install -g app.apk'.

Kind regards,
robgar2001

@dlech dlech reopened this Aug 29, 2023
@Jayy001
Copy link
Author

Jayy001 commented Sep 16, 2023

Can confirm that fixed my issue @robgar2001 and I no longer need to manually add the java files. The only final hurdle I had was to install typing_extensions via pip install typing_extensions and then add that to the buildozer.spec file - otherwise the application kept crashing with a Module not found: typing_extensions error.

Sorry this took so long, I had completely forgotten about this issue

@Jayy001
Copy link
Author

Jayy001 commented Sep 16, 2023

@dlech
Screenshot_2023-09-16-22-44-07-92_0d23cfe9e3915bd9660fb1f32f353107
I now get this error when trying to read the characteristic, using this code:

if selected_device.name.startswith("GAN"):
        cube = GanCube(selected_device.address)
        cube.name = selected_device.name
        cube.address = selected_device.address
    
    print(f"Connecting to {cube.name}, {cube.address}")
    async with BleakClient(cube.address) as client:
        
        cube_service = None
        for service in client.services:
            if service.uuid == cube.SERVICE_UUID:
                cube_service = service
                break
        
        if cube_service is None:
            print(f"Unable to find {cube.BRAND} CUBE service or unsppported {cube.BRAND} CUBEs")
            return
        
        read_chrct = None
        write_chrct = None
        for chrct in cube_service.characteristics:
            if chrct.uuid == cube.CHRCT_UUID_READ:
                read_chrct = chrct
            elif chrct.uuid == cube.CHRCT_UUID_WRITE:
                write_chrct = chrct
        
        if read_chrct is None or write_chrct is None:
            print(f"Unable to find {cube.BRAND} CUBE read or write characteristic")
            return
        print(f"Connected to {cube.BRAND} CUBE")

        await client.start_notify(read_chrct, gan_read_handler)

Now this works fine when I run it on my computer (can read the characteristic just fine), but when I run it from my android device it gives that error. Any ideas as to what could be messing it up? Running it with adb install -g app

@dlech
Copy link
Collaborator

dlech commented Sep 21, 2023

Does the characteristic actually support notifications (i.e. run the service_explorer sample or use nRF Connect app to enumerate everything to see what is there)?

@robgar2001
Copy link
Contributor

Hi @Jayy001

Typing-extensions was indeed still missing as a requirement.
It was probably cached somewhere when I was testing it.

I also added the code to request permissions on startup, aldough it seems to be a bit buggy.
This might be something to add on the todo list.

Maybe you can test and have a look at it on your device before I submit a pull request?
So try to install without the -g flag mentioned earlier.

Link to my bleak fork:

https://github.com/robgar2001/bleak

Kind regards,
robgar2001

@Jayy001
Copy link
Author

Jayy001 commented Sep 21, 2023

Does the characteristic actually support notifications (i.e. run the service_explorer sample or use nRF Connect app to enumerate everything to see what is there)?

yes,

Now this works fine when I run it on my computer (can read the characteristics just fine)

It's 100% a problem with the android version, as I have run this exact application before on my computer with no issues. Furthermore, when scanning for characteristics/descriptors it does show 1 descriptor available - it's only when I try to connect to it that the bug occurs.

@Jayy001
Copy link
Author

Jayy001 commented Sep 21, 2023

Hi @Jayy001

Typing-extensions was indeed still missing as a requirement. It was probably cached somewhere when I was testing it.

I also added the code to request permissions on startup, aldough it seems to be a bit buggy. This might be something to add on the todo list.

Maybe you can test and have a look at it on your device before I submit a pull request? So try to install without the -g flag mentioned earlier.

Link to my bleak fork:

https://github.com/robgar2001/bleak

Kind regards, robgar2001

Can do, will let you know

@su-puhu
Copy link

su-puhu commented Dec 28, 2023

@dlech Screenshot_2023-09-16-22-44-07-92_0d23cfe9e3915bd9660fb1f32f353107 I now get this error when trying to read the characteristic, using this code:

if selected_device.name.startswith("GAN"):
        cube = GanCube(selected_device.address)
        cube.name = selected_device.name
        cube.address = selected_device.address
    
    print(f"Connecting to {cube.name}, {cube.address}")
    async with BleakClient(cube.address) as client:
        
        cube_service = None
        for service in client.services:
            if service.uuid == cube.SERVICE_UUID:
                cube_service = service
                break
        
        if cube_service is None:
            print(f"Unable to find {cube.BRAND} CUBE service or unsppported {cube.BRAND} CUBEs")
            return
        
        read_chrct = None
        write_chrct = None
        for chrct in cube_service.characteristics:
            if chrct.uuid == cube.CHRCT_UUID_READ:
                read_chrct = chrct
            elif chrct.uuid == cube.CHRCT_UUID_WRITE:
                write_chrct = chrct
        
        if read_chrct is None or write_chrct is None:
            print(f"Unable to find {cube.BRAND} CUBE read or write characteristic")
            return
        print(f"Connected to {cube.BRAND} CUBE")

        await client.start_notify(read_chrct, gan_read_handler)

Now this works fine when I run it on my computer (can read the characteristic just fine), but when I run it from my android device it gives that error. Any ideas as to what could be messing it up? Running it with adb install -g app

Is the problems solved? I meet the same problem.

@narodnik
Copy link

Greets check my app if you're having trouble:
https://github.com/narodnik/meater_plus_kivy_app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Android more info required Issues does not have a reproducible test case, has insufficent logs or otherwise needs more feedback
Projects
None yet
Development

No branches or pull requests

5 participants