Skip to content

Android devices can't make serial connections #205

@RaidZeroSix

Description

@RaidZeroSix

Not sure if this goes in Mavsdk-Java or not but anyways...

It is known that Mavsdk_Server can't start a serial connection on Android platforms because of Android's security features. To get around that some people make a bridge that turns serial to UDP and UDP back to serial. Although this works, it's certainly ugly and undesirable since MAVSDK already can handle serial data.

An ideal fix would allow someone to grab a file descriptor in Java and then pass that to mavsdk_server as so:

// 1. Open the USB UART with the Android API
val driver   = UsbSerialProber.getDefaultProber().findAllDrivers(usbMgr).first()
val conn     = usbMgr.openDevice(driver.device)          // permission dialog first!
val port     = driver.ports[0].apply {
                  open(conn)
                  setParameters(57600, 8, STOPBITS_1, PARITY_NONE)
              }

// 2. Extract the raw FD that UsbManager already owns
val fd = if (Build.VERSION.SDK_INT >= 29) conn.fileDescriptor
         else UsbDeviceConnection::class.java
                 .getDeclaredField("mFileDescriptor").apply { isAccessible = true }
                 .getInt(conn)

// 3. Hand that FD to mavsdk_server
val uri = "serial://fd:$fd:57600"
MavsdkEventQueue.executor().execute {
    MavsdkServer().run(uri, 50051)   // Java binding → native C++ core
}
val drone = System("127.0.0.1", 50051)

I'm not an expert Android dev by any means but this is supposedly the correct way to have native code access the USB port.

I haven't looked too deeply into it but by adding something to whatever parses that input string such that it picks up serial://fd:$fd:$baudRate that would be awesome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions