Skip to content

Unable to send a message via BLE #78

@adria0

Description

@adria0

Unable to send a message via BLE

Streaming for receiving packets works well, but I'm unable to send messages. When I attempt to send one, the Bluetooth connection freezes, and no other operations can be performed.

I used an example from another repository (I can’t recall which one), which I assumed was functional, but I’m not sure if there’s an issue with it.

To Reproduce


use std::time::Duration;

use anyhow::Result;
use meshtastic::api::{ConnectedStreamApi, StreamApi};
use meshtastic::protobufs::MeshPacket;
use meshtastic::protobufs::PortNum;
use meshtastic::protobufs::{mesh_packet, Data};
use meshtastic::utils;
use meshtastic::utils::stream::BleId;

async fn send_to_meshtastic(stream_api: &mut ConnectedStreamApi, text: &str) -> Result<()> {
    // Create a text message data payload
    let data = Data {
        portnum: PortNum::TextMessageApp as i32,
        payload: text.as_bytes().to_vec(),
        want_response: false,
        ..Default::default()
    };

    // Create mesh packet for broadcast
    let mesh_packet = MeshPacket {
        to: 0xffffffff, // Broadcast address
        from: 0,        // Will be filled by the device
        channel: 0,
        id: 0, // Will be assigned by the device
        priority: mesh_packet::Priority::Default as i32,
        payload_variant: Some(mesh_packet::PayloadVariant::Decoded(data)),
        ..Default::default()
    };

    // Create the payload variant
    let payload_variant = Some(meshtastic::protobufs::to_radio::PayloadVariant::Packet(
        mesh_packet,
    ));

    // Send using the stream API's send_to_radio_packet method
    println!("Attempting to send packet to Meshtastic radio...");
    match stream_api.send_to_radio_packet(payload_variant).await {
        Ok(_) => {
            println!("Successfully sent to Meshtastic: {}", text);
            Ok(())
        }
        Err(e) => {
            println!("Failed to send to Meshtastic: {}", e);
            Err(anyhow::anyhow!("Failed to send message: {}", e))
        }
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let stream_api = StreamApi::new();

    let ble_device = std::env::args().nth(1).unwrap();

    let ble_stream =
        utils::stream::build_ble_stream(&BleId::from_name(&ble_device), Duration::from_secs(5))
            .await?;

    let (mut _decoded_listener, stream_api) = stream_api.connect(ble_stream).await;

    let config_id = utils::generate_rand_id();

    let mut stream_api = stream_api.configure(config_id).await?;
    send_to_meshtastic(&mut stream_api, "Hallo world").await?;

    let _stream_api = stream_api.disconnect().await?;

    Ok(())
}

Expected behavior
"Hallo world" is broadcasted

Additional context
Tested in OSX and Raspberry Pi.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions