用python寻找低功耗蓝牙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23788176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finding Bluetooth low energy with python
提问by user3582887
Is it possible for this code to be modified to include Bluetooth Low Energy devices as well? https://code.google.com/p/pybluez/source/browse/trunk/examples/advanced/inquiry-with-rssi.py?r=1
是否可以修改此代码以包括低功耗蓝牙设备? https://code.google.com/p/pybluez/source/browse/trunk/examples/advanced/inquiry-with-rssi.py?r=1
I can find devices like my phone and other bluetooth 4.0 devices, but not any BLE. If this cannot be modified, is it possible to run the hcitool lescan and pull the data from hci dump within python? I can use the tools to see the devices I am looking for and it gives an RSSI in hcidump, which is what my end goal is. To get a MAC address and RSSI from the BLE device.
我可以找到手机和其他蓝牙 4.0 设备等设备,但找不到任何 BLE。如果无法修改,是否可以运行 hcitool lescan 并从 python 中的 hci 转储中提取数据?我可以使用这些工具查看我正在寻找的设备,它会在 hcidump 中提供 RSSI,这就是我的最终目标。从 BLE 设备获取 MAC 地址和 RSSI。
Thanks!
谢谢!
采纳答案by Tim Tisdall
As I said in the comment, that library won't work with BLE.
正如我在评论中所说,该库不适用于 BLE。
Here's some example code to do a simple BLE scan:
下面是一些执行简单 BLE 扫描的示例代码:
import sys
import os
import struct
from ctypes import (CDLL, get_errno)
from ctypes.util import find_library
from socket import (
socket,
AF_BLUETOOTH,
SOCK_RAW,
BTPROTO_HCI,
SOL_HCI,
HCI_FILTER,
)
if not os.geteuid() == 0:
sys.exit("script only works as root")
btlib = find_library("bluetooth")
if not btlib:
raise Exception(
"Can't find required bluetooth libraries"
" (need to install bluez)"
)
bluez = CDLL(btlib, use_errno=True)
dev_id = bluez.hci_get_route(None)
sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI)
sock.bind((dev_id,))
err = bluez.hci_le_set_scan_parameters(sock.fileno(), 0, 0x10, 0x10, 0, 0, 1000);
if err < 0:
raise Exception("Set scan parameters failed")
# occurs when scanning is still enabled from previous call
# allows LE advertising events
hci_filter = struct.pack(
"<IQH",
0x00000010,
0x4000000000000000,
0
)
sock.setsockopt(SOL_HCI, HCI_FILTER, hci_filter)
err = bluez.hci_le_set_scan_enable(
sock.fileno(),
1, # 1 - turn on; 0 - turn off
0, # 0-filtering disabled, 1-filter out duplicates
1000 # timeout
)
if err < 0:
errnum = get_errno()
raise Exception("{} {}".format(
errno.errorcode[errnum],
os.strerror(errnum)
))
while True:
data = sock.recv(1024)
# print bluetooth address from LE Advert. packet
print(':'.join("{0:02x}".format(x) for x in data[12:6:-1]))
I had to piece all of that together by looking at the hcitool
and gatttool
source code that comes with Bluez. The code is completely dependent on libbluetooth-dev
so you'll have to make sure you have that installed first.
我不得不通过查看拼凑所有的一起hcitool
,并gatttool
附带配合bluez源代码。该代码完全依赖,libbluetooth-dev
因此您必须先确保已安装该代码。
A better way would be to use dbus to make calls to bluetoothd
, but I haven't had a chance to research that yet. Also, the dbus interface is limited in what you can do with a BLE connection after you make one.
更好的方法是使用 dbus 调用bluetoothd
,但我还没有机会研究它。此外,dbus 接口在您建立一个 BLE 连接后可以执行的操作受到限制。
EDIT:
编辑:
Martin Tram?ak pointed out that in Python 2 you need to change the last line to print(':'.join("{0:02x}".format(ord(x)) for x in data[12:6:-1]))
Martin Tram?ak 指出在 Python 2 中你需要将最后一行改为 print(':'.join("{0:02x}".format(ord(x)) for x in data[12:6:-1]))
回答by oscarah
You could also try pygattlib. It can be used to discover devices, and (currently) there is a basic support for reading/writing characteristics. No RSSI for now.
你也可以试试pygattlib。它可用于发现设备,并且(目前)有对读/写特性的基本支持。暂时没有RSSI。
You could discover using the following snippet:
您可以使用以下代码段发现:
from gattlib import DiscoveryService
service = DiscoveryService("hci0")
devices = service.discover(2)
DiscoveryService
accepts the name of the device, and the method discover
accepts a timeout (in seconds) for waiting responses. devices
is a dictionary, with BL address as keys, and names as values.
DiscoveryService
接受设备的名称,并且该方法discover
接受等待响应的超时(以秒为单位)。devices
是一个字典,以BL地址为键,名称为值。
pygattlibis packaged for Debian (or Ubuntu), and in the Downloads section there is a .deb.
pygattlib是为 Debian(或 Ubuntu)打包的,在下载部分有一个 .deb。