大疆无人机DJI Mobile SDK入门(一)

news/2024/11/17 20:35:27/

目录:

大疆无人机DJI Mobile SDK(Android)入门(一)

大道无疆,创新无限!

将您的想法变为现实!

未来,无所不能!

小Z码农有幸能接触到这么高逼格的玩具。就将这段爱恨往事作为笔记记录下来......哈哈哈哈!下面开始开车了。。请系好安全带!

准备工作:

1、支持Mobile SDK 的DJI 无人机产品一架(满满的土豪感);

2、DJI Assistant 2 测试PC端模拟器下载安装;

3、android5.0手机一个(Mobile SDK 只支持5.0以上,测试手机型号HUAWEI M5 (版本android 9)和Honor 10 (版本android 10));

可根据DJI网站查看对应SDK 支持产品型号:https://developer.dji.com/products/#!/mobile

DJI Assistant 2下载地址:https://www.dji.com/cn/downloads?site=brandsite&from=footer

 

DJI Mobile SDK 文档查看地址:https://developer.dji.com/mobile-sdk/

和其它第三方SDK库一样,

1、注册DJI账号

2、创建Mobile SDK 应用

3、AndroidManifest.xml 填写DJI的应用App key

4、app的build.gradle 中依赖DJI库

创建DJI应用得到App key

 

依赖库如下(最新relase):

implementation('com.dji:dji-sdk:4.11.1', {exclude module: 'library-anti-distortion'exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.11.1

一、注册SDK使用

1)registerApp必须先成功使用SDK注册,然后才能将DJI产品与SDK一起使用。

文档中registerApp属于DJISDKManager下的功能,所以调用如下注册方法

 

2)权限申请:

<!-- SDK permission requirement -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-featureandroid:name="android.hardware.usb.host"android:required="false"/>
<uses-featureandroid:name="android.hardware.usb.accessory"android:required="true"/>

DJISampleApplication

public class DJISampleApplication extends Application {public static final String TAG = DJISampleApplication.class.getName();private static BaseProduct product;private static BluetoothProductConnector bluetoothConnector = null;private static Bus bus = new Bus(ThreadEnforcer.ANY);private static Application app = null;/*** Gets instance of the specific product connected after the* API KEY is successfully validated. Please make sure the* API_KEY has been added in the Manifest*/public static synchronized BaseProduct getProductInstance() {product = DJISDKManager.getInstance().getProduct();return product;}public static synchronized BluetoothProductConnector getBluetoothProductConnector() {bluetoothConnector = DJISDKManager.getInstance().getBluetoothProductConnector();return bluetoothConnector;}public static boolean isAircraftConnected() {return getProductInstance() != null && getProductInstance() instanceof Aircraft;}public static boolean isHandHeldConnected() {return getProductInstance() != null && getProductInstance() instanceof HandHeld;}public static synchronized Aircraft getAircraftInstance() {if (!isAircraftConnected()) {return null;}return (Aircraft) getProductInstance();}public static synchronized HandHeld getHandHeldInstance() {if (!isHandHeldConnected()) {return null;}return (HandHeld) getProductInstance();}public static Application getInstance() {return DJISampleApplication.app;}public static Bus getEventBus() {return bus;}@Overrideprotected void attachBaseContext(Context paramContext) {super.attachBaseContext(paramContext);MultiDex.install(this);com.secneo.sdk.Helper.install(this);app = this;}
}

MainActivity

private static final String[] REQUIRED_PERMISSION_LIST = new String[] {Manifest.permission.VIBRATE, // Gimbal rotationManifest.permission.INTERNET, // API requestsManifest.permission.ACCESS_WIFI_STATE, // WIFI connected productsManifest.permission.ACCESS_COARSE_LOCATION, // MapsManifest.permission.ACCESS_NETWORK_STATE, // WIFI connected productsManifest.permission.ACCESS_FINE_LOCATION, // MapsManifest.permission.CHANGE_WIFI_STATE, // Changing between WIFI and USB connectionManifest.permission.WRITE_EXTERNAL_STORAGE, // Log filesManifest.permission.BLUETOOTH, // Bluetooth connected productsManifest.permission.BLUETOOTH_ADMIN, // Bluetooth connected productsManifest.permission.READ_EXTERNAL_STORAGE, // Log filesManifest.permission.READ_PHONE_STATE, // Device UUID accessed upon registrationManifest.permission.RECORD_AUDIO // Speaker accessory
};
private static final int REQUEST_PERMISSION_CODE = 12345;//region Life-cycle
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);checkAndRequestPermissions();DJISampleApplication.getEventBus().register(this);setContentView(R.layout.activity_main);
}
/*** Result of runtime permission request*/
@Override
public void onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);// Check for granted permission and remove from missing listif (requestCode == REQUEST_PERMISSION_CODE) {for (int i = grantResults.length - 1; i >= 0; i--) {if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {missingPermission.remove(permissions[i]);}}}// If there is enough permission, we will start the registrationif (missingPermission.isEmpty()) {startSDKRegistration();} else {Toast.makeText(getApplicationContext(), "Missing permissions!!!", Toast.LENGTH_LONG).show();}
}/*** Checks if there is any missing permissions, and* requests runtime permission if needed.*/
private void checkAndRequestPermissions() {// Check for permissionsfor (String eachPermission : REQUIRED_PERMISSION_LIST) {if (ContextCompat.checkSelfPermission(this, eachPermission) != PackageManager.PERMISSION_GRANTED) {missingPermission.add(eachPermission);}}// Request for missing permissionsif (missingPermission.isEmpty()) {startSDKRegistration();} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {ActivityCompat.requestPermissions(this,missingPermission.toArray(new String[missingPermission.size()]),REQUEST_PERMISSION_CODE);}
}private void startSDKRegistration() {if (isRegistrationInProgress.compareAndSet(false, true)) {AsyncTask.execute(new Runnable() {@Overridepublic void run() {ToastUtils.setResultToToast(MainActivity.this.getString(R.string.sdk_registration_doing_message));DJISDKManager.getInstance().registerApp(MainActivity.this.getApplicationContext(), new DJISDKManager.SDKManagerCallback() {@Overridepublic void onRegister(DJIError djiError) {if (djiError == DJISDKError.REGISTRATION_SUCCESS) {DJILog.e("App registration", DJISDKError.REGISTRATION_SUCCESS.getDescription());DJISDKManager.getInstance().startConnectionToProduct();ToastUtils.setResultToToast(MainActivity.this.getString(R.string.sdk_registration_success_message));showDBVersion();} else {ToastUtils.setResultToToast(MainActivity.this.getString(R.string.sdk_registration_message) + djiError.getDescription());}Log.v(TAG, djiError.getDescription());}@Overridepublic void onProductDisconnect() {Log.d(TAG, "onProductDisconnect");//断开}@Overridepublic void onProductConnect(BaseProduct baseProduct) {Log.d(TAG, String.format("onProductConnect newProduct:%s", baseProduct));//连上}@Overridepublic void onComponentChange(BaseProduct.ComponentKey componentKey,BaseComponent oldComponent,BaseComponent newComponent) {if (newComponent != null) {newComponent.setComponentListener(mDJIComponentListener);}Log.d(TAG,String.format("onComponentChange key:%s, oldComponent:%s, newComponent:%s",componentKey,oldComponent,newComponent));notifyStatusChange();}@Overridepublic void onInitProcess(DJISDKInitEvent djisdkInitEvent, int i) {}@Overridepublic void onDatabaseDownloadProgress(long current, long total) {//首次注册使用联网下载基础数据}});}});}
}@Override
protected void onDestroy() {DJISampleApplication.getEventBus().unregister(this);super.onDestroy();
}

由于涉及某些制度隐私问题,所以就只晒下应用的一些截图结果,欢迎各位留言交流学习!!

 

 

 


http://www.ppmy.cn/news/438176.html

相关文章

大疆无人机二次开发简介-引入DJI mobile SDK 4.8

基于android studio 3.5 1、引入DJI mobile SDK 4.8和DJI UX SDK 4.8 部分build.gradle如下 apply plugin: com.android.applicationandroid {compileSdkVersion 27buildToolsVersion "27.0.3"defaultConfig {multiDexEnabled truevectorDrawables.useSupportLibr…

【MySQL数据库 | 第十九篇】SQL性能分析工具

目录 前言&#xff1a; SQL执行频率&#xff1a; 慢查询日志&#xff1a; profile&#xff1a; profile各个指令&#xff1a; 总结&#xff1a; 前言&#xff1a; 本篇我们将为大家讲解SQL性能的分析工具&#xff0c;而只有熟练的掌握了性能分析的工具&#xff0c;才可以更…

loadrunner联机

一、前提条件 保证自己电脑和联机的电脑网络相通&#xff0c;并且在同一网段&#xff0c;禁用电脑无线。 二、在场景中添加脚本和联机的电脑ip 三、在工具栏中测试是否能连接成功 四、对号说明成功&#xff0c;叉号需要联机的电脑右键以管理员身份启动小雷达&#xff0c; 关闭…

服务器处于联机状态,打印机状态未联机是怎么回事

大家好&#xff0c;我是智能客服时间君&#xff0c;上述问题将由我为大家进行解答。 打印机状态未联机的原因如下&#xff1a; 1、可能是打印机的电源线或者是相应的数据传输线没有接好。这种状况要确保电源接通&#xff0c;相应的数据传输线接好&#xff0c;就不会出现打印机未…

java联机交易系统_联机交易与批量交易

系统小事>>>联机交易与批量交易 什么是联机交易/批量交易&#xff1f;联机与批量并没有确切的定义&#xff0c;我个人的简单理解如下&#xff1a; 联机交易就意味着系统对外直接提供的交易&#xff0c;该类交易具有事务性——可回滚&#xff1b;实时性——交易有生命…

局域网联机_红警如何局域网联机?详细联机教程,方法特别简单

如果我们想和好友一起玩红警2&#xff0c;但不知道怎么局域网联机怎么办&#xff1f;现在的电脑系统都是win7甚至是win10的&#xff0c;所以想要玩红警2联机还是比较困难的。那么如何在红警2中联机对战呢&#xff1f;下面小编给大家分享一下。希望可以帮助到大家&#xff01; 首…

mcjava盗版联机_盗版我的世界怎么局域网联机

我的世界是一款有着高自由度的沙盘类游戏&#xff0c;但是一个人玩却总觉得有点孤独&#xff0c;如果可以和朋友们一起玩就好了。下面是小编收集整理的&#xff0c;希望对大家有帮助~~我的世界局域网联机的方法方法/步骤第一步我们需要做的是把自己的《我的世界》文件复制一份给…

ue4 android联机,UE4 局域网联机(LAN)

实现目标&#xff1a; 两台同属于一个局域网下的PC进行通讯。 1 同属于一个局域网下&#xff1a; 有时候会发现客户端搜索不到主机的现象一般是2台机器没在同一网段&#xff0c;光ping通不能算做可以进行联机的条件。 检测和修改方法&#xff1a; 1&#xff0c;【Win键R】弹出运…