Android SDK Tutorial : Discover G-Box via Bluetooth

Why is the bluetooth address needed?

The very first step of our Android GHOSTDRONE App is to connect our G-Box via bluetooth. While the EHang Play App discovers and lists all available devices to us for selection, we need to specify with the EHang SDK the G-Box address by ourselves. The goal of this tutorial is to find out the MAC address of our G-Box, and save it as an input parameter for the Drone connection API.

There are numerous tools available on Google Play to assist us to discover the Bluetooth MAC address, however, you are encouraged to try it out since it's fun to code. In addition, you will soon notice the screenshot above lists all available Bluetooth devices instead of G-Boxes, have we just found a bug? Awesome!

Setting up Android project

In Android Studio, start a blank new project with empty activity, and name it DiscoveryMyGBox.

  1. File -> New -> New Project
  2. Name it DiscoveryMyGBox and click Next
  3. Check "Phone and Tablet"
  4. Specify your minimum SDK according to your Phone and click Next
  5. Choose "Empty Activity" and click next

We have setup the basic project skeleton. On the left-hand side navigation panel, expand "manifests" and open AndroidManifest.xml. Put the following two lines of code to add permission of Bluetooth administration and functionality to our app.

//AndroidManifest.xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name=android.permission.ACCESS_COARSE_LOCATION/>

Now again in the navigation panel, expand "app -> res -> layout", and open activity_main.xml to setup basic UI. We define

  • One Button to turn Bluetooth on.
  • One Button to start discovering bluetooth devices.
  • One List View to display bluetooth devices.
//main_activity.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_turnBTOn" android:text="Turn Bluetooth On"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_startDiscover" android:text="Start Discover Bluetooth Devices"/> <ListView android:id="@+id/list_BT" android:layout_width="fill_parent" android:layout_height="300dp"> </ListView> </LinearLayout> </RelativeLayout>

Core function implementation

Our core controller logic is defined in MainActivity.java that locates under app -> java. We are going to instantiate two buttons and one list view, and connect them with the resources we defined in main_activity.xml. We also need an internal data model to store discovered bluetooth info and notify UI once the data is updated. A full list of code goes here

//MainActivity.java package com.ehang.ghostsdk.discovermygbox; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { private Button bt_TurnBTOn; private Button bt_StartDiscover; private ListView listView_BTDevices; private ArrayAdapter<String> array_BTDevices; private BluetoothAdapter adapter; private final BroadcastReceiver bReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); array_BTDevices.add(device.getName() + "\n" + device.getAddress()); array_BTDevices.notifyDataSetChanged(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); adapter = BluetoothAdapter.getDefaultAdapter(); registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); if (adapter == null) { Toast.makeText(getApplicationContext(), "Not able to initialize bluetooth. Please check your phone specification.", Toast.LENGTH_LONG).show(); } else { bt_TurnBTOn = (Button) findViewById(R.id.bt_turnBTOn); bt_TurnBTOn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!adapter.isEnabled()) { Intent turnOnIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(turnOnIntent, 1); Toast.makeText(getApplicationContext(),"Bluetooth turned on" , Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Bluetooth is already on", Toast.LENGTH_LONG).show(); } } }); bt_StartDiscover = (Button) findViewById(R.id.bt_startDiscover); bt_StartDiscover.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (adapter.isDiscovering()) { adapter.cancelDiscovery(); } array_BTDevices.clear(); adapter.startDiscovery(); Toast.makeText(getApplicationContext(), "Start Bluetooth Discovering...", Toast.LENGTH_LONG).show(); } }); listView_BTDevices = (ListView) findViewById(R.id.list_BT); array_BTDevices = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); listView_BTDevices.setAdapter(array_BTDevices); } } @Override protected void onDestroy() { unregisterReceiver(bReceiver); super.onDestroy(); } }

Discover G-Box with your App

We are almost there. Once you are able to install the app onto your Android phone, you can start discovering your G-Box.

  1. Launch DiscoverMyGBox
  2. Name it DiscoveryMyGBox and click Next
  3. Turn on your G-Box
  4. Tap on button "START DISCOVERING BLUETOOTH DEVICES"

We are looking for a bluetooth device with the name "EHANG UAV". If it doesn't show up, turn off the G-Box and repeat step 3 and 4 a few times.

Congratulations for completing this tutorial! Now please note down the MAC Address of your G-Box and we can start getting data from GHOSTDRONE in the next tutorial.

Project files