You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio(Java)开发的App中获取DJI遥控器电量?

解决DJI遥控器电量获取问题

你的代码问题在于:你获取的是无人机飞行器的电池电量,而非遥控器的电量。要获取遥控器电量,需要使用DJI SDK中的RemoteController类,而非飞行器的Battery类。

正确实现方案

1. 单次获取遥控器电量(回调方式)

public void getRemoteControllerBatteryLevel(CommonCallbacks.CompletionCallbackWith<Integer> callback) {
    if (baseProduct != null && baseProduct.isConnected()) {
        if (baseProduct instanceof Aircraft) {
            RemoteController remoteController = ((Aircraft) baseProduct).getRemoteController();
            if (remoteController != null) {
                // 获取遥控器剩余电量(百分比,范围0-100)
                remoteController.getBatteryPercentage(callback);
            } else {
                callback.onFailure(new DJIError("遥控器未连接"));
            }
        } else {
            callback.onFailure(new DJIError("当前连接产品不是飞行器"));
        }
    } else {
        callback.onFailure(new DJIError("产品未连接"));
    }
}

2. 实时监听遥控器电量变化(适合持续更新场景)

如果需要实时获取电量变化,推荐注册监听器:

// 注册遥控器电量监听器
private void registerRCBatteryListener() {
    if (baseProduct != null && baseProduct.isConnected() && baseProduct instanceof Aircraft) {
        RemoteController rc = ((Aircraft) baseProduct).getRemoteController();
        if (rc != null) {
            rc.setBatteryStateListener(batteryState -> {
                int batteryPercent = batteryState.getPercentage();
                // 在这里处理电量更新,比如刷新UI
            });
        }
    }
}

// 在页面销毁或不需要监听时移除监听器
private void unregisterRCBatteryListener() {
    if (baseProduct != null && baseProduct instanceof Aircraft) {
        RemoteController rc = ((Aircraft) baseProduct).getRemoteController();
        if (rc != null) {
            rc.setBatteryStateListener(null);
        }
    }
}

注意事项

  • 确保使用的DJI Mobile SDK是最新稳定版,旧版本API可能存在差异
  • 必须保证遥控器与飞行器已正常配对连接,否则RemoteController实例会为空
  • 确认App已配置DJI SDK要求的权限(如ACCESS_FINE_LOCATION等,需在AndroidManifest.xml中声明)

内容的提问来源于stack exchange,提问作者Shareef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 01:12:16