在工程的build.gradle
添加implementation 'io.dronefleet.mavlink:mavlink:1.1.11'
。
点击右上角sync
// This example uses a TCP socket, however we may also use a UDP socket by injecting
// PipedInputStream/PipedOutputStream to MavlinkConnection, or even USB by using any
// implementation that will eventually yield an InputStream and an OutputStream.
try (Socket socket = new Socket("127.0.0.1", 5760)) {
// After establishing a connection, we proceed to building a MavlinkConnection instance.
MavlinkConnection connection = MavlinkConnection.create(
socket.getInputStream(),
socket.getOutputStream());
// Now we are ready to read and send messages.
MavlinkMessage message;
while ((message = connection.next()) != null) {
// The received message could be either a Mavlink1 message, or a Mavlink2 message.
// To check if the message is a Mavlink2 message, we could do the following:
if (message instanceof Mavlink2Message) {
// This is a Mavlink2 message.
// When a message is received, its payload type isn't statically available.
// We can resolve which kind of message it is by its payload, like so:
if (message.getPayload() instanceof Heartbeat) {
// This is a heartbeat message
MavlinkMessage<Heartbeat> heartbeatMessage = (MavlinkMessage<Heartbeat>)message;
}
// We are better off by publishing the payload to a pub/sub mechanism such
// as RxJava, JMS or any other favorite instead, though.
}
} catch (EOFException eof) {
// The stream has ended.
}
int systemId = 255;
int componentId = 0;
Heartbeat heartbeat = Heartbeat.builder()
.type(MavType.MAV_TYPE_GCS)
.autopilot(MavAutopilot.MAV_AUTOPILOT_INVALID)
.systemStatus(MavState.MAV_STATE_UNINIT)
.mavlinkVersion(3)
.build()
// Write an unsigned heartbeat
connection.send2(systemId, componentId, heartbeat);