ble_monitor icon indicating copy to clipboard operation
ble_monitor copied to clipboard

[Bug]: HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.

Open mdczaplicki opened this issue 3 years ago • 8 comments
trafficstars

What happened?

ERROR (Thread-4) [custom_components.ble_monitor] HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.

I'm trying to set up BLE Monitor on my docker HA instance (yes, I'm running it in containers).

Seems that it's not able to communicate with Bluetooth dongle.

Inside container:

bash-5.1# bluetoothctl 
Waiting to connect to bluetoothd...dbus[299]: arguments to dbus_connection_get_object_path_data() were incorrect, assertion "connection != NULL" failed in file dbus-connection.c line 5905.
This is normally a bug in some application using the D-Bus library.

  D-Bus not compiled with backtrace support so unable to print a backtrace
Aborted (core dumped)

or

bash-5.1# hcitool dev
Devices:

On my host machine:

$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Mon 2022-04-11 11:53:38 UTC; 36min ago

Keep in mind that it's running on Alpine. Host is ubuntu server.

Sensor type

No response

Relevant log output

No response

mdczaplicki avatar Apr 11 '22 12:04 mdczaplicki

Please check the FAQ for the installation issues section. There might be some useful tips. It looks like Bluetooth isn't working at all in your docker instance. To make BLE monitor work, you will have to fix the non-working Bluetooth. Also check this issue https://github.com/custom-components/ble_monitor/issues/295 for additional tips

Ernst79 avatar Apr 11 '22 15:04 Ernst79

I'm encountering the same error message. HA stack running in docker on rpi4. HA itself is running in host mode which should give it access to my onboard BT.

Passive BLE seems to install correctly. My onboard bluetooth (hci0) is detected by Passive BLE Monitor:

Capture

However, when I check the logs, in addition to reoccuring 'interface hcl0 not ready' messages, I also saw this

2022-04-19 22:25:15 ERROR (Thread-4) [root] Uncaught thread exception Traceback (most recent call last): File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner self.run() File "/config/custom_components/ble_monitor/__init__.py", line 728, in run self._event_loop.run_until_complete(asyncio.wait_for(initialized_evt[hci].wait(), 5)) File "/usr/local/lib/python3.9/asyncio/base_events.py", line 640, in run_until_complete raise RuntimeError('Event loop stopped before Future completed.') RuntimeError: Event loop stopped before Future completed.

I guess I need to figure out how to "Grant permissions for Python to have rootless access to the HCI interface" in the context of my HA container, as the python that's in use by my HA container is not shared with my local system.

kitcho1 avatar Apr 20 '22 05:04 kitcho1

因为我的母语不是英语,所以我准备用中文和英文同时来描述一下我解决问题的办法

Because i'm not a native english speaker, so i will try to write my solusion with both english and chinese.


首先,我的unraid服务器正在跑奇偶校验,所以暂时我没有办法重启电脑来反复测试解决问题的最小化方法

First of all, my unraid server is running parity job, so for now I cant reboot my computer to test the best may to solves the problem

根据我的经验,在非特权模式下,容器中想要调用外部设备,外部设备必须要由宿主机来驱动

According to my experience, if the docker container want use an external device without privilege mode, the device must be driven by the host

所以我首先确定了我的蓝牙设备已经在unraid系统下完成了驱动

So i find my bluetooth device has been dirven by the host first.

蓝牙设备通常以usb设备的形式出现在linux系统中(无论是你的板载蓝牙,pice设备上的蓝牙或者usb蓝牙),所以无论如何你都需要lsusb来确认,因为之前我那个usb蓝牙实在太烂了,我就把我主力机上的ax200拆下来装到nas上来测试了,下面那个Bus 001 Device 003: ID 8087:0029 Intel Corp.就是被驱动的ax200蓝牙。

After the bluetooth device is driven, it will appear in the usb device,so i run lsusb to find out my device.

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 0463:ffff MGE UPS Systems UPS
Bus 001 Device 003: ID 8087:0029 Intel Corp.  -- this is my bluetooth device, i use my ax200 to test. the elder usb device sucks.
Bus 001 Device 002: ID 0781:5571 SanDisk Corp. Cruzer Fit
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

我在Nerd-Pack插件中安装了bluez(这很重要!因为没有特权模式,容器是没有权限来启动蓝牙设备的)

i installed bluez from nerd-pack(it's very important because the container can't power on the bluetooth device without privilege mode)

然后我在unraid系统下运行了一下hcitool dev发现没有设备被列出,然后我就hciconfig hci0 uphcitool dev,就能看到ax200的mac地址了

