在python脚本中ble in termux

发布于 2025-02-04 08:38:56 字数 1446 浏览 3 评论 0原文

我正在写我的学士学位论文,并且我已经成功地在Python编辑了一个脚本,该脚本使用BLE从Arduino Nano 33 IoT收集数据。我现在希望在我的Android手机上在Termux上运行此Python脚本:这是麻烦的!我的教授警告我,可能会出现一些试图在Android上使用BLE的问题。

使用BLE的惨淡库中的一个示例如下:

    import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

asyncio.run(main())

但是我在Termux上遇到了此错误:

python scanBLE.py
Traceback (most recent call last):
  File "/storage/emulated/0/Download/python/scanBLE.py", line 2, in <module>
    from bleak import BleakScanner
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/bleak/init.py", line 41, in <module>
    if not _on_ci and not check_bluez_version(5, 43):
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/bleak/backends/bluezdbus/init.py", line 17, in check_bluez_version
    p = subprocess.Popen(["bluetoothctl", "--version"], stdout=subprocess.PIPE)
  File "/data/data/com.termux/files/usr/lib/python3.10/subprocess.py", line 966, in init
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/data/data/com.termux/files/usr/lib/python3.10/subprocess.py", line 1840, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'bluetoothctl'

请不要告诉我我必须扎根手机!所以我的问题是:是否有一种方法可以从termux中运行的python代码访问ble?

I'm writing my bachelor thesis and I've successfully compiled a script in python which uses BLE to gather data from an Arduino nano 33 IoT. I now wish to run this python script on termux on my android phone: here comes the trouble! My professor warned me about some problems that might occur trying to use BLE on Android.

An example from the Bleak library using BLE is the following:

    import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

asyncio.run(main())

but I get this error on termux:

python scanBLE.py
Traceback (most recent call last):
  File "/storage/emulated/0/Download/python/scanBLE.py", line 2, in <module>
    from bleak import BleakScanner
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/bleak/init.py", line 41, in <module>
    if not _on_ci and not check_bluez_version(5, 43):
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/bleak/backends/bluezdbus/init.py", line 17, in check_bluez_version
    p = subprocess.Popen(["bluetoothctl", "--version"], stdout=subprocess.PIPE)
  File "/data/data/com.termux/files/usr/lib/python3.10/subprocess.py", line 966, in init
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/data/data/com.termux/files/usr/lib/python3.10/subprocess.py", line 1840, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'bluetoothctl'

Please don't tell me that I have to root my phone! So my question is: Is there a way to give access to the BLE from a python code running in termux ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文