如何在python中将类变量用于另一个类

时间:2017-07-27 09:07:12

标签: python-2.7 bluetooth

以下是检测低能量蓝牙设备并将BT地址与数据库中的时间戳一起存储的代码。方法handleDiscovery有一个实例(dev),它存储了我需要使用它来存储地址到数据库的地址,但是无法将它连接到数据库     BTA = ScanDelegate.dev.addr并且在create_table显示缩进错误

from  bluepy.btle import DefaultDelegate,Scanner
import sqlite3
import time
import datetime
conn = sqlite3.connect('bluTry1.db')
c = conn.cursor()


class ScanDelegate(DefaultDelegate):
      def __init__(self):
          DefaultDelegate.__init__(self)

      def handleDiscovery(self,dev,isNewDev,isNewData):
          pass

scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)


def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS beaconstat(BT_address TEXT,time_stamp TEXT)')


def data_entry():
    time = str(datetime.datetime.today().strftime('%H:%M:%S'))
    BTA = ScanDelegate.dev.addr
    c.execute("INSERT INTO beaconstat(BT_address , time )VALUES(?,?)",(BTA,time))
    conn.commit()

create_table()

for i in range(5):
   data_entry()
   time.sleep(1)
c.close()
conn.close()

0 个答案:

没有答案