在我不得不重置适配器后,蓝牙的 Python 代码抛出错误 [英] Python code for Bluetooth throws error after I had to reset the adapter
问题描述
我正在尝试用 Python 进行蓝牙编程.直到昨天它都运行良好.今天早上,停电了,由于某种原因,蓝牙模块被禁用,无法打开.所以,我做了一个 sudo hciconfig hci0 reset
然后打开它.从那时起,最简单的程序都无法执行.以 这个 one/a> 例如.它卡在 bluetooth
模块中的 advertise_service
并抛出以下错误(仅供参考:virtualenv 在这里不是问题.系统范围的 python 也做同样的事情).
I was trying out bluetooth programming in python. It was working fine till yesterday. This morning, there was a power outage and for some reason, the bluetooth module got disabled and it could not be turned on. So, I did a sudo hciconfig hci0 reset
and then turned it on. From that point onwards, the simplest of the programs are failing to execute. Take this one for example. It gets stuck at advertise_service
in bluetooth
module and throws the following error (FYI: virtualenv was not a problem here. The systemwide python also does the same thing).
Traceback (most recent call last):
File "bt.py", line 17, in <module>
advertise_service( server_sock, "SampleServer", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ])
File "/home/machinename/.virtualenvs/py27/local/lib/python2.7/site-packages/bluetooth/bluez.py", line 242, in advertise_service
raise BluetoothError (str (e))
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
有时我在编译并重新安装 Bluez
驱动程序时遇到不同的错误:
Sometimes I got a different error when I compiled and reinstalled Bluez
driver:
Traceback (most recent call last):
File "build/bdist.linux-x86_64/egg/bluetooth/bluez.py", line 268, in advertise_service
bluetooth.btcommon.BluetoothError: error no advertisable device.
但所有这些都在那台机器上发挥了作用;事实上,在我写这篇文章的时候,所有的程序在我的其他 ubuntu (14.04LTS) 机器上都运行得很好.我检查了源代码,并追踪到一个 _bluetooth.so
文件 - 这是一个编译代码,因此我无法弄清楚该怎么做.
But all of these worked like a charm before in that machine; in fact all of the program works just fine with my other ubuntu (14.04LTS) machine as I write this. I inspected the source code, and traced to a _bluetooth.so
file - which is a compiled code, hence I couldn't figure out what to do anymore.
任何指针都将受到高度赞赏.
Any pointer will be highly appreciated.
推荐答案
此错误是由于 BlueZ 5 和 SDP 与 bluetoothd
确保运行 sdptool browse local
出现以下错误:
Make sure, running sdptool browse local
gives following error:
Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory
事实证明,罪魁祸首是蓝牙守护进程bluetoothd
.由于某些愚蠢的原因,将 SDP 与 bluetoothd
一起使用需要弃用的功能,因此要解决此问题,必须使用 bluetoothd -C
(或 bluetooth --compat
).
As it turns out, the culprit is bluetoothd
, the Bluetooth daemon. Using SDP with bluetoothd
requires deprecated features for some silly reason, so to fix this, the daemon must be started in compatibility mode with bluetoothd -C
(or bluetooth --compat
).
通过以下方式查找 bluetooth.service
的位置:
Find location of bluetooth.service
by:
systemctl status bluetooth.service
然后编辑bluetooth.service
并寻找
ExecStart=/usr/libexec/bluetooth/bluetoothd
在本行末尾追加--compat
,保存,然后运行
Append --compat
at the end of this line, save, and then run
service bluetooth start
如果一切顺利,你应该可以成功运行
If all goes well, you should be able to successfully run
sudo sdptool browse local
最后,重置适配器:
sudo hciconfig -a hci0 reset
现在一切正常
只是为了让人们知道,我相信最新的 BlueZ
构建不知何故在我的系统中坏了.我下载、编译并安装了 5.35 版本,然后没有任何工作.我拨到 5.34,还是一样.我还注意到蓝牙适配器在启用后 3-4 分钟自动关闭,
Just to let people know, I believe the latest BlueZ
build was somehow broken in my system. I downloaded, compiled and installed the 5.35 version, and nothing was working. I dialed down to 5.34, still same. I also noticed that the bluetooth adapter was going down automatically 3-4 minutes after enabling it using,
sudo hciconfig hci0 up # hci0 is the bt adapter
我使用了一个 USB 蓝牙加密狗进行测试.它没有像内置适配器那样自动停机,但问题仍然存在.然后我用apt-get
重新安装bluez
,
I used one usb bluetooth dongle to test. It did not go down automatically like the inbuilt adapter, but the problems persisted. Then I used apt-get
to reinstall bluez
,
apt-get install --reinstall bluez
突然之间一切都恢复了正常.
and all of a sudden everything came back to normal.
这篇关于在我不得不重置适配器后,蓝牙的 Python 代码抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!