1

when this files run gives corresponding output values as;

# test BLE Scanning software
# jcs 6/8/2014


import MySQLdb as my
import blescan
import sys


import bluetooth._bluetooth as bluez

dev_id = 0

db = my.connect(host="localhost",
user="root",
passwd="root",
db="test"
)

cursor = db.cursor()

try:
    sock = bluez.hci_open_dev(dev_id)
    print "ble thread started"

except:
    print "error accessing bluetooth device..."
        sys.exit(1)

blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)




while True:
    returnedList = blescan.parse_events(sock, 10)
    print "----------"
    for beacon in returnedList:

        print beacon


sql = "insert into beacon VALUES(null, '%s')" % \
(beacon)

number_of_rows = cursor.execute(sql)
db.commit()

db.close()

I want output stored in a text file

cf:68:cc:c7:33:10,b9407f30f5f8466eaff925556b57fe6d,13072,52423,-74,-78
cf:68:cc:c7:33:10,74696d6f74650e160a181033c7cc68cf,46608,13255,-52,-77
da:f4:2e:a0:70:b1,b9407f30f5f8466eaff925556b57fe6d,28849,11936,-74,-79
da:f4:2e:a0:70:b1,74696d6f74650e160a18b170a02ef4da,46769,28832,46,-78
dd:5d:d3:35:09:dd,8aefb0316c32486f825be26fa193487d,1,1,-64,-78
c3:11:48:9b:cf:fa,8aefb0316c32486f825be26fa193487d,0,0,-64,-73
fd:5b:12:7f:02:e4,b9407f30f5f8466eaff925556b57fe6d,740,4735,-74,-79
fd:5b:12:7f:02:e4,74696d6f74650e160a18e4027f125bfd,46820,639,18,-80
dd:5d:d3:35:09:dd,8aefb0316c32486f825be26fa193487d,1,1,-64,-77

so on... and then write store these values in text file. For text file, is it possible to generate text file as a part of script? Thanks

shane
  • 57
  • 3
  • 9
  • [Yes!](https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files) Is your beacon a simple list? – cwahls Nov 09 '16 at 06:32
  • yes and i want to save the iBeacon MAC Address iBeacon UDID iBeacon Major Number iBeacon Minor Number TX Power at 1m results to a mysql database – shane Nov 09 '16 at 06:35

2 Answers2

0

Try this for your loop

while True:
    returnedList = blescan.parse_events(sock, 10)
    print "----------"
    f = open('bluez.txt', 'a+')
    for beacon in returnedList:
        print beacon
        f.write(','.join(beacon)+'\n')
    f.close()

EDIT: now appending mode

cwahls
  • 743
  • 7
  • 22
0

if what you want is to store the data in file, so you can open it later the simplest way is to use the Pickle (or the cPickle) module something like:

import cPickle

#store the data in file

with open('/path/to/file.txt', 'wb') as f:
    cPickle.dump(returnedList, f)

#to read the data

loaded_returnedList = cPickle.load(open('/path/to/file.txt'))

 print loaded_returnedList == returnedList # Prints True

now if what you want is store the data for visual store (maybe to open it in excel later), the csv module is for you

import csv

with open('/path/to/file', 'w') as f:
    writer = csv.writer(f)

    for beacon in returnedList:
        writer.writerow(beacon)
aleivag
  • 2,306
  • 1
  • 13
  • 13