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

DJI Mavic 2 Pro Windows SDK数据无变化问题排查求助

问题

我正尝试通过DJI Windows SDK访问DJI Mavic 2 Pro的数据,已按照DJI官方教程完成代码配置。
打印appActivationState显示状态为ACTIVATED,aircraftBindindState显示为NOT_REQUIRED,SDKRegistrationResultCode显示为NO_ERROR,ProductIndex和ComponentIndex均为0,这些状态均正常。
但打印电池剩余量、温度等无人机数据时,输出始终不变,并非预期的随电池消耗变化的整数值,而是异步任务相关的对象信息(如System.Runtime.CompilerServices.AsyncTaskMethodBuilder...)。

我的代码如下:

using DJI.WindowsSDK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using DJI.WindowsSDK.Components;
using DJI.WindowsSDK.FlySafe;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace DJIWSDKDemo
{    
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
        this.InitializeComponent();
        DJISDKManager.Instance.SDKRegistrationStateChanged += Instance_SDKRegistrationEvent;

        //Replace with your registered App Key. Make sure your App Key matched your application's package name on DJI developer center.
        DJISDKManager.Instance.RegisterApp("c086d1c2xxxxxxxxxxxxxxxx");
        }
        private async void Instance_SDKRegistrationEvent(SDKRegistrationState state, SDKError resultCode)
        {
            if (resultCode == SDKError.NO_ERROR)
            {
                System.Diagnostics.Debug.WriteLine("Register app successfully.");

                //The product connection state will be updated when it changes here.
                DJISDKManager.Instance.ComponentManager.GetProductHandler(0).ProductTypeChanged += async delegate (object sender, ProductTypeMsg? value)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        if (value != null && value?.value != ProductType.UNRECOGNIZED)
                        {
                            System.Diagnostics.Debug.WriteLine("The Aircraft is connected now.");

                            while (true) {
                                System.Threading.Thread.Sleep(3000);
                                System.Diagnostics.Debug.WriteLine("The App Acitvation State is: " + DJISDKManager.Instance.appActivationState);
                                System.Diagnostics.Debug.WriteLine("The Aircraft Binding State: " + DJISDKManager.Instance.aircraftBindindState);
                                System.Diagnostics.Debug.WriteLine("SDK Registeration Result Code: " + DJISDKManager.Instance.SDKRegistrationResultCode);
                                System.Diagnostics.Debug.WriteLine("Product Index: " + DJISDKManager.Instance.ComponentManager.GetBatteryHandler(0, 0).ProductIndex);
                                System.Diagnostics.Debug.WriteLine("Component Index: " + DJISDKManager.Instance.ComponentManager.GetBatteryHandler(0, 0).ComponentIndex);
                                System.Diagnostics.Debug.WriteLine("Remaining Battery: " + DJISDKManager.Instance.ComponentManager.GetBatteryHandler(0, 0).GetChargeRemainingAsync());
                                System.Diagnostics.Debug.WriteLine("Battery Temperature: " + DJISDKManager.Instance.ComponentManager.GetBatteryHandler(0, 0).GetBatteryTemperatureAsync());
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("The Aircraft is disconnected now.");
                            //You can hide your pages according to the aircraft connection state here, or show the connection tips to the users.
                        }

                    });
                };
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Register SDK failed, the error is: ");
                System.Diagnostics.Debug.WriteLine(resultCode.ToString());
            }
        }
    }
}

运行代码后,电池剩余量和温度的输出为异步任务对象,始终无变化,无法获取实际数值。请问问题出在哪里,该如何修复?


解决方法

问题根源

  1. 未正确等待异步方法执行:GetChargeRemainingAsync()和GetBatteryTemperatureAsync()是异步方法,直接输出会返回Task对象而非实际结果,必须用await关键字等待任务完成后获取返回值。
  2. 阻塞UI线程:在UI线程的Dispatcher回调中使用System.Threading.Thread.Sleep(3000)会阻塞整个UI界面,导致应用无响应,应该用异步的await Task.Delay(3000)替代。

修复后的代码

using DJI.WindowsSDK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using DJI.WindowsSDK.Components;
using DJI.WindowsSDK.FlySafe;
using System.Threading.Tasks;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace DJIWSDKDemo
{    
    public sealed partial class MainPage : Page
    {
        private bool _isMonitoring = false;

        public MainPage()
        {
            this.InitializeComponent();
            DJISDKManager.Instance.SDKRegistrationStateChanged += Instance_SDKRegistrationEvent;

            //Replace with your registered App Key. Make sure your App Key matched your application's package name on DJI developer center.
            DJISDKManager.Instance.RegisterApp("c086d1c2xxxxxxxxxxxxxxxx");
        }

        private async void Instance_SDKRegistrationEvent(SDKRegistrationState state, SDKError resultCode)
        {
            if (resultCode == SDKError.NO_ERROR)
            {
                System.Diagnostics.Debug.WriteLine("Register app successfully.");

                //The product connection state will be updated when it changes here.
                DJISDKManager.Instance.ComponentManager.GetProductHandler(0).ProductTypeChanged += async (sender, value) =>
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
                    {
                        if (value != null && value?.value != ProductType.UNRECOGNIZED)
                        {
                            System.Diagnostics.Debug.WriteLine("The Aircraft is connected now.");
                            if (!_isMonitoring)
                            {
                                _isMonitoring = true;
                                await StartBatteryMonitoring();
                            }
                        }
                        else
                        {
                            System.Diagnostics.Debug.WriteLine("The Aircraft is disconnected now.");
                            _isMonitoring = false;
                            //You can hide your pages according to the aircraft connection state here, or show the connection tips to the users.
                        }
                    });
                };
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Register SDK failed, the error is: ");
                System.Diagnostics.Debug.WriteLine(resultCode.ToString());
            }
        }

        private async Task StartBatteryMonitoring()
        {
            var batteryHandler = DJISDKManager.Instance.ComponentManager.GetBatteryHandler(0, 0);
            while (_isMonitoring)
            {
                await Task.Delay(3000);
                
                System.Diagnostics.Debug.WriteLine("The App Acitvation State is: " + DJISDKManager.Instance.appActivationState);
                System.Diagnostics.Debug.WriteLine("The Aircraft Binding State: " + DJISDKManager.Instance.aircraftBindindState);
                System.Diagnostics.Debug.WriteLine("SDK Registeration Result Code: " + DJISDKManager.Instance.SDKRegistrationResultCode);
                System.Diagnostics.Debug.WriteLine("Product Index: " + batteryHandler.ProductIndex);
                System.Diagnostics.Debug.WriteLine("Component Index: " + batteryHandler.ComponentIndex);
                
                // 等待异步方法完成并获取实际数值
                var remainingCharge = await batteryHandler.GetChargeRemainingAsync();
                System.Diagnostics.Debug.WriteLine("Remaining Battery: " + remainingCharge?.value);
                
                var batteryTemp = await batteryHandler.GetBatteryTemperatureAsync();
                System.Diagnostics.Debug.WriteLine("Battery Temperature: " + batteryTemp?.value);
            }
        }
    }
}

额外优化说明

  • 添加_isMonitoring标志,避免重复启动监控循环,无人机断开时自动停止循环。
  • 将电池处理器的获取逻辑提取到循环外,减少重复调用GetBatteryHandler的开销。
  • 用lambda表达式替代匿名委托,代码更简洁易读。
  • 新增using System.Threading.Tasks;引用,确保Task.Delay可用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 15:55:18