gpt4 book ai didi

linux - BT设备树莓派不配对获取蓝牙信号强度

转载 作者:太空狗 更新时间:2023-10-29 12:30:57 51 4
gpt4 key购买 nike

我想为我现有的家庭自动化系统创建一种室内跟踪系统。我想到了使用 BLE。我已经在我的 Raspberry Pi 上成功设置了 hcitool,我可以毫无问题地连接到我的 iPhone。但是如何在不连接它们的情况下获得我的 Raspberry Pi 和我的 iPhone 之间的信号强度。我已经尝试使用 sudo hcitool cc [BTADDRESS] 未经身份验证连接到我的 iPhone,但看起来 iPhone 不允许这些连接保持打开状态。我认为这一定是一种在不连接两个设备的情况下获得信号强度的方法。我想用它来确定从我的 Raspberry Pi 到我的 iPhone 的距离。我可以从我需要找到我的 iPhone 的时间开始计算距离吗?

最佳答案

有两种方法,现在我只能在 Android 设备上可靠地工作。

  1. 利用智能手机的蓝牙友好名称并设置 discoverability to infinite .我已经写了一个简单的应用程序。在后台工作,也在应用程序被杀死之后,因为可发现性设置被保留。据我所知,这在 iOS 中是不可能的。
  2. 在手机的 BLE 数据包中通告 UUID。这可以通过 Android 和 iOS 设备完成。然而,在后台时,iPhone 将广告切换为缩小模式,使数据包无法识别。后台识别广告iOS设备问题依旧open .

在树莓派上,我使用 PyBluez 扫描并寻找运行 (1) 或 (2) 的智能手机。我报告一个代码示例:

import bluetooth
import bluetooth._bluetooth as bluez
import struct, socket, sys, select
def hci_enable_le_scan(sock):
hci_toggle_le_scan(sock, 0x01)

#Discover name and RSS of enabled BLE devices
class MyDiscoverer(bluetooth.DeviceDiscoverer):

def pre_inquiry(self):
self.done = False

def device_discovered(self, address, device_class, rssi, name):
discovery_logger.info("Discovered %s" % (address, ))
if name == "YOUR-DEVICE-FRIENDLY_NAME":
#Use the RSS for your detection / localization system

def inquiry_complete(self):
self.done = True

#Performs inquiry for name request
def async_inquiry():
d = MyDiscoverer()
while True:
d.find_devices(lookup_names = True)
readfiles = [ d, ]
while True:
rfds = select.select( readfiles, [], [] )[0]
if d in rfds:
d.process_event()
if d.done:
break
time.sleep(DISCOVERY_INTERVAL)

#Parse received advertising packets
def parse_events(sock):
# save current filter
old_filter = sock.getsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, 14)

flt = bluez.hci_filter_new()
bluez.hci_filter_all_events(flt)
bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT)
sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, flt )
while True:
pkt = sock.recv(255)
ptype, event, plen = struct.unpack("BBB", pkt[:3])
if event == LE_META_EVENT:
subevent, = struct.unpack("B", pkt[3])
pkt = pkt[4:]
if subevent == EVT_LE_CONN_COMPLETE:
le_handle_connection_complete(pkt)
elif subevent == EVT_LE_ADVERTISING_REPORT:
#Check if the advertisement is the one we are searching for
if getASCII(pkt[start:end]) == "YOUR-UUID"
report_pkt_offset = 0
report_data_length, = struct.unpack("B", pkt[report_pkt_offset + 9])
# each report is 2 (event type, bdaddr type) + 6 (the address)
# + 1 (data length field) + report_data length + 1 (rssi)
report_pkt_offset = report_pkt_offset + 10 + report_data_length + 1
rssi, = struct.unpack("b", pkt[report_pkt_offset -1])
#Now you have the RSS indicator, use it for monitoring / localization

sock.setsockopt( bluez.SOL_HCI, bluez.HCI_FILTER, old_filter )
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
except:
print "error accessing bluetooth device..."
sys.exit(1)

p = threading.Thread(group=None, target=parse_events, name='parsing', args=(sock, ))
d = threading.Thread(group=None, target=async_inquiry, name='async_inquiry', args=())
try:
p.start()
except:
print "Error: unable to start parsing thread"

try:
d.start()
except:
print "Error: unable to start asynchronous discovery thread"

关于linux - BT设备树莓派不配对获取蓝牙信号强度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27364581/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com