dji-sdk / Mobile-SDK-Android

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

DJISDKManager.startconnToProd.. doesn't work properly #1209

Closed lbujnak closed 1 year ago

lbujnak commented 1 year ago

Hi, I'm too frustrated because I build my app whole through bridge mode and now I found out, I can't just connect it through USB.

I've been finding solution for like 5 hours and no progress. Every demo I open works fine, so I don't know what is wrong with it ...

Any idea what might be wrong? thanks for all responses.

Here is my productCommService.swift

import UIKit
import SwiftUI
import DJISDK

class ProductCommunicationService: NSObject {

    private var globalData : GlobalData
    var libController : LibraryCommunicationService

    init(globalData: GlobalData) {
        self.globalData = globalData
        self.libController = LibraryCommunicationService(globalData: globalData)
    }

    func registerWithSDK() {
        let appKey = Bundle.main.object(forInfoDictionaryKey: SDK_APP_KEY_INFO_PLIST_KEY) as? String

        guard appKey != nil && appKey!.isEmpty == false else {
            createAlert(globalData: self.globalData, title: "AppKey error", msg: "Please enter your app key in the info.plist")
            return
        }
        DJISDKManager.registerApp(with: self)
    }

    func connectToProduct(){
        DJISDKManager.stopConnectionToProduct()
        if (self.globalData.enableBridgeMode) {
            DJISDKManager.enableBridgeMode(withBridgeAppIP: self.globalData.bridgeAppIP)
            print("Bridge connection to " + self.globalData.bridgeAppIP + " has been started.")
        } else {
            self.stopBridgeMode()
            if (DJISDKManager.startConnectionToProduct()) {
                print("Connection has been started.")
            } else {
                createAlert(globalData: self.globalData, title: "Connection error", msg: "There was a problem starting the connection.")
            }
        }
    }

    func stopBridgeMode(){
        self.globalData.connected = false
        self.globalData.enableBridgeMode = false
        DJISDKManager.disableBridgeMode()
        print("Bridge connection to " + self.globalData.bridgeAppIP + " has been disabled.")
    }

}

extension ProductCommunicationService : DJISDKManagerDelegate {

    func didUpdateDatabaseDownloadProgress(_ progress: Progress) {
        print("SDK downloading db file \(progress.completedUnitCount / progress.totalUnitCount)")
    }

    func appRegisteredWithError(_ error: Error?) {
        if (error != nil) {
            createAlert(globalData: self.globalData, title: "SDK Registered with error", msg: error?.localizedDescription ?? "")
            return
        }

        self.globalData.registered = true
        //self.connectToProduct()
        DJISDKManager.startConnectionToProduct()
        self.globalData.connected = true
    }

    func productConnected(_ product: DJIBaseProduct?) {
        print("Entered productConnected")
        createAlert(globalData: self.globalData, title: "Test", msg: "in")
        guard let _ = product else {
            print("Product connected but was nil")
            createAlert(globalData: self.globalData, title: "Connection error", msg: "There was a problem connectiong to device.")
            return
        }
        self.globalData.connected = true
        createAlert(globalData: self.globalData, title: "Test", msg: "Connected")
    }

    func productDisconnected() {
        print("Entered productDisconnected")
        self.globalData.connected = false
        self.globalData.mediaFetched = false
        self.globalData.mediaPreview = nil
        self.globalData.mediaList = []

        if(self.globalData.libMode) { self.globalData.libMode = false }
        if(self.globalData.fpvMode) { self.globalData.fpvMode = false }
    }

    func componentConnected(withKey key: String?, andIndex index: Int) {
        print("Entered componentConnected")
        if(!self.globalData.connected && DJISDKManager.product() != nil && DJISDKManager.product()!.model != "Only RemoteController"){
            //self.productConnected(DJISDKManager.product())
        }
    }

    func componentDisconnected(withKey key: String?, andIndex index: Int) {
        print("Entered componentDisonnected")
        if(self.globalData.connected && (DJISDKManager.product() == nil || DJISDKManager.product()!.model == "Only RemoteController")){
            self.productDisconnected()
        }
    }
}
DJI-William commented 1 year ago

This is an Android forum