英文:
how to get battery level of the dji remote controller in android app (built using android studio - java)?
问题
以下是翻译好的部分:
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.
下面的代码可以检索 DJI 无人机遥控器的序列号。然而,我无法获取电池电量。有没有办法可以获取电池电量?请提供建议。
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);
}
英文:
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
得分: 1
你提供的代码看起来是正确的,假设baseProduct是对您的DJI遥控器的有效引用。
但是,要获取遥控器的电池电量,您需要调用getRemoteControllerBatteryInfo方法,而不是getBatteryLevel,如下所示:
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);
}
}
这将返回遥控器的电池电量,作为介于0和100之间的整数,表示剩余充电的百分比。
确保您的应用程序具有访问DJI SDK函数所需的权限,并在调用此方法之前,遥控器已连接到飞行器并打开。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论