一、python的包的API怎么用
1、依赖包的版本与python的版本要匹配
下载python的国内源:https://mirrors.huaweicloud.com/python/
2、官网:https://docs.python.org/zh-cn/3.9/看自带的文档,搜索 “python 库名”,找到官网(有 github 地址的也算),如果有,看上面的说明,如果没有,参见
3、利用 Python 的帮助系统,安装的第三方库也是可以通过 help
获取的。不过鉴于它没官网 doc,估计其中的 docstring 也不会太详尽,所以不如直接跳到
4、看源码
5、可用网站:
1)https://pybluez.readthedocs.io/en/latest/index.html
2)…
二、例如如何使用包pybluez(蓝牙)
参考:https://blog.csdn.net/weixin_50396804/article/details/109823229
1、如何连接成功
import bluetooth
import sys
import time
name='小米手机10pro' #需连接的设备名字
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print(nearby_devices) #附近所有可连的蓝牙设备
addr=None
for device in nearby_devices: #遍历查看nearby_devices列表中有无需连接的设备小米手机10pro,有就打印出来
if name==device[1]:
addr = device[0]
print("device found!",name," address is: ",addr)
break
if addr==None: #nearby_devices列表中没有需连接的设备小米手机10pro
print("device not exist")
services = bluetooth.find_service(address=addr) #find_service用于查找可用的蓝牙服务。搜索结果将是一个列表
print(services)
for svc in services: #svc依次表示services中的一个元素,遍历完所有元素循环结束
print("Service Name: %s" % svc["name"])
print(" Host: %s" % svc["host"])
print(" Description: %s" % svc["description"])
print(" Provided By: %s" % svc["provider"])
print(" Protocol: %s" % svc["protocol"])
print(" channel/PSM: %s" % svc["port"])
print(" svc classes: %s "% svc["service-classes"])
print(" profiles: %s "% svc["profiles"])
print(" service id: %s "% svc["service-id"]) #打印蓝牙设备的各种属性
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) #代表蓝牙连接的一端的蓝牙套接字。套接字将使用的协议RFCOMM
sock.connect((addr, 2)) #将套接字绑定到本地地址和端口。
print("连接成功,端口:")
i=0
while i<255: #当 while 的 <i<255>为 True 时运行【语句块】,【语句块】运行结束后,再次进入 <条件表达式>进行 判断,如果 <条件表达式>结果为 True 则再次运行【语句块】, 以此循环直到 <条件表达式> 结果为 False 结束循环。
try:
sock.connect((addr, i))
print("连接成功,端口:",i)
break
except Exception as e:
print("端口:",i,"连接失败",e)
i=i+1 #遍历端口号,进行连接
2、连接与进入通讯循环
import time
import sys
import bluetooth
#uuid = "98B97136-36A2-11EA-8467-484D7E99A198"
nearby_devices = bluetooth.discover_devices(lookup_names=True)
print(nearby_devices)
uuid = "00001101-0000-1000-8000-00805f9b34fb"
service_matches = bluetooth.find_service( uuid = uuid ) # match匹配
if len(service_matches) == 0:
print("couldn't find the FooBar service")
sys.exit(0)
first_match = service_matches[0]
print(first_match)
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("connecting to \"%s\" on %s" % (name, host))
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((host, port))
print("连接成功")
while True: #进入循环,不然通讯会自动关闭
sock.send("12345".encode('utf-8'))
sock.send("hello".encode('utf-8'))
data=sock.recv(1024) #1024为数据长度
print("received:",data)
time.sleep(5)
三、蓝牙协议栈bluez移植
bluez-5.64软件包包含了适用于 Linux 的蓝牙协议栈
bluez-libs-3.36.tar.gz (Library 库)
bluez-utils-3.36.tar.gz(utils 常用工具)
看看:https://blog.csdn.net/gatieme/article/details/48751743
四、官方介绍Expat,D-Bus
1、官网主页介绍Expat
这是Expat,一个用于解析XML的C库,由James Clark于1997年创立。Expat 是一个面向流的 XML 解析器。这意味着在开始分析之前,您向解析器注册处理程序。当分析器发现正在分析的文档中的关联结构时,将调用这些处理程序。start 标记是可以为其注册处理程序的结构类型的一个示例。
Expat 支持以下编译器:
GNU GCC >=4.5
LLVM Clang >=3.5
Microsoft Visual Studio >=15.0/2017 (滚动${today} minus 5 years)
2、官网主页介绍 D-Bus
D-Bus 是一种消息总线系统,是应用程序相互通信的一种简单方法。除了进程间通信外,D-Bus还有助于协调进程生命周期;它使编写“单实例”应用程序或守护程序的代码变得简单可靠,并在需要其服务时按需启动应用程序和守护程序。
D-Bus 提供系统守护程序(用于“添加新硬件设备”或“打印机队列已更改”等事件)和每用户登录会话守护程序(用于用户应用程序之间的一般 IPC 需求)。此外,消息总线构建在通用的一对一消息传递框架之上,任何两个应用程序都可以使用该框架直接进行通信(无需通过消息总线守护程序)。目前,通信应用程序位于一台计算机上,或者通过未加密的 TCP/IP,适合在具有共享 NFS 主目录的防火墙后面使用。(需要更好的远程传输的帮助 - 传输机制是抽象的和可扩展的。
dbus低级API参考实现和D-Bus协议已经在现实世界中进行了几年的大量测试,现在已经“一成不变”。将来的更改将兼容或适当地进行版本控制。
低级 libdbus 参考库没有必需的依赖项;引用总线守护程序唯一需要的依赖项是 XML 解析器(expat)。特定于特定框架(Qt,GLib,Java,C#,Python等)的更高级别绑定会添加更多依赖项,但可以做出更多假设,因此使用起来要简单得多。绑定与低级libdbus分开发展,因此有些比其他绑定更成熟,ABI更稳定;查看您计划使用的绑定的文档。
D-Bus协议也有一些针对C#,Java和Ruby等语言的重新实现。它们不使用 libdbus 引用实现。
应该注意的是,低级实现主要不是为应用程序作者设计的。相反,它是绑定作者的基础,也是重新实现的参考。如果能够这样做,建议您使用更高级别的绑定或实现之一。可以在绑定页面上找到这些列表。
使用 D-Bus 的项目列表正在增长,它们提供了大量使用各种 API 进行学习的示例。
D-Bus非常适用于任何Linux或UNIX风格,并且到Windows的移植大部分已经完成。
3、glib-2.8.3中的readme
4、libusb-1.0.26中的readme
libusb是一个用于从Linux、macOS、,
Windows、OpenBSD/NetBSD、俳句和Solaris用户空间。
它是用C编写的(用C++编写的俳句后端),并在GNU下获得许可
Lesser General Public License 2.1版或更高版本(由您选择)
版本(请参见复制)。
libusb的内部抽象方式是
可以移植到其他操作系统。请查看端口
文件以获取更多信息。
libusb主页:
http://libusb.info/
开发人员希望查阅API文档:
http://api.libusb.info
使用邮件列表进行问题、评论等:
http://mailing-list.libusb.info
-汉斯·德戈德hdegoede@redhat.com
-陈晓凡(音译)xiaofanc@gmail.com
-卢多维奇·卢梭。rousseau@gmail.com>
-内森·杰尔姆hjelmn@cs.unm.edu
-克里斯·狄更斯<克里斯托弗。A.dickens@gmail.com>
(请使用邮件列表,而不是直接向开发人员发送邮件)
五、配置编译安装移植
源码:wget http://zlib.net/zlib-1.2.8.tar.gz
配置:./configure --prefix=/wsh_space/my_install/bluez5
修改Makefile('修改一些对配置有影响的环境变量,即环境变量是Makefile变量,要去看懂顶层Makefile才能知道环境变量的具体含义,当然也可以执行./configure -help'):
改前:
CC=gcc
LDSHARED=gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
CPP=gcc -E
AR=ar
RANLIB=ranlib
改后:
CC=arm-cortex_a9-linux-gnueabi-gcc
LDSHARED=arm-cortex_a9-linux-gnueabi-gcc -shared -Wl,-soname,libz.so.1,--version-script,zlib.map
CPP=arm-cortex_a9-linux-gnueabi-gcc -E
AR=arm-cortex_a9-linux-gnueabi-ar
RANLIB=arm-cortex_a9-linux-gnueabi-ranlib
源码:wget ftp://sourceware.org/pub/libffi/libffi-3.0.13.tar.gz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5
我自己配置: ./configure --host=arm-linux-gnueabihf --prefix=/wsh_space/my_install/bluez5
源码:wget http://ftp.gnome.org/pub/gnome/sources/glib/2.40/glib-2.40.0.tar.xz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5 PKG_CONFIG_PATH=/wsh_space/my_install/bluez5/lib/pkgconfig glib_cv_stack_grows=no glib_cv_uscore=yes ac_cv_func_posix_getpwuid_r=yes ac_cv_func_posix_getgrgid_r=yes
我自己配置:./configure --host=arm-linux-gnueabihf --prefix=/wsh_space/my_install/bluez5 PKG_CONFIG_PATH=/wsh_space/my_install/bluez5/lib/pkgconfig glib_cv_stack_grows=no glib_cv_uscore=no ac_cv_func_posix_getpwuid_r=yes ac_cv_func_posix_getgrgid_r=yes
源码:wget http://sourceforge.net/projects/expat/files/expat/2.1.0/expat-2.1.0.tar.gz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5
我自己配置:./configure --host=arm-linux-gnueabihf --prefix=/wsh_space/my_install/bluez5
源码:wget http://dbus.freedesktop.org/releases/dbus/dbus-1.9.4.tar.gz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5 --disable-tests
我自己配置: sudo ./configure --host=arm-linux-gnueabihf --prefix=/wsh_space/my_install/bluez5 --disable-tests(没有管报错configure: error: Explicitly requested expat but expat not found
)
源码:wget http://downloads.sourceforge.net/freeassociation/libical-1.0.tar.gz
配置:export CC=arm-cortex_a9-linux-gnueabi-gc
我自己配置:export CC=/usr/bin/arm-linux-gnueabihf-gcc
export CXX=/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin
cmake -DCMAKE_INSTALL_PREFIX=/wsh_space/my_install/bluez5
源码:wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5 CXX="arm-linux-gnueabi-g++"
源码:wget ftp://ftp.cwru.edu/pub/bash/readline-6.3.tar.gz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5 bash_cv_wcwidth_broken=yes
源码:wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.25.tar.xz
配置:./configure --host=arm-cortex_a9-linux-gnueabi --prefix=/wsh_space/my_install/bluez5 PKG_CONFIG_PATH=/wsh_space/my_install/bluez5/lib/pkgconfig CC="arm-cortex_a9-linux-gnueabi-gcc -L/wsh_space/my_install/bluez5/lib -I/wsh_space/my_install/bluez5/include" --disable-systemd --disable-udev --disable-cups --disable-obex --enable-library
1、configure的参数--host=HOST指定软件运行的系统平台.如果没有指定,将会运行config.guess来检测.系统平台(英语:computing platform)是指在电脑里让软件运行的系统环境,包括硬件环境和软件环境。典型的系统平台包括一台电脑的硬件体系结构(computer architecture)、操作系统、运行时库等。
2、--prefix=PREFIX 把所有文件装在目录PREFIX下面,而不是/usr/local/pgsql里.实际的文件会安装到PREFIX的若干'子目录'里;甚至可能没有一个文件会直接安装到PREFIX目录里.
3、环境变量PKG_CONFIG_LIBDIR是路径覆盖pkg-config的内置的搜索路径
4、gcc交叉编译指令:-L Directory 增加库文件的搜索路径;-I library :连接名为 library 的库文件
5、禁用systemd,udev,cups,obex,library
我自己的配置:
./configure --host=arm-linux --prefix=/opt/arm/bluez PKG_CONFIG_PATH=/opt/arm/bluez/lib/pkgconfig CC="arm-linux-gnueabihf-gcc -L/opt/arm/bluez/lib -I/opt/arm/bluez/include" --disable-systemd --disable-udev --disable-cups --disable-obex --enable-library
六、翻阅源代码中的有关说明文档
doc,readme
有关发布历史和bug修复说明,请参阅文件NEWS。
请参阅文件TO-DO以了解仍然需要做的事情,包括已知的bug。
将任何反馈发送到ncurses邮件列表
bug-ncurses@gnu.org。订阅发送邮件到
bug-ncurses-request@gnu.org, body为:
订阅ncurses < your-email-address-here >
文档目录中的黑客指南包括一些指南
如何以最快的速度报告bug。