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

BleakScannerWinRT import fails in Jupyter Notebook - [WinError -2147417850] Cannot change thread mode after it is set #698

Closed
datoslabs opened this issue Dec 9, 2021 · 2 comments · Fixed by #709
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend

Comments

@datoslabs
Copy link

  • bleak version: 0.13.0
  • Python version: 3.8.8
  • Operating System: Windows 10
  • BlueZ version (bluetoothctl -v) in case of Linux: N/A

Description

Hi,

New to bleak. I encountered [WinError -2147417850] Cannot change thread mode after it is set when I attempted to import the scanner with the following:

from bleak.backends.winrt.scanner import (
        BleakScannerWinRT as BleakScanner,
    ) 

After some googling, it appears to be a known issue trying to initialize winrt with MTA in Jupyter Notebook. I was able to avoid the error by:

  1. Install winrt
  2. Execute the following twice (the first execution resulted in the same error but the second time will run without errors) :
import importlib
_winrt=importlib.import_module("winrt._winrt")
_winrt.uninit_apartment()
import winrt
  1. Afterwards, running the following to import BleakScannerWinRT works without any error:
from bleak.backends.winrt.scanner import (
        BleakScannerWinRT as BleakScanner,
    ) 

Is this the right approach? I seemed to be able to scan and discover my BLE device; I am still getting familiar with this library to read the characteristics so I am not sure if I would run into any problems down the road.

@dlech dlech added the Backend: WinRT Issues or PRs relating to the WinRT backend label Dec 13, 2021
@dlech
Copy link
Collaborator

dlech commented Dec 13, 2021

Hmm... now that we have our own bleak-winrt package, we should probably set the default to be STA instead of MTA to avoid the need for such workarounds.

@hbldh
Copy link
Owner

hbldh commented Dec 13, 2021

I ran into this trying to debug from VS Code. It is a shame if such a basic and not at all uncommon operation produces an error. I am way too uninitiated in why it is needed to say what the default should be, but in general I think Bleak should aim for making things as non-problematic for the non-experts as possible.

dlech added a commit that referenced this issue Dec 31, 2021
This removes the explicit apartment initialization that breaks apps
that have already initialized a single threaded apartment.

Fixes: #698
dlech added a commit that referenced this issue Jan 8, 2022
Added
-----

* Added ``service_uuids`` kwarg to  ``BleakScanner``. This can be used to work around issue of scanning not working on macOS 12. Fixes #230. Works around #635.
* Added UUIDs for LEGO Powered Up Smart Hubs.

Changed
-------

* Changed WinRT backend to use GATT session status instead of actual device connection status.
* Changed handling of scan response data on WinRT backend. Advertising data and scan response data is now combined in callbacks like other platforms.
* Updated ``bleak-winrt`` dependency to v1.1.0. Fixes #698.

Fixed
-----

* Fixed ``InvalidStateError`` in CoreBluetooth backend when read and notification of the same characteristic are used. Fixes #675.
* Fixed reading a characteristic on CoreBluetooth backend also triggers notification callback.
* Fixed in Linux, scanner callback not setting metadata parameters. Merged #715.
@dlech dlech mentioned this issue Jan 8, 2022
dlech added a commit that referenced this issue Jan 10, 2022
Added
-----

* Added ``service_uuids`` kwarg to  ``BleakScanner``. This can be used to work around issue of scanning not working on macOS 12. Fixes #230. Works around #635.
* Added UUIDs for LEGO Powered Up Smart Hubs.

Changed
-------

* Changed WinRT backend to use GATT session status instead of actual device connection status.
* Changed handling of scan response data on WinRT backend. Advertising data and scan response data is now combined in callbacks like other platforms.
* Updated ``bleak-winrt`` dependency to v1.1.0. Fixes #698.

Fixed
-----

* Fixed ``InvalidStateError`` in CoreBluetooth backend when read and notification of the same characteristic are used. Fixes #675.
* Fixed reading a characteristic on CoreBluetooth backend also triggers notification callback.
* Fixed in Linux, scanner callback not setting metadata parameters. Merged #715.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend: WinRT Issues or PRs relating to the WinRT backend
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants