dji-sdk / Mobile-SDK-iOS

DJI Mobile SDK for iOS: http://developer.dji.com/mobile-sdk/
Other
581 stars 256 forks source link

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

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

Do you mean you are running an iOS MSDK on a PC simulator, and you want to use bridge app to communicate with an iOS device? The iOS device must connect with RC via an USB cable. For the bridge app, you also need to change the IP address to yours.

lbujnak commented 1 year ago

So it was magic air through USB and I figured out, that I forget to add to external access protocols in pinfo dji things.