MFiAdapter

Connection

Yuneec MFiAdapter is the framework to connect to use the Dronecode SDK with the ST10C Remote Controller via MFi. Complete usage of this framework can be seen in the example app.

To start, monitor the MFi Connection state using the code snippet below. Use the code snippet below in your application to receive connection updates between the remote controller and the mobile device. Add an observer for the notification "MFiConnectionStateNotification" to get updates for the MFi connection.


NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleConnectionStateNotification(notification:)),
            name: Notification.Name("MFiConnectionStateNotification"),
            object: nil) 

 

Define a method "handleConnectionStateNotification" to process the notification.


@objc func handleConnectionStateNotification(notification: NSNotification) {
        // Get userInfo dictionary using - String(describing:notification.userInfo)
    }

MFiRemoteControllerAdapter

The MFiRemoteControllerAdapter class in MFiAdapter provides methods to manage binding and unbinding from Wifi and RC. To bind to the camera Wifi from the RC, use the code snippet below.  First, we need to scan for the list of SSIDs in range, choose the desired Wifi ID, then send the bind command.


  MFiAdapter.MFiRemoteControllerAdapter.sharedInstance().scanCameraWifi { (error, wifis) in
            
            if let error = error {
                // Error scanning wifi
            } else {
                // Get wifi list from wifis
            }
        }

  MFiAdapter.MFiRemoteControllerAdapter.sharedInstance().bindCameraWifi(wifiSelected, wifiPassword: wifiPassword) { (error) in
            if let error = error {
                // Error binding
            } else {
                // Pairing Successful 
            }
        }

To bind the RC with the vehicle, use the code snippet below. We need to send three commands to complete RC binding. First, the vehicle must be inverted (flipped over with the top facing down) to enable binding mode (blinking yellow LEDs). Once the vehicle enters binding mode, we need to invoke scanRC, bindRC, and exitBind respectively.


     MFiAdapter.MFiRemoteControllerAdapter.sharedInstance().scanAutoPilot { (error, ids) in
            
            if let error = error {
                   // Error scanning RC
            } else if let ids = ids {
                for id in ids {
                    // Get id's of the scanned vehicles
                }
            }
        }

MFiAdapter.MFiRemoteControllerAdapter.sharedInstance().bindAutoPilot(self.autoPilotId) { (error) in
            if let error = error {
               // Error binding
            } else {
                // Binding Successful
            }
        }

This is the last step to complete the binding process.


  MFiAdapter.MFiRemoteControllerAdapter.sharedInstance().exitBind { (error) in
            if let error = error {
                // Error exiting bind
            } else {
                // Exit Bind Successful
            }
        }

MFiCameraAdapter

MFiCameraAdapter provides functions required to request and download media from the camera. It also provides methods to get firmware versions of camera and gimbal.

Below code snippet shows how to request media info using this class.


 MFiAdapter.MFiCameraAdapter.sharedInstance().requestMediaInfo { (array, error) in
            DispatchQueue.main.async {
                if (error == nil) {
                    // Get the array of media in the camera
                } else {
                   // Error fetching the media array
                }
            }
        }

Below code snippet demonstrates how to format camera storage.


MFiAdapter.MFiCameraAdapter.sharedInstance().formatCameraStorage { (error)
            in
            if (error != nil) {
                // Error in formatting storage
            }
            else {
                // Format storage successful
            }
        }

MFiOTAAdapter

MFiOTAAdapter provides all the required methods to perform OTA updates of the firmwares. The firmwares supported by this interface are Remote Controller, Camera, Auto Pilot and Gimbal.

To successfully update a firmware, you have to first download the latest firmware file from the server and then upload it to the vehicle.

The code snippet below demonstrates how to download OTA package.


MFiAdapter.MFiOtaAdapter.sharedInstance().downloadOtaPackage(.autopilot, filePath: autopilotFilePath, progressBlock: {(progress) in
                DispatchQueue.main.async {
                    // Get firmware file download progress 
                }
            }, completionBlock: { (error) in
                DispatchQueue.main.async {
                    if (error == nil) {
                        // Firmware file download successful
                    } else {
                        // Error downloading the firmware
                    }
                }
            })

 

Check out the complete working example to perform OTA updates here!