2

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

4

1 回答 1

1

有两种方法可以走,到目前为止,我只能在 Android 设备上可靠地运行这两种方法。

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

在 raspberry 上,我使用 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"
于 2016-07-11T13:22:35.443 回答