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

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

基于android studio 3.5

1、引入DJI mobile SDK 4.8和DJI UX SDK 4.8

部分build.gradle如下

apply plugin: 'com.android.application'android {compileSdkVersion 27buildToolsVersion "27.0.3"defaultConfig {multiDexEnabled truevectorDrawables.useSupportLibrary = truendk {abiFilters 'armeabi-v7a', 'x86', 'arm64-v8a'}}dexOptions {preDexLibraries = falsejavaMaxHeapSize "4g"}packagingOptions {doNotStrip "*/*/libdjivideo.so"doNotStrip "*/*/libSDKRelativeJNI.so"doNotStrip "*/*/libFlyForbid.so"doNotStrip "*/*/libduml_vision_bokeh.so"doNotStrip "*/*/libyuv2.so"doNotStrip "*/*/libGroudStation.so"doNotStrip "*/*/libFRCorkscrew.so"doNotStrip "*/*/libUpgradeVerify.so"doNotStrip "*/*/libFR.so"pickFirst 'lib/*/libstlport_shared.so'pickFirst 'lib/*/libRoadLineRebuildAPI.so'pickFirst 'lib/*/libGNaviUtils.so'pickFirst 'lib/*/libGNaviMapex.so'pickFirst 'lib/*/libGNaviData.so'pickFirst 'lib/*/libGNaviMap.so'pickFirst 'lib/*/libGNaviSearch.so'exclude 'META-INF/rxjava.properties'}
}
dependencies {//DJI UI库compile 'com.dji:dji-uxsdk:4.8'//DJI SDKprovided 'com.dji:dji-sdk-provided:4.8'
}

2、AndroidManifest配置

 <!-- DJI SDK need permission --><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.WAKE_LOCK" /><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.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-feature android:name="android.hardware.camera" /><uses-feature android:name="android.hardware.camera.autofocus" /><uses-featureandroid:name="android.hardware.usb.host"android:required="false" /><uses-featureandroid:name="android.hardware.usb.accessory"android:required="true" /><!-- SDK requirement permission end -->
 <applicationandroid:name=".YourApplication"android:allowBackup="true"android:icon="@mipmap/applogo"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"tools:replace="android:icon,android:theme"><!-- DJI SDK --><uses-library android:name="com.android.future.usb.accessory" /><meta-dataandroid:name="com.dji.sdk.API_KEY"android:value="Your Key" /> <!-- KEY去大疆开发者网页去申请android mobile SDK --><!-- DJI SDK --><activityandroid:name=".UI.StartActivity"android:configChanges="orientation|keyboardHidden|screenSize"android:label="@string/app_name"android:launchMode="standard"android:theme="@style/WelcomeTheme"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" /></intent-filter><meta-dataandroid:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"android:resource="@xml/accessory_filter" /></activity>  </application>

3、注册帮助类,从4.4之后

