从 Android 或 IOS 应用程序到 Pi Pico W 的蓝牙数据传输

问题描述 投票:0回答:1

我需要一些帮助。 (如果这个问题不符合正确的结构,请提前道歉,我不经常在这里提问。)

我有一个项目需要做一些我认为很简单的事情。不幸的是,似乎文档对我来说有点太复杂,或者不是我特别需要的东西。

项目: 通过蓝牙使用应用程序连接到我的 Pi Pico W。向 pico 提供 Wifi 详细信息。将详细信息存储在本地,然后连接到提供的 Wifi Ap。

我有一个使用 Jetpack Compose (Kotlin) 构建的 Android 应用程序。到目前为止,应用程序可以搜索蓝牙设备并连接到它们。

我还有一个运行 micro python 的 Raspberry Pi Pico W。这就是事情变得复杂的地方。

到目前为止,使用 aioble 我可以宣传我的设备并连接到它。

我似乎无法获取有关如何接收数据并将其处理为 JSON 文件的信息。

任何指针或易于阅读的文档链接将不胜感激。也请随意指出对这个问题还有什么帮助。

下面的代码来自 YT 教程。

import sys
import aioble
import bluetooth
import machine
import uasyncio as asyncio
from micropython import const

def uuid():
    return "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format(
        *machine.unique_id()
    )

MANUFACTURER_ID = const(0x02A29)
MODEL_NUMBER_ID = const(0x2A24)
SERIAL_NUMBER_ID = const(0x2A25)
HARDWARE_REVISION_ID = const(0x2A26)
BLE_VERSION_ID = const(0x2A28)

led = machine.Pin("LED", machine.Pin.OUT)

_GENERIC = bluetooth.UUID(0x1848)
_BLE_APPEARANCE = const(960)

ADV_INTERVAL_MS = 250_000

device_info = aioble.Service(_GENERIC)

connection = None

#Characteristics for device info
aioble.Characteristic(device_info, bluetooth.UUID(MANUFACTURER_ID), read=True, initial="AuraByte")
aioble.Characteristic(device_info, bluetooth.UUID(MODEL_NUMBER_ID), read=True, initial="1.0")
aioble.Characteristic(device_info, bluetooth.UUID(SERIAL_NUMBER_ID), read=True, initial=uuid())
aioble.Characteristic(device_info, bluetooth.UUID(HARDWARE_REVISION_ID), read=True, initial=sys.version)
aioble.Characteristic(device_info, bluetooth.UUID(BLE_VERSION_ID), read=True, initial="1.0")

async def peripheral_task():
    print('peripheral task started')
    global connected, connection
    while True:
        connected = False
        async with await aioble.advertise(
            ADV_INTERVAL_MS, 
            name="AuraByteGateModule", 
            appearance=_BLE_APPEARANCE, 
            services=[_GENERIC]
        ) as connection:
            print("Connection from", connection.device)
            connected = True
            print(f"connected: {connected}")
            await connection.disconnected()
            print(f'disconnected')
        

async def blink_task():
    print('blink task started')
    toggle = True
    while True:
        led.value(toggle)
        toggle = not toggle
        blink = 1000
        if connected:
            blink = 1000
        else:
            blink = 250
        await asyncio.sleep_ms(blink)

async def main():
    tasks = [
        asyncio.create_task(peripheral_task()),
        asyncio.create_task(blink_task())
    ]
    await asyncio.gather(*tasks)

asyncio.run(main())
android-jetpack-compose micropython raspberry-pi-pico aioble
1个回答
0
投票

虽然这个问题看起来有点离题,但解决方案并不难实现:

您所需要做的就是在 pico pi 上设置蓝牙 LE GATT 服务器,该服务器将从四个应用程序获取 WiFi 凭据。幸运的是,其他人已经完成并记录了这一点。例如,请点击下面的链接,了解需要考虑的事项的详细说明:

如何使用低功耗蓝牙进行 Wi-Fi 调试

© www.soinside.com 2019 - 2024. All rights reserved.