quinzyamg
quinzyamg

Reputation: 11

Bluetooth Data Transfer from Android or IOS App to a Pi Pico W

I need some help here. (Apologies in advance if this question does not meet the correct structure I don't ask questions here often.)

I have a project that needs to do something I thought to be simple. Unfortunately it seems that documentation is either a bit too complex for me or not the thing i need specifically.

The Project: Connect to my Pi Pico W with an App through Bluetooth. Provide the pico with Wifi details. Store the details locally and then connect to the Wifi Ap Provided.

I have a Android app built in Jetpack Compose (Kotlin). So far the app can search for Bluetooth devices and connect to them.

I also have a Raspberry Pi Pico W running micro python. This is where things become complex.

So far using aioble i can Advertise my device and connect to it.

I just can't seem to get info on how to receive data and process it into a JSON file.

Any pointers or easy to read links with documentation will be much appreciated. Also feel free to point out what else would be helpful in this question.

The code below is from a YT tutorial.

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())

Upvotes: 1

Views: 780

Answers (1)

Risto
Risto

Reputation: 1648

While the question may seem a little off-topic, the solution is not that difficult to realise:

All you need to do is set up a Bluetooth LE GATT server on your pico pi, which will take the WiFi credentials from your apps. Fortunately, this has already been done and documented by other people. As an example, follow the link below for a detailed description of things to consider:

How to use Bluetooth Low-Energy for Wi-Fi commissioning

Upvotes: 1

Related Questions