蟒蛇接口
bable-interface的Python项目详细描述
bable-python接口
这是一个跨平台的python接口,用于与依赖于平台的bable桥通信 本机蓝牙在您的项目中能耗低。
- Usage
- Installation
- "bable" script
- Installing Support for IOTile Based Devices
- How to use it without sudo
用法
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
命令。可能有用
两件事:
- 安装当前的bable版本(
bable --version
) - 设置网桥可执行文件的功能(cf "How to use it without sudo" part)
(
bable --set-cap
)
如何在没有sudo的情况下使用它
默认情况下,在Linux上,您需要具有与Bluetooth子内核通信的根权限。但如果你不想 作为sudo执行程序,您可以简单地设置网桥可执行文件的功能。 最简单的方法是运行:
$ bable --set-cap
这只需运行以下命令,显然您可以自己运行,特别是如果您想使用 不同的婴儿桥:
$ sudo setcap cap_net_raw,cap_net_admin+eip <path_to_your_bridge_exe>