2017-02-01 10 views
0

私は自分のアプリを特定の無人機に接続することはできません。私はブリッジ・アプリをダウンロードして、それを使ってデバッグしています。サンプルアプリケーションから "DJIBaseViewController"をコピーして、自分のView Controllerをその代理人にしました。多くのブレークポイントをコードに追加した後、私のアプリケーションとサンプルアプリケーションの主な違いは、デリゲートメソッド "sdkManagerProductDidChange"です。DJIドローンが接続されていません(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は、指定されたアプリキーとバンドラ識別子で正しく登録されています。私はまた、3つの要素、com.dji.video、com.dji.protocol、およびcom.dji.commonで、 "サポートされている外部アクセサリプロトコル"をinfo.plistファイルに追加しました。

かなり長い間ここにこだわってきたことがありました。誰かが助けを呼ぶことを望む。

ありがとうございます。

答えて

0

私はそれを理解しました。ここでの問題は、私のサンプルアプリケーションDJISDKで何らかの理由で、私の代表団が

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

それはこのような大きな作ったビット迷惑としての機能を知っているのに対し、サンプルアプリは

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

をそのデリゲートを指名したということですそれは私が推測するコードをコピーするために得られるものです。これは道路の下の誰かに役立つことを願っています...

乾杯

P.S.彼らは私が私のエクステンションセクションに関数をタイプしたときに私が見つけた方法でそれを再び変更するかもしれません。Xcodeは使用できる関数のリストを吐き出し、sdkManagerDidChangeは異なる入力変数を持つものでした。

編集:誰かがそれがサンプルアプリケーションで動作した理由を説明することができ、それは私も甘いだろう。

関連する問題