Then i run hciconfig hci0 up & hcitool dev, my ax200 was list following.

Devices:
        hci0    64:BC:XX:XX:XX:XX

然后用 homeassistant/home-assistant 运行容器,问题解决了,我的Bluetooth Low Energy Monitor成功发现了全屋的温湿度计。

Then i use homeassistant/home-assistant to create my docker container, then the bluetooth device works successfully in container.

然后容器内的 bluetoothctl 命令还是不能正确工作,甚至 hcitool lescan 也不好用了。

But the bluetoothctl command and hcitool lescan was broken, i dont know how to fix it.

bash-5.1# bluetoothctl
Waiting to connect to bluetoothd...dbus[279]: arguments to dbus_connection_get_object_path_data() were incorrect, assertion "connection != NULL" failed in file dbus-connection.c line 5905.
This is normally a bug in some application using the D-Bus library.

  D-Bus not compiled with backtrace support so unable to print a backtrace
Aborted
bash-5.1# hcitool lescan
Set scan parameters failed: I/O error

VergilGao avatar Apr 29 '22 14:04 VergilGao

1st of thanks for this wonderfull integration

I am also running HA in a docker container, passing thru (probably not the correct wording) a usb dongle by mapping /dev/bus/usb & /dev/run/usb Everything works fine until I restart HA, after that I get the reported message and nothing works.
hcitool dev in the container reports the device fine and a plane scan works but HA doesnt report any of my devices temperature values.

The only thing I found the restores service is to open a shell in the container and run hciconfig hci0 down hciconfig hci0 up

I then need to go into the BLE integration, click configure and then submit (without changing anything) Ticking "Automatically restart Bluetooth adapter on failure" made no difference other than report it was trying to power cycle the adaptor

Hope this might help narrow down the problem

JustANewGuy avatar May 09 '22 09:05 JustANewGuy

I am running BLE Monitor in a docker container on a raspberry pi 3b.

But sometimes this error is reported (The frequency is about 10 times a day). "HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period. "

This error usually lasts for a few minutes. When the error is reported, It's normal to run the command "hcitool" in the container.

What is the reason for this?

ljw092 avatar May 23 '22 04:05 ljw092

I have the same problem in Home Assistant Supervised installed on Raspberry Pi 3B (Ubuntu server). For the first time after I installed BLE Monitor integration everything worked fine. But next day I see that there is no data from my ble device (Xiaomi Mijia Bluetooth Thermometer). In HA system log I see a lot of errors related to BLE Monitor. For example:

