我有一个已变成 Eddystone URL 信标的树莓派,我希望使用 python 在我的笔记本电脑上读取该 URL,因为我必须在我的网络应用程序中进一步使用该 URL。
问问题
284 次
1 回答
1
我使用 Python 在 Windows 10 上成功扫描信标的唯一方法是使用 Windows 运行时 Python 投影 ( Python/WinRT )
下面是扫描和打印 Eddystone、iBeacon 和 AltBeacon 数据的示例:
import asyncio
import uuid
import winrt.windows.devices.bluetooth.advertisement as winBLE
from winrt.windows.storage.streams import DataReader
async def discover():
def process_eddystone(data):
_url_prefix_scheme = ['http://www.', 'https://www.',
'http://', 'https://', ]
_url_encoding = ['.com/', '.org/', '.edu/', '.net/', '.info/',
'.biz/', '.gov/', '.com', '.org', '.edu',
'.net', '.info', '.biz', '.gov']
tx_pwr = int.from_bytes([data[1]], 'big', signed=True)
if data[0] == 0x00:
namespace_id = int.from_bytes(data[2:12], 'big')
instance_id = int.from_bytes(data[12:18], 'big')
print(f'\t\tEddystone UID: {namespace_id} '
f'- {instance_id} \u2197 {tx_pwr}')
elif data[0] == 0x10:
prefix = data[2]
encoded_url = data[3:]
full_url = _url_prefix_scheme[prefix]
for letter in encoded_url:
if letter < len(_url_encoding):
full_url += _url_encoding[letter]
else:
full_url += chr(letter)
print(f'\t\tEddystone URL: {full_url} \u2197 {tx_pwr}')
def process_ibeacon(data, beacon_type='iBeacon'):
beacon_uuid = uuid.UUID(bytes=bytes(data[2:18]))
major = int.from_bytes(bytearray(data[18:20]), 'big', signed=False)
minor = int.from_bytes(bytearray(data[20:22]), 'big', signed=False)
tx_pwr = int.from_bytes([data[22]], 'big', signed=True)
print(f'\t\t{beacon_type}: {beacon_uuid} - {major} '
f'- {minor} \u2197 {tx_pwr}')
def on_advert(sender, evt):
for s_data_buf in evt.advertisement.data_sections:
if s_data_buf.data_type == 0x16:
data_reader = DataReader.from_buffer(s_data_buf.data)
s_data = data_reader.read_bytes(s_data_buf.data.length)
if s_data[0:2] == [0xaa, 0xfe]:
process_eddystone(s_data[2:])
for m_data_buf in evt.advertisement.manufacturer_data:
if m_data_buf.company_id == 0x004c:
data_reader = DataReader.from_buffer(m_data_buf.data)
m_data = data_reader.read_bytes(m_data_buf.data.length)
if m_data[0] == 0x02:
process_ibeacon(m_data)
elif m_data_buf.company_id == 0xffff:
data_reader = DataReader.from_buffer(m_data_buf.data)
m_data = data_reader.read_bytes(m_data_buf.data.length)
if m_data[0:2] == [0xbe, 0xac]:
process_ibeacon(m_data, 'AltBeacon')
watcher = winBLE.BluetoothLEAdvertisementWatcher()
watcher.add_received(on_advert)
watcher.start()
await asyncio.sleep(25)
watcher.stop()
asyncio.run(discover())
于 2020-12-07T06:55:52.030 回答