简体   繁体   English

缺少 2 个必需的位置 arguments flask python

[英]missing 2 required positional arguments flask python

I'm developing a web application where I pull and use bluetooth data via the browser via the Bleak library.I do not intend to connect to the database.我正在开发一个 web 应用程序,我在其中通过浏览器通过 Bleak 库提取和使用蓝牙数据。我不打算连接到数据库。 My only purpose is to keep the person's bluettoh data on the browser (cookies or sessions) as well.我的唯一目的是将此人的蓝牙数据也保存在浏览器(cookie 或会话)上。 I haven't gotten to this stage yet.我还没有到这个阶段。 At the moment, I just need to view the detailed bluetooth data on the browser.目前,我只需要在浏览器上查看详细的蓝牙数据即可。 But I am getting this error.但是我收到了这个错误。 "TypeError: devices() missing 2 required positional arguments: 'device' and 'advertisement_data'" “TypeError:devices() 缺少 2 个必需的位置 arguments:‘device’ 和 ‘advertisement_data’”

Look at my Codes看看我的代码

from flask import Flask
import asyncio
from bleak import BleakScanner
import asyncio
from uuid import UUID
import json
from construct import Array, Byte, Const, Int8sl, Int16ub, Struct
from construct.core import ConstError
from bleak import BleakScanner
from bleak.backends.device import BLEDevice
from bleak.backends.scanner import AdvertisementData
app = Flask(__name__)
ibeacon_format = Struct(
    "type_length" / Const(b"\x02\x15"),
    "uuid" / Array(16, Byte),
    "major" / Int16ub,
    "minor" / Int16ub,
    "power" / Int8sl,
)
class UUIDEncoder(json.JSONEncoder):
    def default(self, uuid):
        if isinstance(uuid, UUID):
            # if the obj is uuid, we simply return the value of uuid
            return uuid.hex
        return json.JSONEncoder.default(self, uuid)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"


@app.route("/beacons")
async def devices(device: BLEDevice, advertisement_data: AdvertisementData):

    try:
            macadress = device.address
            name = advertisement_data.local_name
            apple_data = advertisement_data.manufacturer_data[0x004C]
            ibeacon = ibeacon_format.parse(apple_data)
            uuid = UUID(bytes=bytes(ibeacon.uuid))
            minor = ibeacon.minor
            major = ibeacon.major
            power = ibeacon.power
            rssi = device.rssi
            rssi = int(rssi)

            beacons = {
                "Mac Adress": macadress,
                "Local Name": name,
                "UUID": uuid,
                "Major": major,
                "Minor": minor,
                "TX Power": power,
                "RSSI": rssi
            }

            if (beacons["Local Name"] == "POI" and beacons["RSSI"] <= -40 and beacons["RSSI"] >= -80):
                return print(beacons)
                # with open("data.json","a") as file:
                #     json.dump(beacons,file,sort_keys=True,indent=4,skipkeys=True,cls=UUIDEncoder,separators=(",",":"))
            else:
                pass


    except KeyError:
        # Apple company ID (0x004c) not found
        pass
    except ConstError:
        # No iBeacon (type 0x02 and length 0x15)
        pass

async def main():
    """Scan for devices."""
    scanner = BleakScanner()
    scanner.register_detection_callback(devices)

    
    while (True):
        await scanner.start()
        await asyncio.sleep(0.5)
        await scanner.stop() 

if __name__ == '__main__':
    app.run(host="127.0.0.0", port=5000, debug=True)

Don't you call the function devices in the line:不要在线路中调用 function 设备:
scanner.register_detection_callback(devices) scanner.register_detection_callback(设备)

If so you have to give it the two arguments device and advertisement_data.如果是这样,你必须给它两个 arguments 设备和广告数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM