HI ALL,
from past two week ,i was jumbling my self to how to do hcitool lescan using python and store it's real time data directly to a file(temporary file),
if you are the one looking for the same ,
then yeah,this post can solve your all question ,
so let me just give you code.if you dont want to understand whats happening in the code ,and worried only about out put,then just go ahead and paste the below code on python
and to understand the code read below,ok
first 9 lines are used call library's,
import os
import signal
import subprocess
import threading
import tempfile
import sys
import time
from tempfile import TemporaryFile
import commands
then go further to create a tempoary file,temporary file is used to create a virtual memory space to store the data,the advantage of this temp file is,that it will vanish once we are out of our script ,i.e you dont have to delete it manually,it will delete itself,
t = TemporaryFile()
now 't' is our tempfile,
going to next line,
print commands.getoutput("hcitool dev")
this line will print the available blue-tooth device on system
out of displayed devices find ,which one reefer’s to BLE and make a note of it ,
from past two week ,i was jumbling my self to how to do hcitool lescan using python and store it's real time data directly to a file(temporary file),
if you are the one looking for the same ,
then yeah,this post can solve your all question ,
so let me just give you code.if you dont want to understand whats happening in the code ,and worried only about out put,then just go ahead and paste the below code on python
#working fine
import os
import signal
import subprocess
import threading
import tempfile
import sys
import time
from tempfile import TemporaryFile
import commands
t = TemporaryFile()
print commands.getoutput("hcitool dev")
commands.getoutput('hciconfig hci0 down')
commands.getoutput('hciconfig hci0 up')
print commands.getoutput("hcitool dev")
commands.getoutput('killall hcitool')
p = subprocess.Popen('hcitool lescan', bufsize = 0,shell = True, stdout =subprocess.PIPE,stderr = subprocess.STDOUT)
time.sleep(10)
#os.kill(p.pid,signal.SIGTERM)
for i in range(0,30,1):
print 'for'
inchar = p.stdout.readline()
i+=1
if inchar:
print 'loop num:',i
print str(inchar)
t.write(str(inchar))
print 'out of loop'
t.seek(0)
print t.read()
and to understand the code read below,ok
first 9 lines are used call library's,
import os
import signal
import subprocess
import threading
import tempfile
import sys
import time
from tempfile import TemporaryFile
import commands
then go further to create a tempoary file,temporary file is used to create a virtual memory space to store the data,the advantage of this temp file is,that it will vanish once we are out of our script ,i.e you dont have to delete it manually,it will delete itself,
t = TemporaryFile()
now 't' is our tempfile,
going to next line,
print commands.getoutput("hcitool dev")
this line will print the available blue-tooth device on system
out of displayed devices find ,which one reefer’s to BLE and make a note of it ,
No comments:
Post a Comment