deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

macOS panic for multiple calls to Manager::adapters()

pevers opened this issue · comments

First of all, thanks for your work!

Describe the bug
When calling Manager::adapters() twice the program will panic with on macOS.

[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::internal] Authorization status AllowedAlways
[2022-02-27T10:12:03Z DEBUG btleplug::corebluetooth::adapter] Waiting on adapter connect
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::central_delegate::CentralDelegate] delegate_class
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::central_delegate::CentralDelegate] delegate_init
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::central_delegate::CentralDelegate] Storing off ivars!
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::central_delegate::CentralDelegate] delegate_centralmanagerdidupdatestate
[2022-02-27T10:12:03Z DEBUG btleplug::corebluetooth::adapter] Adapter connected
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::internal] Authorization status AllowedAlways
[2022-02-27T10:12:03Z DEBUG btleplug::corebluetooth::adapter] Waiting on adapter connect
[2022-02-27T10:12:03Z TRACE btleplug::corebluetooth::central_delegate::CentralDelegate] delegate_class
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', /redacted/btleplug/src/corebluetooth/central_delegate.rs:249:91

Example code.

let manager = Manager::new().await.unwrap();
manager.adapters().await.unwrap();
manager.adapters().await.unwrap();

Expected behavior
It should not panic when calling this method twice. It might be useful to scan for devices without keeping state, and therefore calling manager.adapters() multiple times from a different context.

Additional context
I think it is caused because the creation of the BtlePlugCentralManagerDelegate is not part of the Once initialization block. I'll push a PR shortly to fix this.