I'm working on a project where I get device data from ibeacon bluetooth devices. Normally I can have all Mac addresses and device local_names. But when I want to change the code a little bit I can't get the Mac addresses
Executing Code
import asyncio
from bleak import BleakScanner
async def main(): print("scanning for 5 seconds, please wait...")
devices = await BleakScanner.discover(return_adv=True)
for d, a in devices.values():
print()
print(d) # beacon names are POI
print("-" * len(str(d)))
print(a)
if name == "main": asyncio.run(main())
FF:CC:DD:KK:8A:01: POI
----------------------
AdvertisementData(local_name='POI', manufacturer_data={76: b'\x02\x15\x85x\xd2\x85\xfd\x1fO{6\xf2\x1b2\x97\xd6\xf0\xe3\x00\x01\x00$\xbd'}, service_data={'0000180a-0000-1000-8000-00805f9b555': b'\x05\x01\xc2c'}, rssi=-88)
Here we can get mac addresses
but there isnt
import asyncio from uuid import UUID
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
ibeacon_format = Struct( "type_length" / Const(b"\x02\x15"), "uuid" / Array(16, Byte), "major" / Int16ub, "minor" / Int16ub, "power" / Int8sl, )
def device_found( device: BLEDevice, advertisement_data: AdvertisementData ): """Decode iBeacon.""" try: macadress = BleakScanner.discover(return_adv=True) // => problem line name = advertisement_data.local_name apple_data = advertisement_data.manufacturer_data[0x004C] ibeacon = ibeacon_format.parse(apple_data) uuid = UUID(bytes=bytes(ibeacon.uuid))
print(f"Mac Adress : {macadress[0]}")// => problem line
print(f"Local Name : {name}")
print(f"UUID : {uuid}")
print(f"Major : {ibeacon.major}")
print(f"Minor : {ibeacon.minor}")
print(f"TX power : {ibeacon.power} dBm")
print(f"RSSI : {device.rssi} dBm")
print(47 * "-")
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(device_found)
while True:
await scanner.start()
await asyncio.sleep(1.0)
await scanner.stop()
asyncio.run(main())
result = TypeError: 'coroutine' object is not subscriptable
What is the coroutine object? How can i get MAC adress information with thesee methods?
Executing Code
import asyncio
from bleak import BleakScanner
async def main():
print("scanning for 5 seconds, please wait...")
devices = await BleakScanner.discover(return_adv=True)
for d, a in devices.values():
print()
print(d) # beacon names are POI
print("-" * len(str(d)))
print(a)
if __name__ == "__main__":
asyncio.run(main())
FF:CC:DD:KK:8A:01: POI
AdvertisementData(local_name='POI', manufacturer_data={76: b'\x02\x15\x85x\xd2\x85\xfd\x1fO{6\xf2\x1b2\x97\xd6\xf0\xe3\x00\x01\x00$\xbd'}, service_data={'0000180a-0000-1000-8000-00805f9b555': b'\x05\x01\xc2c'}, rssi=-88)
Here we can get mac addresses
but there isnt
import asyncio
from uuid import UUID
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
ibeacon_format = Struct(
"type_length" / Const(b"\x02\x15"),
"uuid" / Array(16, Byte),
"major" / Int16ub,
"minor" / Int16ub,
"power" / Int8sl,
)
def device_found(
device: BLEDevice, advertisement_data: AdvertisementData
):
"""Decode iBeacon."""
try:
macadress = BleakScanner.discover(return_adv=True) // => problem line
name = advertisement_data.local_name
apple_data = advertisement_data.manufacturer_data[0x004C]
ibeacon = ibeacon_format.parse(apple_data)
uuid = UUID(bytes=bytes(ibeacon.uuid))
print(f"Mac Adress : {macadress[0]}")// => problem line
print(f"Local Name : {name}")
print(f"UUID : {uuid}")
print(f"Major : {ibeacon.major}")
print(f"Minor : {ibeacon.minor}")
print(f"TX power : {ibeacon.power} dBm")
print(f"RSSI : {device.rssi} dBm")
print(47 * "-")
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(device_found)
while True:
await scanner.start()
await asyncio.sleep(1.0)
await scanner.stop()
asyncio.run(main())
result = TypeError: 'coroutine' object is not subscriptable