在Applicatiom中加入如下代码:

 @Overrideprotected void attachBaseContext(Context base) {super.attachBaseContext(base);MultiDex.install(this);Helper.install(YourApplication.this);//引入大疆帮助类}

4、MainActivity连接飞机

FlightController mFlightController = null;
FlightController.OnboardSDKDeviceDataCallback receiveCallback;private AtomicBoolean isRegistrationInProgress = new AtomicBoolean(false);private DJISDKManager.SDKManagerCallback registrationCallback = new DJISDKManager.SDKManagerCallback() {@Overridepublic void onRegister(DJIError error) {isRegistrationInProgress.set(false);if (error == DJISDKError.REGISTRATION_SUCCESS) {DJISDKManager.getInstance().setCallbackRunInUIThread(true);DJISDKManager.getInstance().startConnectionToProduct();//Toast.makeText(getApplicationContext(), "SDK registration succeeded!", Toast.LENGTH_LONG).show();myHandler.sendEmptyMessage(3);//检查登陆} else {Toast.makeText(getApplicationContext(),//"SDK registration failed, check network and retry!","DJI SDK 注册失败, 请检查网络后重试!",Toast.LENGTH_LONG).show();}}@Overridepublic void onProductChange(BaseProduct baseProduct, BaseProduct baseProduct1) {Log.v(TAG, String.format("onProductChanged oldProduct:%s, newProduct:%s", baseProduct, baseProduct1));mProduct = baseProduct1;if (mProduct != null) {mProduct.setBaseProductListener(mDJIBaseProductListener);}notifyStatusChange();}private BaseProduct.BaseProductListener mDJIBaseProductListener = new BaseProduct.BaseProductListener() {@Overridepublic void onComponentChange(BaseProduct.ComponentKey key, BaseComponent oldComponent, BaseComponent newComponent) {if (newComponent != null) {newComponent.setComponentListener(mDJIComponentListener);}Log.v(TAG, String.format("onComponentChange key:%s, oldComponent:%s, newComponent:%s", key, oldComponent, newComponent));notifyStatusChange();}@Overridepublic void onConnectivityChange(boolean b) {Log.v(TAG, "onProductConnectivityChanged: " + b);notifyStatusChange();}};private BaseComponent.ComponentListener mDJIComponentListener = new BaseComponent.ComponentListener() {@Overridepublic void onConnectivityChange(boolean b) {notifyStatusChange();}};private void notifyStatusChange() {myHandler.sendEmptyMessage(2);}};private static final String[] REQUIRED_PERMISSION_LIST = new String[]{Manifest.permission.VIBRATE,Manifest.permission.INTERNET,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.WAKE_LOCK,Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CHANGE_WIFI_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.READ_PHONE_STATE,};private static final int REQUEST_PERMISSION_CODE = 12345;private List<String> missingPermission = new ArrayList<>();/*** 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);}}/*** Result of runtime permission request*/@Overridepublic 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();}}private void startSDKRegistration() {if (isRegistrationInProgress.compareAndSet(false, true)) {AsyncTask.execute(new Runnable() {@Overridepublic void run() {DJISDKManager.getInstance().registerApp(MainActivity.this, registrationCallback);}});}}@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);checkAndRequestPermissions();
}  private void refreshSDKRelativeUI() {//此方法sendEmptyMessege 2的调用mProduct = SearchApplication.getProductInstance();if (null != mProduct && mProduct.isConnected()) {        if (mFlightController == null) {Aircraft mAircraft1 = (Aircraft) DJISDKManager.getInstance().getProduct();if (mAircraft1 != null)mFlightController = mAircraft1.getFlightController();//获取飞控类}if (mFlightController != null) {if (mFlightController.isOnboardSDKDeviceAvailable() && bFirst && actimer == null)//检查板载SDK}imgproduct.setImageResource(R.mipmap.v2_m600_pro_connect_icon);String str = mProduct instanceof Aircraft ? "DJIAircraft" : "DJIHandHeld";mTextConnectionStatus.setText("状态: " + str + " 连接成功");if (null != mProduct.getModel()) {mTextProduct.setText("" + mProduct.getModel().getDisplayName());} else {mTextProduct.setText(R.string.product_information);}if (receiveCallback == null) {receiveCallback = new FlightController.OnboardSDKDeviceDataCallback() {//板载SDK回复的数据,如果要用到的话@Overridepublic void onReceive(byte[] bytes) {}};mFlightController.setOnboardSDKDeviceDataCallback(receiveCallback);}}
}

 

5、高版本SDK必须要登录帐号,如果版本过低或者未登录帐号,飞机则会限高限远,下面是检查登录和登录操作

private void loginAccount() {//检查登录UserAccountState userAccountState = UserAccountManager.getInstance().getUserAccountState();//如果用户 NOT_LOGGED_IN TOKEN_OUT_OF_DATE INVALID_TOKEN UNKNOWN 则弹出对话框重新登陆if (userAccountState == UserAccountState.NOT_LOGGED_IN ||userAccountState == UserAccountState.TOKEN_OUT_OF_DATE ||userAccountState == UserAccountState.INVALID_TOKEN ||userAccountState == UserAccountState.UNKNOWN) {login();} else {//否则显示用户名UserAccountManager.getInstance().getLoggedInDJIUserAccountName(new CommonCallbacks.CompletionCallbackWith<String>() {@Overridepublic void onSuccess(String s) {csUser = s;myHandler.sendEmptyMessage(0);}@Overridepublic void onFailure(DJIError djiError) {login();}});}}private void login() {if (UserAccountManager.getInstance() == null)return;UserAccountManager.getInstance().logIntoDJIUserAccount(MainActivity.this,new CommonCallbacks.CompletionCallbackWith<UserAccountState>() {@Overridepublic void onSuccess(final UserAccountState userAccountState) {if (userAccountState == UserAccountState.NOT_AUTHORIZED ||userAccountState == UserAccountState.AUTHORIZED) {Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();UserAccountManager.getInstance().getLoggedInDJIUserAccountName(new CommonCallbacks.CompletionCallbackWith<String>() {@Overridepublic void onSuccess(String s) {csUser = s;myHandler.sendEmptyMessage(0);}@Overridepublic void onFailure(DJIError djiError) {}});}}@Overridepublic void onFailure(DJIError error) {Toast.makeText(MainActivity.this, "登陆错误:" + error.getDescription(), Toast.LENGTH_LONG).show();}});}

6、可以FlightController 获取飞控信息,或者直接用UI库里面的控件会自动刷新飞行器信息


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

相关文章

【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】弹出运…

python联网斗地主_局域网联机斗地主

局域网联机斗地主 一、介绍 大一Python期末大作业&#xff1a;《局域网联机斗地主》 二、软件思维导图 三、信息传递流程 四、软件模块架构 4.1、获取/get 4.2、随机发牌 4.3、牌型转换 4.4、规则 4.5、判断 4.6、发送消息 使用说明 1. 保证所有玩家(3位)都连接至同一局域网下 …