大疆无人机安卓Mobile Sdk开发(二)连接无人机,获取无人机信息
大疆无人机安卓Mobile Sdk开发(一)简单介绍大疆无人机安卓Mobile Sdk开发(二)连接无人机,获取无人机信息qq505057618我主要说核心部分,首先建议大家先下载一个官方的mobile sdk的Demo跑看一看,研究研究后再去尝试自己开发。开发的话你需要注册一个大疆开发者账号,然后注册一个基于mobilesdk的应用,官方会分配给你一个App Key然后在你A...
大疆无人机安卓Mobile Sdk开发(二)连接无人机,获取无人机信息
大疆无人机安卓Mobile Sdk开发(三)制定航点任务WaypointMission
大疆无人机安卓Mobile Sdk开发(四)读取飞机的图片和视频并下载
大疆无人机安卓Mobile Sdk开发(五)解决M300Rtk H20相机无法获取图片视频的问题
qq505057618
我主要说核心部分,首先建议大家先下载一个官方的mobile sdk的Demo跑看一看,研究研究后再去尝试自己开发。
开发的话你需要注册一个大疆开发者账号,然后注册一个基于mobilesdk的应用,官方会分配给你一个App Key
然后在你AndroidMnifest需要填入这个key,这个key是和你的包名绑定的,另外需要声明
<uses-feature android:name="android.hardware.usb.host" android:required="false"/>
<uses-feature android:name="android.hardware.usb.accessory" android:required="true"/>
还要注意你的主activity的<intent-filter>的写法,对USB_ACCESSORY_ATTACHED不了解的可以去google一下,主要是保证安卓与外设之间的通讯的,在这里,是保证你的无人机遥控器和安卓设置能连接上的。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dji.sdk.sample">
<!-- 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.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.RECORD_AUDIO"/>
<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-feature
android:name="android.hardware.usb.host"
android:required="false"/>
<uses-feature
android:name="android.hardware.usb.accessory"
android:required="true"/>
<!-- SDK requirement permission end -->
<application
android:name=".internal.controller.DJISampleApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/sample_app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
ADD API_KEY HERE and make sure you
are connected to the Internet before
the app is launched
-->
<meta-data
android:name="com.dji.sdk.API_KEY"
android:value="Please enter your App Key here."/>
<!-- Required for receiving accessory attachment events.
This is necessary for product when connecting over USB. -->
<activity
android:name="com.dji.sdk.sample.internal.controller.MainActivity"
android:label="@string/sample_app_name"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="@style/AppTheme">
<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-data
android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
android:resource="@xml/accessory_filter"/>
</activity>
</application>
</manifest>
然后是gradle文件,需要添加对大疆的依赖
dependencies {
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.squareup:otto:1.3.8'
implementation ('com.dji:dji-sdk:4.10', {
// Uncomment the following line if your app does not need Anti Distortion for
// Mavic 2 Pro and Mavic 2 Zoom. It will greatly reducing the size of the APK:
exclude module: 'library-anti-distortion'
// Uncomment the following line if your APP does not need network RTK
exclude module: 'library-networkrtk-helper'
})
compileOnly 'com.dji:dji-sdk-provided:4.10'
}
下面分析主activity代码的分析,只贴出核心代码。首先你要申请指定的权限,然后在权限允许后调用registerApp方法去注册应用,在收到注册成功的回调后,调用DJISDKManager.getInstance().startConnectionToProduct()去连接无人机,连接成功后会走到onProductConnect回调函数,这时候你可以获取你想要的无人机信息了。
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
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,
Manifest.permission.RECORD_AUDIO
};
private static final int REQUEST_PERMISSION_CODE = 12345;
private FrameLayout contentFrameLayout;
private Stack<ViewWrapper> stack;
private TextView titleTextView;
private List<String> missingPermission = new ArrayList<>();
private AtomicBoolean isRegistrationInProgress = new AtomicBoolean(false);
private BaseComponent.ComponentListener mDJIComponentListener = new BaseComponent.ComponentListener() {
@Override
public void onConnectivityChange(boolean isConnected) {
Log.d(TAG, "onComponentConnectivityChanged: " + isConnected);
notifyStatusChange();
}
};
//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 list
if (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 registration
if (missingPermission.isEmpty()) {
startSDKRegistration();
} else {
Toast.makeText(getApplicationContext(), "Missing permissions!!!", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
DJISampleApplication.getEventBus().unregister(this);
super.onDestroy();
}
@Override
protected void onNewIntent(@NonNull Intent intent) {
String action = intent.getAction();
if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
Intent attachedIntent = new Intent();
attachedIntent.setAction(DJISDKManager.USB_ACCESSORY_ATTACHED);
sendBroadcast(attachedIntent);
}
}
//region Registration n' Permissions Helpers
/**
* Checks if there is any missing permissions, and
* requests runtime permission if needed.
*/
private void checkAndRequestPermissions() {
// Check for permissions
for (String eachPermission : REQUIRED_PERMISSION_LIST) {
if (ContextCompat.checkSelfPermission(this, eachPermission) != PackageManager.PERMISSION_GRANTED) {
missingPermission.add(eachPermission);
}
}
// Request for missing permissions
if (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() {
@Override
public void run() {
ToastUtils.setResultToToast(MainActivity.this.getString(R.string.sdk_registration_doing_message));
DJISDKManager.getInstance().registerApp(MainActivity.this.getApplicationContext(), new DJISDKManager.SDKManagerCallback() {
@Override
public 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));
} else {
ToastUtils.setResultToToast(MainActivity.this.getString(R.string.sdk_registration_message) + djiError.getDescription());
}
Log.v(TAG, djiError.getDescription());
}
@Override
public void onProductDisconnect() {
Log.d(TAG, "onProductDisconnect");
notifyStatusChange();
}
@Override
public void onProductConnect(BaseProduct baseProduct) {
Log.d(TAG, String.format("onProductConnect newProduct:%s", baseProduct));
notifyStatusChange();
}
@Override
public 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();
}
@Override
public void onInitProcess(DJISDKInitEvent djisdkInitEvent, int i) {
}
});
}
});
}
}
private void notifyStatusChange() {
DJISampleApplication.getEventBus().post(new ConnectivityChangeEvent());
}
//endregion
private void refreshTitle() {
if (stack.size() > 1) {
ViewWrapper wrapper = stack.peek();
titleTextView.setText(wrapper.getTitleId());
} else if (stack.size() == 1) {
BaseProduct product = DJISampleApplication.getProductInstance();
if (product != null && product.getModel() != null) {
titleTextView.setText("" + product.getModel().getDisplayName());
} else {
titleTextView.setText(R.string.sample_app_name);
}
}
}
@Subscribe
public void onConnectivityChange(ConnectivityChangeEvent event) {
runOnUiThread(new Runnable() {
@Override
public void run() {
refreshTitle();
}
});
}
}
更多推荐
所有评论(0)