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

macOS client.start_notify fails after reconnect #1645

Open
dakhnod opened this issue Sep 28, 2024 · 5 comments
Open

macOS client.start_notify fails after reconnect #1645

dakhnod opened this issue Sep 28, 2024 · 5 comments
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working

Comments

@dakhnod
Copy link

dakhnod commented Sep 28, 2024

  • bleak version: 0.22.2
  • Python version: 3.9
  • Operating System: macOS sonoma 14.4.1
  • BlueZ version (bluetoothctl -v) in case of Linux:

Description

When running client.start_notify on a device that has previously been connected to and that disconnected and connected again, it throws the exception ValueError: Characteristic notifications already started.

Not calling the function a second time causes no notifications to arrive.

@dlech
Copy link
Collaborator

dlech commented Sep 29, 2024

Can you give an example program and the exact steps to reproduce the problem?

@dlech dlech added Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working labels Sep 29, 2024
@dakhnod
Copy link
Author

dakhnod commented Oct 2, 2024

Yep, sorry. Attached is a minimal demo program.
Somehow, bleak or some other component seems to remember a notification subscription, even if the device disconnected...

import bleak
import asyncio

# replace this with anything suiting on your device
notify_uuid = '00002a56-0000-1000-8000-00805f9b34fb'

async def main():
    scanner = bleak.BleakScanner()

    disconnect_event = asyncio.Event()
    client = bleak.BleakClient(
        await scanner.find_device_by_name('BLEnky demo'),
        lambda client: disconnect_event.set()
    )
    await client.connect()
    await client.start_notify(notify_uuid, print)

    print('connected, please restart device')

    await disconnect_event.wait()

    print('reconnecting...')

    await client.connect()
    # this will throw on macOS, not on linux
    await client.start_notify(notify_uuid, print)

    print('reconnected.')

asyncio.run(main())

Steps to reproduce:

  1. Put in the name of a test device you can connect to and restart
  2. Put in the UUID of some notification that can be subscribed to
  3. Run script
  4. When prompted, restart device
  5. Watch exception get thrown, but only on mac

Besides, great work on this! This library is a pleasure to use, I wish there was something comperable for js! Thank you!

@MAKOMO
Copy link

MAKOMO commented Oct 6, 2024

I experience this too for v0.22.3 on macOS 15.0.1

@dlech
Copy link
Collaborator

dlech commented Oct 6, 2024

Calling connect() a second time on the same BleakClient object is something that has never worked well in Bleak. We recommend to create a new BleakClient object in order to connect again.

@MAKOMO
Copy link

MAKOMO commented Oct 7, 2024

Thanks David for this hint! Not reusing that client resolves the reconnect issues for me. Keep going!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: Core Bluetooth Issues and PRs relating to the Core Bluetooth backend bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants