简体   繁体   English

Hcitool lescan不会实时打印到文件

[英]Hcitool lescan will not print in real time to a file

UPDATE: I solved my solution using os.system: 更新:我使用os.system解决了我的解决方案:

sensortag=0
while sensortag != "B4:99:4C:64:33:E0":
    #call the command and write to scan.txt file and then fill the process.
    #loop to find if the MAC address given is available
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
    scan = open("scan.txt","r")
    readscan = scan.read()
    if "B4:99:4C:64:33:E0" in readscan:
        print "SensorTag found."
        sensortag = "B4:99:4C:64:33:E0"

I have two programs, essentially the same, but with two different commands, on a Raspberry PI, running Raspbian. 我有两个程序,基本上是相同的,但在Raspberry PI上有两个不同的命令,运行Raspbian。

What I am trying to do is write both command outputs to a file, so I can process them later on. 我想要做的是将两个命令输出写入文件,以便稍后我可以处理它们。

I am puzzled to why the first program won't work, yet the second one will. 我很困惑为什么第一个程序不起作用,但第二个程序将会起作用。

The First program has a " sudo timeout 5 hcitool lescan " command, which does not work . 一个程序有一个“ sudo timeout 5 hcitool lescan ”命令,该命令不起作用

import os
import subprocess

#r+ because the file is already there, w without the file
myfile = open("scan.txt", "r+")

#Reset Bluetooth interface, hci0
os.system("sudo hciconfig hci0 down")
os.system("sudo hciconfig hci0 up")

#Scan for bluetooth devices
dev = subprocess.Popen(["sudo timeout 5 hcitool lescan"], stdout=subprocess.PIPE, shell=True)
(device, err) = dev.communicate()

#Print bluetooth devices
print device

#Write the hcitool lescan output to a file
myfile.write(device)

#Close the file
myfile.close()

Here is the Second program I have which works fine printing the " sudo hciconfig ": 这是我的第二个程序, 可以很好地打印“ sudo hciconfig ”:

import os
import subprocess

#r+ because the file is already there, w without the file
myfile = open("test.txt", "r+")

#Reset Bluetooth interface, hci0
os.system("sudo hciconfig hci0 down")
os.system("sudo hciconfig hci0 up")

#Make sure device is up
interface = subprocess.Popen(["sudo hciconfig"], stdout=subprocess.PIPE, shell=True)
(int, err) = interface.communicate()

#Print hciconfig to make sure it's up
print int

#Write the hciconfig output to a file
myfile.write(int)

#Close the file
myfile.close()

I solved my solution using os.system and killing the scan straight away: 我使用os.system解决了我的解决方案并立即终止扫描:

sensortag=0
while sensortag != "B4:99:4C:64:33:E0":
    #call the command and write to scan.txt file and then fill the process.
    #loop to find if the MAC address given is available
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
    scan = open("scan.txt","r")
    readscan = scan.read()
    if "B4:99:4C:64:33:E0" in readscan:
        print "SensorTag found."
        sensortag = "B4:99:4C:64:33:E0"

After spending a few hours working this out, I came up with this solution. 花了几个小时来完成这个工作之后,我想出了这个解决方案。 The problem with hcitool lescan is that it doesn't return until it receives a SIGINT, so we send it one with Python: hcitool lescan的问题是它在收到SIGINT之前不会返回,所以我们用Python发送它:

    bashCommand = "hcitool lescan"
    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
    time.sleep(3)
    os.kill(process.pid, signal.SIGINT)
    output = process.communicate()[0]

This for me returned a string containing all of the mac addresses found, after a 3 second search. 这对我来说,经过3秒的搜索,返回了一个包含找到的所有mac地址的字符串。

Using terminal only, following command did work: 仅使用终端,以下命令确实起作用:

hcitool lescan > scan.txt & sleep 2 && pkill --signal SIGINT hcito

I will just leave it here, maybe this will help someone. 我会把它放在这里,也许这会对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM