簡體   English   中英

DJI無人機未連接(未調用sdkManagerProductDidChange委托方法)

[英]DJI drone not connecting (sdkManagerProductDidChange delegate method not called)

我似乎無法獲得自己的應用程序以連接到特定的無人機。 我已經下載了bridger應用程序,並正在使用它進行調試。 我從示例應用程序復制了“ DJIBaseViewController”,並將自己的視圖控制器作為它的委托。 在代碼中添加了很多斷點后,我發現我的應用程序與示例應用程序之間的主要區別在於委托方法“ sdkManagerProductDidChange”。

//  DJIBaseViewController.swift


import UIKit
import DJISDK

protocol DJIProductObjectProtocol {
    func fetchAircraft() -> DJIAircraft?
    func fetchCamera() -> DJICamera?
    func fetchGimbal() -> DJIGimbal?
    func fetchFlightController() -> DJIFlightController?
    func fetchRemoteController() -> DJIRemoteController?
    func fetchBattery() -> DJIBattery?
    func fetchAirLink() -> DJIAirLink?
    func fetchHandheldController() -> DJIHandheldController?
}

class ConnectedProductManager: DJIProductObjectProtocol {
    static let sharedInstance = ConnectedProductManager()

    var connectedProduct:DJIBaseProduct? = nil

    func fetchAircraft() -> DJIAircraft? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft)
        }
        return nil
    }

    func fetchCamera() -> DJICamera? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).camera
        }
        else if (self.connectedProduct is DJIHandheld) {
            return (self.connectedProduct as! DJIHandheld).camera
        }

        return nil
    }

    func fetchGimbal() -> DJIGimbal? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).gimbal
        }
        else if (self.connectedProduct is DJIHandheld) {
            return (self.connectedProduct as! DJIHandheld).gimbal
        }

        return nil
    }

    func fetchFlightController() -> DJIFlightController? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).flightController
        }
        return nil
    }

    func fetchRemoteController() -> DJIRemoteController? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).remoteController
        }
        return nil
    }

    func fetchBattery() -> DJIBattery? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).battery
        }
        else if (self.connectedProduct is DJIHandheld) {
            return (self.connectedProduct as! DJIHandheld).battery
        }

        return nil
    }

    func fetchAirLink() -> DJIAirLink? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIAircraft) {
            return (self.connectedProduct as! DJIAircraft).airLink
        }
        else if (self.connectedProduct is DJIHandheld) {
            return (self.connectedProduct as! DJIHandheld).airLink
        }

        return nil
    }

    func fetchHandheldController() -> DJIHandheldController? {
        if (self.connectedProduct == nil) {
            return nil
        }
        if (self.connectedProduct is DJIHandheld) {
            return (self.connectedProduct as! DJIHandheld).handheldController
        }
        return nil
    }

    func setDelegate(delegate:DJIBaseProductDelegate?) {
        self.connectedProduct?.delegate = delegate
    }

}

class DJIBaseViewController: UIViewController, DJIBaseProductDelegate, DJIProductObjectProtocol {

    //var connectedProduct:DJIBaseProduct?=nil
    var moduleTitle:String?=nil

    override func viewDidLoad() {
        super.viewDidLoad()
        if (moduleTitle != nil) {
            self.title = moduleTitle
        }

        // Do any additional setup after loading the view.
    }


    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if (ConnectedProductManager.sharedInstance.connectedProduct != nil) {
            ConnectedProductManager.sharedInstance.setDelegate(self)
        }
    }

    override func viewWillDisappear(
        animated: Bool) {
        super.viewWillDisappear(animated)
        if (ConnectedProductManager.sharedInstance.connectedProduct != nil &&
            ConnectedProductManager.sharedInstance.connectedProduct?.delegate === self) {
            ConnectedProductManager.sharedInstance.setDelegate(nil)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func product(product: DJIBaseProduct, connectivityChanged isConnected: Bool) {
        if isConnected {
            NSLog("\(product.model) connected. ")
            ConnectedProductManager.sharedInstance.connectedProduct = product
            ConnectedProductManager.sharedInstance.setDelegate(self)

        }
        else {
            NSLog("Product disconnected. ")
            ConnectedProductManager.sharedInstance.connectedProduct = nil
        }
    }

    func componentWithKey(withKey key: String, changedFrom oldComponent: DJIBaseComponent?, to newComponent: DJIBaseComponent?) {
        //     (newComponent as? DJICamera)?.delegate = self
        if ((newComponent is DJICamera) == true && (self is DJICameraDelegate) == true) {
            (newComponent as! DJICamera).delegate = self as? DJICameraDelegate

        }
        if ((newComponent is DJICamera) == true && (self is DJIPlaybackDelegate) == true) {
            (newComponent as! DJICamera).playbackManager?.delegate = self as? DJIPlaybackDelegate
        }

        if ((newComponent is DJIFlightController) == true && (self is DJIFlightControllerDelegate) == true) {
            (newComponent as! DJIFlightController).delegate = self as? DJIFlightControllerDelegate
        }

        if ((newComponent is DJIBattery) == true && (self is DJIBatteryDelegate) == true) {
            (newComponent as! DJIBattery).delegate = self as? DJIBatteryDelegate
        }

        if ((newComponent is DJIGimbal) == true && (self is DJIGimbalDelegate) == true) {
            (newComponent as! DJIGimbal).delegate = self as? DJIGimbalDelegate
        }

        if ((newComponent is DJIRemoteController) == true && (self is DJIRemoteControllerDelegate) == true) {
            (newComponent as! DJIRemoteController).delegate = self as? DJIRemoteControllerDelegate
        }

    }


    func showAlertResult(info:String) {
        // create the alert
        var message:String? = info

        if info.hasSuffix(":nil") {
            message = info.stringByReplacingOccurrencesOfString(":nil", withString: " success")
        }

        let alert = UIAlertController(title: "Message", message: "\(message ?? "")", preferredStyle: .Alert)
        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        // show the alert
        self.presentViewController(alert, animated: true, completion: nil)
    }


    func fetchAircraft() -> DJIAircraft?{
        return ConnectedProductManager.sharedInstance.fetchAircraft()
    }

    func fetchCamera() -> DJICamera? {
        return ConnectedProductManager.sharedInstance.fetchCamera()
    }

    func fetchGimbal() -> DJIGimbal? {
        return ConnectedProductManager.sharedInstance.fetchGimbal()
    }

    func fetchFlightController() -> DJIFlightController? {
        return ConnectedProductManager.sharedInstance.fetchFlightController()
    }

    func fetchRemoteController() -> DJIRemoteController? {
        return ConnectedProductManager.sharedInstance.fetchRemoteController()
    }

    func fetchBattery() -> DJIBattery? {
        return ConnectedProductManager.sharedInstance.fetchBattery()
    }
    func fetchAirLink() -> DJIAirLink? {
        return ConnectedProductManager.sharedInstance.fetchAirLink()
    }
    func fetchHandheldController() -> DJIHandheldController?{
        return ConnectedProductManager.sharedInstance.fetchHandheldController()
    }
}

啟動屏幕加載后加載的第一個視圖是。

//  MenuViewController.swift
import UIKit
import DJISDK

let enterDebugMode=true

class MenuViewController: DJIBaseViewController {

    @IBOutlet weak var aircraft: UILabel!
    @IBOutlet weak var productID: UILabel!
              // Do any additional setup after loading the view.
    @IBOutlet weak var appConectivity: UILabel!


        var connectedProduct:DJIBaseProduct?=nil
        var componentDictionary = Dictionary<String, Array<DJIBaseComponent>>()

        let APP_KEY = "*******"//Please enter App Key Here
        override func viewDidLoad() {

            super.viewDidLoad()
            let air = self.fetchAircraft()

            if air == nil{
                aircraft.text?="no aircraft connected"
            }
            print(air?.model)
            initUI();

            guard !APP_KEY.isEmpty else {
                showAlert("Please enter your app key.")
                return
            }
            DJISDKManager.registerApp(APP_KEY, withDelegate: self)


            if DJISDKManager.product() == nil{
                productID.text?="Drone Not Connected"
            }
            else{
                productID.text? = "Drone Connected"
            }
        }

        func initUI() {
            self.title = "DJI iOS SDK Sample"
            //sdkVersionLabel.text = "DJI SDK Version: \(DJISDKManager.getSDKVersion())"
            //openComponents.isEnabled = false;
            //bluetoothConnectorButton.isEnabled = true;
            //productModel.isHidden = true
            //productFirmwarePackageVersion.isHidden = true
            //debugModeLabel.isHidden = !enterDebugMode
        }
    func showAlert(msg: String?) {
        // create the alert
        let alert = UIAlertController(title: "", message: msg, preferredStyle: .Alert)
        // add the actions (buttons)
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        // show the alert
        self.presentViewController(alert, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
}
extension MenuViewController: DJISDKManagerDelegate{

        func sdkManagerDidRegisterAppWithError(error: NSError?) {

            guard error == nil  else {
                self.showAlertResult("Error:\(error!.localizedDescription)")
                appConectivity.text?="app isn't registering properly"
                return
            }

            //Debug("Registered!")

            if enterDebugMode {

                DJISDKManager.enterDebugModeWithDebugId("10.202.38.238")
                print("WTF")
            }else{
                //DJISDKManager.enterDebugModeWithDebugId("10.202.38.238")
                DJISDKManager.startConnectionToProduct()
            }

        }

        func sdkManagerProductDidChange(From oldProduct: DJIBaseProduct?, To newProduct: DJIBaseProduct?) {

            print("entered changed product")
            if oldProduct==nil{
                print("old product is nill")
            }
            if newProduct==nil{
                print("new product is nill")
            }

            guard let newProduct = newProduct else
            {
                appConectivity.text? = "Status: No Product Connected"

                ConnectedProductManager.sharedInstance.connectedProduct = nil
                //logDebug("Product Disconnected")
                return
            }

            //Updates the product's model
            productID.text = "Model: \((newProduct.model)!)"
            productID.hidden = false

            if let oldProduct = oldProduct {
                print("Product changed from: \(oldProduct.model) to \((newProduct.model)!)")
            }
            //Updates the product's firmware version - COMING SOON

            //Updates the product's connection status
            //appConectivity.text = "Status: Product Connected"

            ConnectedProductManager.sharedInstance.connectedProduct = newProduct
            productID.text?="product connected"
            //openComponents.isEnabled = true;
            //openComponents.alpha = 1.0;
            //logDebug("Product Connected")

        }
    override func product(product: DJIBaseProduct, connectivityChanged isConnected: Bool) {

            if isConnected {
                print("Status: Product Connected")
                //appConectivity.text?="Drone Recognized"
            } else {
                print("Status: No Product Connected")
                //appConectivity.text="Atleast Its trying"
            }
        }


    }

sdkManager正在使用給定的應用程序密鑰和捆綁程序標識符正確注冊。 我還在我的info.plist文件中添加了“受支持的外部附件協議”,其中包含三個元素com.dji.video,com.dji.protocol和com.dji.common。

被困在這里已經有一段時間了,真是令人沮喪。 希望有人打電話幫助。

提前致謝。

我想到了。 這里的問題是示例應用程序已為其委托命名

func sdkManagerProductDidChange(from oldProduct: DJIBaseProduct?, to newProduct: DJIBaseProduct?)

而無論出於何種原因,DJISDK都知道我的委托函數是

func sdkManagerProductDidChangeFrom(oldProduct: DJIBaseProduct?, to newProduct: DJIBaseProduct?)

有點煩人,這帶來了很大的不同,但這就是我猜到的復制代碼所得到的。 希望這對以后的人有幫助...

干杯

PS,當我在擴展部分的Xcode中鍵入func時,他們可能會再次更改它,方法是吐出我可以使用的功能列表,而sdkManagerDidChange是其中一個具有不同輸入變量的函數。

編輯:如果有人可以解釋為什么它可以在示例應用程序上運行,而不是我認為那也很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM