Hcitool lescan, werden nicht gedruckt und in Echtzeit an einer Datei

UPDATE: ich löste meine Lösung mit 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"

Ich habe zwei Programme, die im wesentlichen die gleichen, aber mit zwei verschiedenen Befehlen, die auf einem Raspberry PI läuft Raspbian.

Was ich versuche zu tun, ist schreiben Sie Befehl-outputs in eine Datei, damit ich diese verarbeiten kann später auf.

Ich bin ratlos, warum das erste Programm wird nicht funktionieren, aber der zweite wird.

Den Ersten Programm hat eine "sudo timeout 5 hcitool lescan" - Befehl, der funktioniert nicht.

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()

Hier ist die Zweiten Programm habe ich die funktioniert feiner Druck der "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()
Schreibe einen Kommentar