Spring 2016 Pathfinder: Project Tango Bluetooth API

tango_system

by:

Peiyuan Xu (Project Manager)

with contribution from:

Qianyi Tang (CSULB Computer Science Major Graduate Student)

Introduction:

In our Project Tango system block diagram, one of the software module is to develop Bluetooth API in the Point Cloud App command sequence. This blog post is to show the approach of modifying point cloud App to pair and connect another device to Tango tablet through Bluetooth while still running Point Cloud.

Steps:

  1. Download and install Android Studio and Tango SDK
  2. Download the Point Cloud sample code and import it into Android Studio
  3. Connect the Tango tablet to the PC and enable the Android Debugging Bridge (ADB)
  4. Build and run the Point Cloud sample code. This would automatically install the App in Tango tablet
  5. Open project → app → Java → PointCloudActivity.java
  6. Open project → res → layout → activity_jpoint_cloud.xml
  7. Open project → res → values → strings.xml
  8. Breakdown and modify the code

The download instruction and links can be found here:

 

Code Breakdown and explanations:

1.Create the Bluetooth Button

Add the following code to “activity_jpoint_cloud.xml”
/******/
<Button
android:id=“@+id/bluetooth_button”
android:layout_width=“100dp”
android:layout_height=“wrap_content”
android:layout_above=“@+id/first_person_button”
android:layout_alignParentRight=“true”
android:layout_marginBottom=“5dp”
android:layout_marginRight=“5dp”
android:paddingRight=“5dp”
android:text=“@string/bluetooth” />

/******/

The code above will generate a button on the screen called”bluetooth” with the width of 100 dp(density-independent pixles). The “bluetooth” button will be placed above the “first_person_button” in the point cloud App.

2. Add the string of “bluetooth”

Next, go to “strings.xml”. Add one line of code:

/******/

</string>
<string name=”bluetooth”>bluetooth</string>

/******/

This gives the string type of “bluetooth” that can be called in the “activity_jpoint_cloud.xml”

3. Modify the code in point_cloud_activity

Now, go to “PointCloudActivity.java”. Add the following private classes:

/******/

private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
private boolean bluetoothFlag = false;

/******/

This declares Bluetooth adapter and store the list of paired Bluetooth devices. The last line is to store the state of Bluetooth(on/off) because the button can be used as a switch. By initial, it is set to off.

Add one line below:

/******/

BA = BluetoothAdapter.getDefaultAdapter();

/******/

This will assign the BluetoothAdapter.

Now go down to public classes, Add the following code:

/********/

public void bluetoothOn(){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(),”Turned on”,Toast.LENGTH_LONG).show();
visible();
}
else
{
Toast.makeText(getApplicationContext(),”Already on”, Toast.LENGTH_LONG).show();
}
}

/********/
This creates a function “bluetoothOn()” to turn on Bluetooth device and toast a message (“Turned on” or “Already on”).
/********/

public void bluetoothOff(){
BA.disable();
Toast.makeText(getApplicationContext(),”Turned off” ,Toast.LENGTH_LONG).show();
}

/********/

similarly, this creates a function “bluetoothOff()” to turn off Bluetooth device and toast a message “Turned off”

Next add the following lines below:

/********/

public  void visible(){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}

/********/

This will alter the Bluetooth device into visible state.

Continue adding these lines of code:

/********/

public void list(){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(),”Showing Paired Devices”,Toast.LENGTH_SHORT).show();

}

/********/

This creates an array and add paired devices into list.

Finally, incorporate the main function:

/********/

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.first_person_button:
mRenderer.setFirstPersonView();
break;
case R.id.third_person_button:
mRenderer.setThirdPersonView();
break;
case R.id.top_down_button:
mRenderer.setTopDownView();
break;
case R.id.bluetooth_button:
if(bluetoothFlag == false){
bluetoothOn();
bluetoothFlag = true;
}else {
bluetoothOff();
bluetoothFlag = false;
}
default:
Log.w(TAG, “Unrecognized button click.”);
}
}

/********/

This will create the click event handler. Based on different click event,it will switch viewer’s perspective and turn on/off Bluetooth device. In Bluetooth button section, this function will switch adapter between on/off.

IMG_3210

This is what the App will looks like after the modification. Notice the Button shows upon on the screen.

The modified Java Code can be downloaded here

 

Conclusion:

Modifying an existing Android App could be difficult since it requires good knowledge of Java Programming language as well as understand Android Studio platform. In this approach, I first did research and learn the basic Android App development and Java programming on Udacity website. Then I start modifying code in Point Cloud Existing App but unfortunately I crushed the App over and over again. After we got the Project Tango system block diagram from professor Hill, I start researching into Android Bluetooth API and trying to implement existing bluetooth sample code into the Point Cloud activity. Finally I successfully debug and run the modified App but the button itself does not work properly. After trying to debug the code, I found out that I need assistant from someone who develop the point cloud App to let me know where the breakpoints goes since I lost track of the breakpoints when stepping into the code.

This blog post does not show the fully functional Point Cloud App with Bluetooth feature, but can be documented and used as a good resource for individuals or groups from future class to continue developing the Java Point Cloud App.

 

Source Materials: