以下是检测低能量蓝牙设备并将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()