2022-06-26 22:29:09 ERROR (Thread-10) [homeassistant] Error doing job: Fatal error: protocol.data_received() call failed. Traceback (most recent call last): File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 867, in _read_ready__data_received self._protocol.data_received(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1855, in data_received self.process(packet) File "/config/custom_components/ble_monitor/init.py", line 662, in process_hci_events sensor_msg, tracker_msg = self.ble_parser.parse_data(data) File "/config/custom_components/ble_monitor/ble_parser/init.py", line 112, in parse_data service_class_uuid16 = (adstruct[2] << 8) | adstruct[3] IndexError: index out of range

2022-06-26 22:37:45 ERROR (Thread-10) [homeassistant] Error doing job: Fatal error: protocol.data_received() call failed. Traceback (most recent call last): File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 867, in _read_ready__data_received self._protocol.data_received(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1843, in data_received extra_data = ev.decode(packet) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1303, in decode data = ev.decode(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1350, in decode data = ev.decode(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 833, in decode data = field.decode(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1379, in decode data = ad.decode(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1649, in decode val.decode(data[: length.val - len(type)]) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 664, in decode data = mynbyte.decode(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 623, in decode self.val = unpack(">%ds" % self.length, data[: self.length])[0][::-1] struct.error: unpack requires a buffer of 4 bytes

2022-06-26 22:37:48 ERROR (Thread-10) [custom_components.ble_monitor] HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period.

2022-06-26 22:45:23 ERROR (Thread-10) [homeassistant] Error doing job: Fatal error: protocol.data_received() call failed. Traceback (most recent call last): File "/usr/local/lib/python3.9/asyncio/selector_events.py", line 867, in _read_ready__data_received self._protocol.data_received(data) File "/usr/local/lib/python3.9/site-packages/aioblescan/aioblescan.py", line 1855, in data_received self.process(packet) File "/config/custom_components/ble_monitor/init.py", line 662, in process_hci_events sensor_msg, tracker_msg = self.ble_parser.parse_data(data) File "/config/custom_components/ble_monitor/ble_parser/init.py", line 112, in parse_data service_class_uuid16 = (adstruct[2] << 8) | adstruct[3] IndexError: index out of range

HCIdump thread: Something wrong - interface hci0 not ready, and will be skipped for current scan period. Logger: custom_components.ble_monitor Source: custom_components/ble_monitor/init.py:730 Integration: Passive BLE monitor (documentation, issues) First occurred: 2022-06-23 19:05:34 (806 occurrences) Last logged: 2022-06-26 22:57:51

Some outputs on my host machine (raspberry pi): image image image

I connected to shell inside homeassistant container. Here is 'bluetoothctl' command output: image

What else can I do to understand the cause of problem?

tms320 avatar Jun 26 '22 20:06 tms320

It seems like your Bluetooth adapter is receiving malformed ble messages. Not sure what the cause is, but I think something is wrong with the Bluetooth adapter. These issues are hard to figure out. If you have a second dongle, I would start to try it with that.

Ernst79 avatar Jun 26 '22 21:06 Ernst79

What else can I do to understand the cause of problem?

https://custom-components.github.io/ble_monitor/Installation

This is usually only needed for alternative installations of Home Assistant that only install Home Assistant core. Also, for alternative installations, please note that BlueZ package must be installed on the system, but its bluetooth daemon service can be stopped and disabled, if you are sure of such a need.

to grant access:

sudo setcap 'cap_net_raw,cap_net_admin+eip' readlink -f \which python3``

to verify:

sudo getcap readlink -f \which python3``

electromost avatar Jul 06 '22 17:07 electromost

I ran into this issue after I updated my RPi3b from Ubuntu 20.04 to 22.04, which runs Home Assistant Core and that brought me python 3.10 as default python3 version. I noticed that running which python3 inside the homeassistant venv points to /srv/homeassistant/bin/python3:

(homeassistant) homeassistant@rpi:~/.homeassistant$ which python3
/srv/homeassistant/bin/python3

with python3 finally pointing to /usr/bin/python3.9

ubuntu@rpi:/srv/homeassistant/bin$ ls -l python*
lrwxrwxrwx 1 homeassistant homeassistant  9 Dec 29  2021 python -> python3.9
lrwxrwxrwx 1 homeassistant homeassistant  9 Dec 29  2021 python3 -> python3.9
lrwxrwxrwx 1 homeassistant homeassistant  7 Jan  5 15:15 python3.10 -> python3
lrwxrwxrwx 1 homeassistant homeassistant 18 Dec 29  2021 python3.9 -> /usr/bin/python3.9

But I ran the the command to give python root-less access to hci0 outside of python venv.

sudo setcap 'cap_net_raw,cap_net_admin+eip' `readlink -f \`which python3\``

where python3.10 was returned by which python3

ubuntu@rpi:~$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root       7 Oct 11  2021 /usr/bin/python -> python3
lrwxrwxrwx 1 root root      10 Aug 18 12:39 /usr/bin/python3 -> python3.10

So I was able to fix the issue with giving explicitly the path to python3.9 (=used by homeassistant) to the setcap command: sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/bin/python3.9 Now the "HCIdump thread: Something wrong ..." error message is gone and the ble_monitor integration started working after a restart of HA.

realzoulou avatar Jan 06 '23 10:01 realzoulou

Thank you very much for the fix @realzoulou ! For some inexplicable reason BLE monitor stopped working after a restart last night and I couldn't narrow it down to anything. I'd never have guessed it would be down to Python and still have no idea why it broke by simply restarting HA. I changed your last command to use Python3.10 instead of 3.9 and it worked like a charm.

Vitz1988 avatar Jan 20 '23 15:01 Vitz1988

Thank you @realzoulou, that worked for me too! I upgraded to Python 3.10 as that is required soon, and I had a few problems. I think this was the last of them. Thanks!

For me these are the exact command I used

sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/bin/python3.10 sudo systemctl restart home-assistant@homeassistant

tomwaldnz avatar Jan 23 '23 00:01 tomwaldnz

I've updated the documentation, most of it was already in the FAQ, but not related to the error message "HCIdump thread: something wrong ...". Added some comments to make it more clear that you need to run the commands inside the docker container or venv.

https://custom-components.github.io/ble_monitor/faq#i-get-a-permissionerror-in-home-assistant-after-the-installation-or-python-upgrade

Ernst79 avatar Jan 23 '23 10:01 Ernst79