Android Auto API

The Java Auto API package is used to parse Auto API command bytes to native Java objects.
It is open source and available on GitHub.

Create the command bytes

Every AutoAPI command has a designated class for it in the com.highmobility.autoapi package with the same name. Use the designated class's constructor to create the command object that can be sent to the vehicle.
Here are some examples for common vehicle commands:

// Get Vehicle Status
Command command = new VehicleStatus.GetVehicleStatus();

// Get Capabilities
Command command = new Capabilities.GetCapabilities();

// Get Vehicle Location
Command command = new VehicleLocation.GetVehicleLocation();

// Some commands require parameters
Command command = new Doors.LockUnlockDoors(LockState.UNLOCKED);

Parse commands

Use the CommandResolver to parse incoming command bytes into Java objects. Every Auto API command
has a designated class for it in the com.highmobility.autoapi package with the same name. Check
if the CommandResolver returns an object with the class you are looking for.

Vehicle Information

Use the CommandResolver to resolve the VehicleInformation command.

Command command = CommandResolver.resolve(bytes);

// check whether command is VehicleInformation
if (command instanceof VehicleInformation.State) {
    VehicleInformation.State vehicleInfo = (VehicleInformation.State) command;

    // Get the VIN number
    vehicleInfo.getVin();

    // Check the power train type
    if (vehicleInfo.getPowertrain().getValue() == VehicleInformation.Powertrain.ALL_ELECTRIC) {
        // vehicle has all electric power train
    }
}

Capabilities

Use the CommandResolver to resolve the Capabilites command.

Command command = CommandResolver.resolve(bytes);

// check whether command is Capabilities
if (command instanceof Capabilities.State) {
    Capabilities.State capabilities = (Capabilities.State) command;

    if (capabilities.getSupported(Doors.IDENTIFIER,
        Doors.PROPERTY_LOCKS_STATE)) {
        // Vehicle supports the door locks property. You can query/set the
        // locks state with LockUnlockDoors.
    }

    if (capabilities.getSupported(Trunk.IDENTIFIER,
        Trunk.PROPERTY_LOCK)) {
        // Vehicle supports the trunk position property. You can query/set the trunk
        // position with the ControlTrunk command.
    }
}

Vehicle Location

Use the CommandResolver to resolve the Vehicle Location command.

Command command = CommandResolver.resolve(bytes);

// check whether command is Vehicle Location
if (command instanceof VehicleLocation.State) {
    VehicleLocation.State location = (VehicleLocation.State) command;

    // coordinates
    location.getCoordinates().getValue().getLatitude();
    location.getCoordinates().getValue().getLongitude();

    // heading
    location.getHeading().getValue();
}

Lock State

Use the CommandResolver to resolve the Doors command.

Command command = CommandResolver.resolve(bytes);

// check whether command is Doors
if (command instanceof Doors.State) {
    Doors.State state = (Doors.State) command;

    // lock state for a specific door
    state.getLock(Location.FRONT_LEFT).getValue();

    // lock states for all of the available doors
    LockState left = null, right = null, rearRight = null, rearLeft = null;
    for (Property<Lock> lockState : state.getLocks()) {
        if (lockState.getValue() == null) continue;
        switch (lockState.getValue().getLocation()) {
            case FRONT_LEFT:
                left = lockState.getValue().getLockState();
                break;
            case FRONT_RIGHT:
                right = lockState.getValue().getLockState();
                break;
            case REAR_RIGHT:
                rearRight = lockState.getValue().getLockState();
                break;
            case REAR_LEFT:
                rearLeft = lockState.getValue().getLockState();
                break;
        }
    }
}

Check failed command type

Commands can fail. To understand the reason, a Failure Command is returned from the vehicle with some information.
Use the CommandResolver to parse the Failure command

Command command = CommandResolver.resolve(bytes);

if (command instanceof FailureMessage.State) {
    FailureMessage.State failure = (FailureMessage.State) command;
    if (failure.getFailedMessageID().getValue() == Identifier.VEHICLE_STATUS &&
            failure.getFailedMessageType().getValue() == Type.GET) {
        // The Get Vehicle Status command failed.
        if (failure.getFailureReason().getValue() == FailureMessage.FailureReason.UNAUTHORISED) {
            // The command failed because the vehicle is not authorized. Try to connect
            // to vehicle again
        }
    }
}

More commands

All of the other Auto API commands are handled the same way. Browse the pages under Android Code Reference for all of the available commands in Android. See AutoAPI documentation for all of the available commands.