how to get battery level of the dji remote controller in android app (built using android studio - java)?

126 Views Asked by At

with the code below i am able to retrieve serial number of the dji drone remote controller. however, I cannot get battery level. is there anyway I can retrieve battery level? please advise.

the code for serial number (which is fine):

public void getSerialNumber(CommonCallbacks.CompletionCallbackWith<String> callback)
    {
        if(baseProduct != null && baseProduct.isConnected()) {
            if (baseProduct instanceof Aircraft) {
                ((Aircraft) baseProduct).getFlightController().getSerialNumber(callback);
            }
            else callback.onFailure(null);
        }
        else callback.onFailure(null);
    }

the code for remaining battery (which doesn't work):

public void getBatteryLevel(CommonCallbacks.CompletionCallbackWith<Integer> callback)
    {
        if(baseProduct != null && baseProduct.isConnected()) {
            if (baseProduct instanceof Aircraft) {
                ((Aircraft) baseProduct).getBattery().getChargeRemaining(callback);
            }
            else callback.onFailure(null);
        }
        else callback.onFailure(null);
    }
1

There are 1 best solutions below

1
Gopi Arumugam On

The code you have provided looks correct, assuming that baseProduct is a valid reference to your DJI remote controller.

However, to get the battery level of the remote controller, you need to call the getRemoteControllerBatteryInfo method instead of getBatteryLevel as follows:

public void getBatteryLevel(CommonCallbacks.CompletionCallbackWith<Integer> callback) {
    if (baseProduct != null && baseProduct.isConnected()) {
        if (baseProduct instanceof Aircraft) {
            ((Aircraft) baseProduct).getRemoteController().getBatteryInfo().getChargeRemainingInPercent(callback);
        } else {
            callback.onFailure(null);
        }
    } else {
        callback.onFailure(null);
    }
}

This will return the battery level of the remote controller as an integer between 0 and 100, representing the percentage of charge remaining.

Make sure that your app has the necessary permissions to access the DJI SDK functions, and that the remote controller is connected to the aircraft and turned on before calling this method.