蟒蛇接口

bable-interface的Python项目详细描述


bable-python接口

这是一个跨平台的python接口,用于与依赖于平台的bable桥通信 本机蓝牙在您的项目中能耗低。

用法

importbable_interface# Import the libraryimporttimebable=bable_interface.BaBLEInterface()# Create a BaBLEInterface object to send commandsconnections={}defon_notification_received(success,result,failure_reason):print("Notification received:",result,failure_reason)defon_connected(success,result,failure_reason):print("Connected callback called",result,failure_reason)ifnotsuccess:bable.cancel_connection()else:connections[result['address']]=resultchar=bable_interface.Characteristic(uuid='1234',handle='0x000a',value_handle='0x000b')# Command can be forced to be asynchronous by setting `sync` to False (if command is sync by default)# It is often useful if you want to call a command into a callback function (because they must not be blocking)bable.set_notification(True,result['connection_handle'],char,on_notification_received=on_notification_received,sync=False)defon_unexpected_disconnection(success,result,failure_reason):print("Device has been unexpectedly disconnected",result,failure_reason)defon_device_found(success,result,failure_reason):print("Device found:",result,failure_reason)ifresult['address']=="11:22:33:44:55:66":print("Connecting...")# You can precise timeout duration (meaning the time before an exception is raised if no response has been received)# by setting the `timeout` parameter with the value you want (in seconds)bable.connect(result['address'],result['address_type'],on_connected,on_unexpected_disconnection,timeout=5.0)defon_disconnected(success,result,failure_reason):print("Disconnected callback called",result,failure_reason)defon_error(status,message):print("Error received:",status,message)bable.stop(sync=False)# Start the bable interface (meaning starting the threads and the subprocess needed to make it work)# You can restrict baBLE to only one controller by setting the controller_id (if you want to use multiple controllers)bable.start(on_error=on_error,controller_id=1)# Commands that could take a long time are asynchronous by default: result is sent by calling a callback function.# If an error occures, callback function will be called with `success` parameter as False and failure_reason as the errorbable.start_scan(on_device_found,timeout=1)time.sleep(10)# We can sleep here without blocking the interface (because it is running in another thread)try:print("Disconnecting...")# Command can be forced to be synchronous by setting `sync` to True (if command is async by default)bable.disconnect(connections['11:22:33:44:55:66'],sync=True)# Will block until disconnectedprint("Disconnected")exceptExceptionase:# Synchronous commands raise exception on errorprint("Error while disconnecting:",e)# When we have finished, we have to stop the bable interface to cleanly terminate the threads and the subprocess.bable.stop()print("End")

安装

目前,只有Linux可用(Windows和Mac即将推出…)

来自PYPI

建议从pypi安装它,以便有一个准备就绪的包,其中包含一个预编译的桥。

pip install bable-interface

这项技术适用于x86_64、i686和armv7l体系结构。

源代码

您也可以从源代码构建它,但是您必须自己编译桥 (cfplatforms/查看如何从源代码构建桥)。

$ git clone https://github.com/iotile/baBLE.git
$ cd baBLE/interfaces/python

# Here you can either copy your compiled bridge to the bin folder to add it to your package...
$ cp <path_to_your_bridge_exe> ./bable_interface/bin

# ... or you can add the folder where the bridge executable is to your PATH (and add use_path=True to BaBLEInterface.start())
$ exportPATH=<path_to_your_bridge_exe_folder>:$PATH

$ python setup.py install

“bable”脚本

安装python bable接口后,您将可以从shell访问bable命令。可能有用 两件事:

如何在没有sudo的情况下使用它

默认情况下,在Linux上,您需要具有与Bluetooth子内核通信的根权限。但如果你不想 作为sudo执行程序,您可以简单地设置网桥可执行文件的功能。 最简单的方法是运行:

$ bable --set-cap

这只需运行以下命令,显然您可以自己运行,特别是如果您想使用 不同的婴儿桥:

$ sudo setcap cap_net_raw,cap_net_admin+eip <path_to_your_bridge_exe>

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java原子式更新2个长值   从WSDL生成java代码   java如何将web外部化。XMLServletInitParam?Spring DelegatingFilterProxy用于servlet?   使用JSoup从网页读取元素时发生java Getting 503错误   java如何比较数组列表中存储的两种基本整数类型?   安卓连接到java主机名中的https服务时,其中是否包含“\”?   java访问接口中定义的注释,在实现它的类中?   java如何在Android中将数字放在ListView项之前   Java中12factor的spring引导管理过程   java使用MockMvc使用删除方法rest api从存储库测试deleteAll()   用单个文件编写的java应用程序引发NullPointerException   eclipse java。util。NoTouchElementException错误?   java从mapbox复制了一个缺少变量的教程,但它不会运行   java动态更改方法的返回类型   使用LibGdx在Java中使用opengl奇怪的CPU