dji-sdk / Mobile-SDK-Android

DJI Mobile SDK for Android: http://developer.dji.com/mobile-sdk/
Other
996 stars 581 forks source link

Battery Voltage #1287

Open Vickyxdev opened 7 months ago

Vickyxdev commented 7 months ago

How to get Battery Voltage in DJI SDK 4.16?

danilofariadutra commented 7 months ago

If your aircraft has just one battery:

try {
            DJISampleApplication.getProductInstance().getBattery().setStateCallback(new BatteryState.Callback() {
                @Override
                public void onUpdate(BatteryState batteryState) {
                    batteryState.getVoltage();
                }
            });
        } catch (Exception exception) {
            ToastUtils.showToast(String.format("Error in getting battery voltage %s", exception.getMessage()));
        }

If your aircraft has more than one battery:

      int batteryPos = 0;
        for (Battery battery : DJISampleApplication.getProductInstance().getBatteries()) {
            int finalBatteryPos = batteryPos;
            try {
                battery.setStateCallback(djiBatteryState -> {
                    showToast(String.format("Battery %d: %d", finalBatteryPos, djiBatteryState.getVoltage()));
                });
            } catch (Exception exception) {
                ToastUtils.showToast(String.format("Error in getting battery voltage %s", exception.getMessage()));
            }
            batteryPos++;
        }
Vickyxdev commented 7 months ago

thanks for information

dji-lyt commented 7 months ago

Thank you for @danilofariadutra response. The method you provided is indeed the correct way to obtain battery information in MSDK V4.