|
@@ -10,56 +10,78 @@ import dji.v5.manager.KeyManager;
|
|
|
import dji.v5.manager.SDKManager;
|
|
import dji.v5.manager.SDKManager;
|
|
|
import dji.sdk.keyvalue.key.FlightControllerKey;
|
|
import dji.sdk.keyvalue.key.FlightControllerKey;
|
|
|
import dji.sdk.keyvalue.key.RemoteControllerKey;
|
|
import dji.sdk.keyvalue.key.RemoteControllerKey;
|
|
|
|
|
+import lombok.Getter;
|
|
|
|
|
+import lombok.Setter;
|
|
|
|
|
|
|
|
import static dji.sdk.keyvalue.key.DJIKey.create;
|
|
import static dji.sdk.keyvalue.key.DJIKey.create;
|
|
|
|
|
|
|
|
import com.paul.drone.manager.DJISDKManager;
|
|
import com.paul.drone.manager.DJISDKManager;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 设备连接状态管理器
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * 设备连接与飞行状态管理器
|
|
|
|
|
+ *
|
|
|
* 功能:
|
|
* 功能:
|
|
|
* 1. 监听无人机连接状态 (FlightControllerKey.KeyConnection)
|
|
* 1. 监听无人机连接状态 (FlightControllerKey.KeyConnection)
|
|
|
* 2. 监听遥控器连接状态 (RemoteControllerKey.KeyConnection)
|
|
* 2. 监听遥控器连接状态 (RemoteControllerKey.KeyConnection)
|
|
|
- * 3. 使用弱引用避免内存泄漏
|
|
|
|
|
- * 4. 支持生命周期管理
|
|
|
|
|
|
|
+ * 3. 监听无人机起飞与降落事件 (FlightControllerKey.KeyIsFlying) // MODIFIED
|
|
|
|
|
+ * 4. 使用弱引用避免内存泄漏
|
|
|
|
|
+ * 5. 支持生命周期管理
|
|
|
*/
|
|
*/
|
|
|
-public class DeviceConnectionManager {
|
|
|
|
|
|
|
+public class DeviceConnectionManager{
|
|
|
|
|
|
|
|
private static final String TAG = "DeviceConnectionManager";
|
|
private static final String TAG = "DeviceConnectionManager";
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ @Getter
|
|
|
|
|
+ @Setter
|
|
|
|
|
+ private volatile Float cachedTotalFlightTime = 0.0f;
|
|
|
|
|
+ @Getter
|
|
|
|
|
+ @Setter
|
|
|
|
|
+ private volatile Double cachedTotalFlightDistance = 0.0;
|
|
|
|
|
+
|
|
|
// 单例
|
|
// 单例
|
|
|
private static volatile DeviceConnectionManager instance;
|
|
private static volatile DeviceConnectionManager instance;
|
|
|
-
|
|
|
|
|
- // 回调接口
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // MODIFIED: 接口增加了起飞和降落的回调
|
|
|
public interface ConnectionStateCallback {
|
|
public interface ConnectionStateCallback {
|
|
|
/**
|
|
/**
|
|
|
* 无人机连接状态变化
|
|
* 无人机连接状态变化
|
|
|
* @param isConnected 是否已连接
|
|
* @param isConnected 是否已连接
|
|
|
*/
|
|
*/
|
|
|
void onDroneConnectionChanged(boolean isConnected);
|
|
void onDroneConnectionChanged(boolean isConnected);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 遥控器连接状态变化
|
|
* 遥控器连接状态变化
|
|
|
- * @param isConnected 是否已连ected
|
|
|
|
|
|
|
+ * @param isConnected 是否已连接
|
|
|
*/
|
|
*/
|
|
|
void onRemoteControllerConnectionChanged(boolean isConnected);
|
|
void onRemoteControllerConnectionChanged(boolean isConnected);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 无人机起飞事件
|
|
|
|
|
+ */
|
|
|
|
|
+ void onDroneTakeoff();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 无人机降落事件
|
|
|
|
|
+ */
|
|
|
|
|
+ void onDroneLanding();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 使用弱引用存储回调,避免内存泄漏
|
|
// 使用弱引用存储回调,避免内存泄漏
|
|
|
private final ConcurrentHashMap<String, WeakReference<ConnectionStateCallback>> callbacks = new ConcurrentHashMap<>();
|
|
private final ConcurrentHashMap<String, WeakReference<ConnectionStateCallback>> callbacks = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 当前连接状态
|
|
// 当前连接状态
|
|
|
private final AtomicBoolean isDroneConnected = new AtomicBoolean(false);
|
|
private final AtomicBoolean isDroneConnected = new AtomicBoolean(false);
|
|
|
private final AtomicBoolean isRemoteControllerConnected = new AtomicBoolean(false);
|
|
private final AtomicBoolean isRemoteControllerConnected = new AtomicBoolean(false);
|
|
|
-
|
|
|
|
|
|
|
+ // ADDED: 新增无人机飞行状态
|
|
|
|
|
+ private final AtomicBoolean isDroneFlying = new AtomicBoolean(false);
|
|
|
|
|
+
|
|
|
// 监听器状态
|
|
// 监听器状态
|
|
|
private volatile boolean isListening = false;
|
|
private volatile boolean isListening = false;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
private DeviceConnectionManager() {
|
|
private DeviceConnectionManager() {
|
|
|
Log.i(TAG, "DeviceConnectionManager 初始化");
|
|
Log.i(TAG, "DeviceConnectionManager 初始化");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取单例实例
|
|
* 获取单例实例
|
|
|
*/
|
|
*/
|
|
@@ -84,20 +106,20 @@ public class DeviceConnectionManager {
|
|
|
Log.w(TAG, "注册回调失败:tag 或 callback 为空");
|
|
Log.w(TAG, "注册回调失败:tag 或 callback 为空");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Log.i(TAG, "注册连接状态回调: " + tag);
|
|
Log.i(TAG, "注册连接状态回调: " + tag);
|
|
|
callbacks.put(tag, new WeakReference<>(callback));
|
|
callbacks.put(tag, new WeakReference<>(callback));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 如果还未开始监听,则启动监听
|
|
// 如果还未开始监听,则启动监听
|
|
|
if (!isListening) {
|
|
if (!isListening) {
|
|
|
startListening();
|
|
startListening();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 立即回调当前状态
|
|
// 立即回调当前状态
|
|
|
callback.onDroneConnectionChanged(isDroneConnected.get());
|
|
callback.onDroneConnectionChanged(isDroneConnected.get());
|
|
|
callback.onRemoteControllerConnectionChanged(isRemoteControllerConnected.get());
|
|
callback.onRemoteControllerConnectionChanged(isRemoteControllerConnected.get());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 取消注册回调
|
|
* 取消注册回调
|
|
|
* @param tag 标识符
|
|
* @param tag 标识符
|
|
@@ -106,30 +128,35 @@ public class DeviceConnectionManager {
|
|
|
if (tag == null) {
|
|
if (tag == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Log.i(TAG, "取消注册连接状态回调: " + tag);
|
|
Log.i(TAG, "取消注册连接状态回调: " + tag);
|
|
|
callbacks.remove(tag);
|
|
callbacks.remove(tag);
|
|
|
-
|
|
|
|
|
- // 如果没有任何回调了,停止监听以节省资源
|
|
|
|
|
- if (callbacks.isEmpty()) {
|
|
|
|
|
- stopListening();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+// // 如果没有任何回调了,停止监听以节省资源
|
|
|
|
|
+// if (callbacks.isEmpty()) {
|
|
|
|
|
+// stopListening();
|
|
|
|
|
+// }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取当前无人机连接状态
|
|
* 获取当前无人机连接状态
|
|
|
*/
|
|
*/
|
|
|
public boolean isDroneConnected() {
|
|
public boolean isDroneConnected() {
|
|
|
return isDroneConnected.get();
|
|
return isDroneConnected.get();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取当前遥控器连接状态
|
|
* 获取当前遥控器连接状态
|
|
|
*/
|
|
*/
|
|
|
public boolean isRemoteControllerConnected() {
|
|
public boolean isRemoteControllerConnected() {
|
|
|
return isRemoteControllerConnected.get();
|
|
return isRemoteControllerConnected.get();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // ADDED: 获取当前无人机是否在飞行
|
|
|
|
|
+ public boolean isDroneFlying() {
|
|
|
|
|
+ return isDroneFlying.get();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 开始监听连接状态
|
|
* 开始监听连接状态
|
|
|
*/
|
|
*/
|
|
@@ -137,47 +164,88 @@ public class DeviceConnectionManager {
|
|
|
if (isListening) {
|
|
if (isListening) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (!DJISDKManager.getInstance().isSDKRegistered()) {
|
|
if (!DJISDKManager.getInstance().isSDKRegistered()) {
|
|
|
Log.w(TAG, "SDK 未初始化,无法开始监听");
|
|
Log.w(TAG, "SDK 未初始化,无法开始监听");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- Log.i(TAG, "开始监听设备连接状态");
|
|
|
|
|
-
|
|
|
|
|
|
|
+ Log.i(TAG, "开始监听设备连接与飞行状态");
|
|
|
|
|
+
|
|
|
// 监听无人机连接状态
|
|
// 监听无人机连接状态
|
|
|
KeyManager.getInstance().listen(create(FlightControllerKey.KeyConnection), this, (oldValue, newValue) -> {
|
|
KeyManager.getInstance().listen(create(FlightControllerKey.KeyConnection), this, (oldValue, newValue) -> {
|
|
|
boolean connected = newValue != null && newValue;
|
|
boolean connected = newValue != null && newValue;
|
|
|
Log.i(TAG, "无人机连接状态变化: " + connected);
|
|
Log.i(TAG, "无人机连接状态变化: " + connected);
|
|
|
-
|
|
|
|
|
- // 更新状态
|
|
|
|
|
|
|
+
|
|
|
isDroneConnected.set(connected);
|
|
isDroneConnected.set(connected);
|
|
|
-
|
|
|
|
|
- // 通知所有有效的回调
|
|
|
|
|
notifyDroneConnectionChanged(connected);
|
|
notifyDroneConnectionChanged(connected);
|
|
|
|
|
+
|
|
|
|
|
+ // ADDED: 如果无人机断开连接,重置飞行状态
|
|
|
|
|
+ if (!connected) {
|
|
|
|
|
+ if (isDroneFlying.get()) {
|
|
|
|
|
+ isDroneFlying.set(false);
|
|
|
|
|
+ notifyDroneLanding(); // 认为连接断开即降落
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 监听遥控器连接状态
|
|
// 监听遥控器连接状态
|
|
|
KeyManager.getInstance().listen(create(RemoteControllerKey.KeyConnection), this, (oldValue, newValue) -> {
|
|
KeyManager.getInstance().listen(create(RemoteControllerKey.KeyConnection), this, (oldValue, newValue) -> {
|
|
|
boolean connected = newValue != null && newValue;
|
|
boolean connected = newValue != null && newValue;
|
|
|
Log.i(TAG, "遥控器连接状态变化: " + connected);
|
|
Log.i(TAG, "遥控器连接状态变化: " + connected);
|
|
|
-
|
|
|
|
|
- // 更新状态
|
|
|
|
|
|
|
+
|
|
|
isRemoteControllerConnected.set(connected);
|
|
isRemoteControllerConnected.set(connected);
|
|
|
-
|
|
|
|
|
- // 通知所有有效的回调
|
|
|
|
|
notifyRemoteControllerConnectionChanged(connected);
|
|
notifyRemoteControllerConnectionChanged(connected);
|
|
|
});
|
|
});
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ //监听无人机飞行状态
|
|
|
|
|
+ KeyManager.getInstance().listen(create(FlightControllerKey.KeyIsFlying), this, (oldValue, newValue) -> {
|
|
|
|
|
+ boolean nowFlying = newValue != null && newValue;
|
|
|
|
|
+ boolean wasFlying = isDroneFlying.get(); // 获取上一次的状态
|
|
|
|
|
+
|
|
|
|
|
+ if (nowFlying != wasFlying) {
|
|
|
|
|
+ Log.i(TAG, "无人机飞行状态变化: " + nowFlying);
|
|
|
|
|
+
|
|
|
|
|
+ if (nowFlying) {
|
|
|
|
|
+ // 如果新状态是在飞行(因为状态变了,说明之前肯定是不在飞)
|
|
|
|
|
+ // -> 起飞事件
|
|
|
|
|
+ Log.i(TAG, "事件: 无人机起飞");
|
|
|
|
|
+ notifyDroneTakeoff();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 如果新状态是不在飞行(因为状态变了,说明之前肯定是在飞)
|
|
|
|
|
+ // -> 降落事件
|
|
|
|
|
+ Log.i(TAG, "事件: 无人机降落");
|
|
|
|
|
+ notifyDroneLanding();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新当前状态
|
|
|
|
|
+ isDroneFlying.set(nowFlying);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ //监听无人机的飞行总距离,以及飞行总时长
|
|
|
|
|
+ KeyManager.getInstance().listen(create(FlightControllerKey.KeyAircraftTotalFlightDuration), this, (oldValue, newValue) -> {
|
|
|
|
|
+ if (newValue != null) {
|
|
|
|
|
+ this.cachedTotalFlightTime = Float.valueOf(String.valueOf(newValue));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 监听总飞行距离
|
|
|
|
|
+ KeyManager.getInstance().listen(create(FlightControllerKey.KeyAircraftTotalFlightDistance), this, (oldValue, newValue) -> {
|
|
|
|
|
+ if (newValue != null) {
|
|
|
|
|
+ this.cachedTotalFlightDistance = newValue;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
isListening = true;
|
|
isListening = true;
|
|
|
- Log.i(TAG, "设备连接状态监听已启动");
|
|
|
|
|
-
|
|
|
|
|
|
|
+ Log.i(TAG, "设备连接与飞行状态监听已启动");
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- Log.e(TAG, "启动设备连接状态监听失败", e);
|
|
|
|
|
|
|
+ Log.e(TAG, "启动设备状态监听失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 停止监听连接状态
|
|
* 停止监听连接状态
|
|
|
*/
|
|
*/
|
|
@@ -185,67 +253,92 @@ public class DeviceConnectionManager {
|
|
|
if (!isListening) {
|
|
if (!isListening) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- Log.i(TAG, "停止监听设备连接状态");
|
|
|
|
|
|
|
+ Log.i(TAG, "停止监听设备连接与飞行状态");
|
|
|
KeyManager.getInstance().cancelListen(this);
|
|
KeyManager.getInstance().cancelListen(this);
|
|
|
isListening = false;
|
|
isListening = false;
|
|
|
- Log.i(TAG, "设备连接状态监听已停止");
|
|
|
|
|
|
|
+ Log.i(TAG, "设备连接与飞行状态监听已停止");
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
- Log.e(TAG, "停止设备连接状态监听失败", e);
|
|
|
|
|
|
|
+ Log.e(TAG, "停止设备状态监听失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 通知无人机连接状态变化
|
|
* 通知无人机连接状态变化
|
|
|
*/
|
|
*/
|
|
|
private void notifyDroneConnectionChanged(boolean isConnected) {
|
|
private void notifyDroneConnectionChanged(boolean isConnected) {
|
|
|
- // 遍历所有回调,清理失效的弱引用
|
|
|
|
|
- callbacks.entrySet().removeIf(entry -> {
|
|
|
|
|
- WeakReference<ConnectionStateCallback> weakRef = entry.getValue();
|
|
|
|
|
|
|
+ callbacks.forEach((tag, weakRef) -> {
|
|
|
ConnectionStateCallback callback = weakRef.get();
|
|
ConnectionStateCallback callback = weakRef.get();
|
|
|
-
|
|
|
|
|
- if (callback == null) {
|
|
|
|
|
- // 回调已被垃圾回收,移除这个条目
|
|
|
|
|
- Log.d(TAG, "清理失效的回调: " + entry.getKey());
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- callback.onDroneConnectionChanged(isConnected);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- Log.e(TAG, "回调执行失败: " + entry.getKey(), e);
|
|
|
|
|
|
|
+ if (callback != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ callback.onDroneConnectionChanged(isConnected);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Log.e(TAG, "回调 onDroneConnectionChanged 执行失败: " + tag, e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return false;
|
|
|
|
|
});
|
|
});
|
|
|
|
|
+ // 清理失效的弱引用
|
|
|
|
|
+ callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 通知遥控器连接状态变化
|
|
* 通知遥控器连接状态变化
|
|
|
*/
|
|
*/
|
|
|
private void notifyRemoteControllerConnectionChanged(boolean isConnected) {
|
|
private void notifyRemoteControllerConnectionChanged(boolean isConnected) {
|
|
|
- // 遍历所有回调,清理失效的弱引用
|
|
|
|
|
- callbacks.entrySet().removeIf(entry -> {
|
|
|
|
|
- WeakReference<ConnectionStateCallback> weakRef = entry.getValue();
|
|
|
|
|
|
|
+ callbacks.forEach((tag, weakRef) -> {
|
|
|
ConnectionStateCallback callback = weakRef.get();
|
|
ConnectionStateCallback callback = weakRef.get();
|
|
|
-
|
|
|
|
|
- if (callback == null) {
|
|
|
|
|
- // 回调已被垃圾回收,移除这个条目
|
|
|
|
|
- Log.d(TAG, "清理失效的回调: " + entry.getKey());
|
|
|
|
|
- return true;
|
|
|
|
|
|
|
+ if (callback != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ callback.onRemoteControllerConnectionChanged(isConnected);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Log.e(TAG, "回调 onRemoteControllerConnectionChanged 执行失败: " + tag, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ // 清理失效的弱引用
|
|
|
|
|
+ callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ADDED: 新增通知方法
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通知无人机起飞
|
|
|
|
|
+ */
|
|
|
|
|
+ private void notifyDroneTakeoff() {
|
|
|
|
|
+ Log.i(TAG, "通知无人机起飞");
|
|
|
|
|
+ callbacks.forEach((tag, weakRef) -> {
|
|
|
|
|
+ ConnectionStateCallback callback = weakRef.get();
|
|
|
|
|
+ if (callback != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ callback.onDroneTakeoff();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Log.e(TAG, "回调 onDroneTakeoff 执行失败: " + tag, e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- callback.onRemoteControllerConnectionChanged(isConnected);
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- Log.e(TAG, "回调执行失败: " + entry.getKey(), e);
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+ // 清理失效的弱引用
|
|
|
|
|
+ callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通知无人机降落
|
|
|
|
|
+ */
|
|
|
|
|
+ private void notifyDroneLanding() {
|
|
|
|
|
+ Log.i(TAG, "通知无人机降落");
|
|
|
|
|
+ callbacks.forEach((tag, weakRef) -> {
|
|
|
|
|
+ ConnectionStateCallback callback = weakRef.get();
|
|
|
|
|
+ if (callback != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ callback.onDroneLanding();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Log.e(TAG, "回调 onDroneLanding 执行失败: " + tag, e);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return false;
|
|
|
|
|
});
|
|
});
|
|
|
|
|
+ // 清理失效的弱引用
|
|
|
|
|
+ callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 手动刷新连接状态(用于 SDK 初始化后)
|
|
* 手动刷新连接状态(用于 SDK 初始化后)
|
|
|
*/
|
|
*/
|
|
@@ -254,52 +347,57 @@ public class DeviceConnectionManager {
|
|
|
Log.w(TAG, "SDK 未初始化,无法刷新连接状态");
|
|
Log.w(TAG, "SDK 未初始化,无法刷新连接状态");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
- // 获取当前连接状态
|
|
|
|
|
Boolean droneConnected = KeyManager.getInstance().getValue(create(FlightControllerKey.KeyConnection));
|
|
Boolean droneConnected = KeyManager.getInstance().getValue(create(FlightControllerKey.KeyConnection));
|
|
|
Boolean rcConnected = KeyManager.getInstance().getValue(create(RemoteControllerKey.KeyConnection));
|
|
Boolean rcConnected = KeyManager.getInstance().getValue(create(RemoteControllerKey.KeyConnection));
|
|
|
-
|
|
|
|
|
- // 更新状态
|
|
|
|
|
|
|
+ // ADDED: 刷新飞行状态
|
|
|
|
|
+ Boolean droneFlying = KeyManager.getInstance().getValue(create(FlightControllerKey.KeyIsFlying));
|
|
|
|
|
+
|
|
|
if (droneConnected != null) {
|
|
if (droneConnected != null) {
|
|
|
isDroneConnected.set(droneConnected);
|
|
isDroneConnected.set(droneConnected);
|
|
|
notifyDroneConnectionChanged(droneConnected);
|
|
notifyDroneConnectionChanged(droneConnected);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if (rcConnected != null) {
|
|
if (rcConnected != null) {
|
|
|
isRemoteControllerConnected.set(rcConnected);
|
|
isRemoteControllerConnected.set(rcConnected);
|
|
|
notifyRemoteControllerConnectionChanged(rcConnected);
|
|
notifyRemoteControllerConnectionChanged(rcConnected);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- Log.i(TAG, "连接状态已刷新 - 无人机: " + isDroneConnected.get() + ", 遥控器: " + isRemoteControllerConnected.get());
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // ADDED: 更新飞行状态
|
|
|
|
|
+ if (droneFlying != null) {
|
|
|
|
|
+ isDroneFlying.set(droneFlying);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log.i(TAG, "连接状态已刷新 - 无人机: " + isDroneConnected.get() + ", 遥控器: " + isRemoteControllerConnected.get() + ", 飞行中: " + isDroneFlying.get());
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
Log.e(TAG, "刷新连接状态失败", e);
|
|
Log.e(TAG, "刷新连接状态失败", e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取有效回调数量(用于调试)
|
|
* 获取有效回调数量(用于调试)
|
|
|
*/
|
|
*/
|
|
|
public int getActiveCallbackCount() {
|
|
public int getActiveCallbackCount() {
|
|
|
- // 清理失效的回调并返回数量
|
|
|
|
|
callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
callbacks.entrySet().removeIf(entry -> entry.getValue().get() == null);
|
|
|
return callbacks.size();
|
|
return callbacks.size();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 销毁管理器(通常在应用退出时调用)
|
|
* 销毁管理器(通常在应用退出时调用)
|
|
|
*/
|
|
*/
|
|
|
public void destroy() {
|
|
public void destroy() {
|
|
|
Log.i(TAG, "销毁 DeviceConnectionManager");
|
|
Log.i(TAG, "销毁 DeviceConnectionManager");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
stopListening();
|
|
stopListening();
|
|
|
callbacks.clear();
|
|
callbacks.clear();
|
|
|
-
|
|
|
|
|
- // 重置状态
|
|
|
|
|
|
|
+
|
|
|
isDroneConnected.set(false);
|
|
isDroneConnected.set(false);
|
|
|
isRemoteControllerConnected.set(false);
|
|
isRemoteControllerConnected.set(false);
|
|
|
-
|
|
|
|
|
|
|
+ // ADDED: 重置飞行状态
|
|
|
|
|
+ isDroneFlying.set(false);
|
|
|
|
|
+
|
|
|
instance = null;
|
|
instance = null;
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|