hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.71k stars 286 forks source link

Android example permissions broken #1363

Open Jayy001 opened 1 year ago

Jayy001 commented 1 year ago

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 commented 1 year ago

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

robgar2001 commented 1 year ago

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 commented 1 year ago

Should be fixed by #1398

robgar2001 commented 1 year ago

@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

Jayy001 commented 11 months ago

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 commented 11 months ago

@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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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 commented 11 months ago

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 commented 8 months ago

@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 commented 4 months ago

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