Android workshop to prepare for 48hacks (http://www.ntuventures.com/events/48hacks/index.html), organized by NTU Venture.
Covers basic Android application programming, and connectivity to Arduino board via Bluetooth.
19. Hello World: Installing SDK
1. Install Eclipse - the IDE
http://www.eclipse.org/downloads/packages/eclipse-classic-371/indigosr1
2. Install Android SDK
http://developer.android.com/sdk/index.html
Guide from http://developer.android.com/sdk/installing.html
20. Hello World: Installing SDK
3. Install Android ADT Plugin for Eclipse
http://developer.android.com/sdk/eclipse-adt.html#installing
a. Eclipse > Help > Install new software
b. Add repository https://dl-ssl.google.com/android/eclipse/
c. Select “Developer Tools”
d. Next, next, next and finish!
Guide from http://developer.android.com/sdk/installing.html
21. Hello World: Installing SDK
4. Setup ADT Plugin
a. Ecplise > Preferences > Android
b. Point to your Android ADK Location (from step 2)
Guide from http://developer.android.com/sdk/installing.html
24. Hello World: Run
• Run > Run as > Android Application
• If running on actual device, do this first:
Settings > Applications > Development and turn on USB debugging
25. Now you see “Hello World”, let’s look at the code..
28. Fundamentals: Activity
• Launch an Activity by calling
startActivity(intent)
Launch a known Activity
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
Initiate a system launch
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);
31. Fundamentals: Activity
• Activity must be declared in AndroidManifest.xml
<manifest ... >
<application ... >
<activity android:name=".ExampleActivity" />
...
</application ... >
...
</manifest >
32. Fundamentals: Service
• Service or Thread ?
• Service runs in the background, even when user is not
interacting with your app.
• To start, call startService()
• Similar to Activity, has its lifecycle and various event
callbacks.
33. Fundamentals: ContentProvider
• A way to share data across applications,
including apps such as phonebook,
calendar, etc.
• In other words, you can access the
phonebook using a ContentProvider
• Have query, insert, delete methods (SQLite)
ContentResolver cr = getContentResolver();
cr.query(“content://android.provider.Contacts.Phones.CONTACT_URI”,
projection,
selection, selectionArg,
sortOrder)
37. User Interfaces: XML
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
38. User Interfaces: Code
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
50. Bluetooth
• Bluetooth Modem, aka BlueSMiRF Gold, aka Firefly
• http://www.sparkfun.com/products/582
• Specifications
• Extremely small
• Operating Voltage: 3.3V-6V
• Serial communications: 2400-115200bps
51. Bluetooth: The Pins
• Pins from left to right
• GND
• CTS-1
• VCC
• TX-0
• RX-1
• RTS-0
52. Bluetooth: Connect to Android
• A guide from instructables.com
http://www.instructables.com/id/Android-talks-to-Arduino/?ALLSTEPS
• !!! NOTE: Arduino version 1.0 does not work with
the libraries. Download version 0023 instead
http://arduino.cc/en/Main/Software
53. Bluetooth: Connect to Android
1. Connect from Bluetooth modem to Arduino
• VCC to +3.3V
• GND to GND
• TX-0 to PIN-2 (RX)
• RX-1 to PIN-3 (TX)
If works, you will see a red LED blinking
55. Bluetooth: Connect to Android
2. Import NewSoftSerial library to Arduino
• Provides an interrupt architecture (vs SoftwareSerial)
• Copy /Library/NewSoftSerial to:
• Mac: ~/Documents/Arduino/libraries/
• Windows: My DocumentsArduinolibraries
• Instructions on how to use at http://arduiniana.org/libraries/
newsoftserial/
You will see NewSoftSerial in Sketch > Import Library >
56. Bluetooth: Connect to Android
3. Upload sketch to Arduino
• /Sample Code/bluetooth/bluetooth.pde
57. Bluetooth: Connect to Android
4. Open BluetoothChat, an Android sample app
• File > New > Android Project
• Select target Android 2.3.3
• Create project from existing sample > BluetoothChat > Finish
• In BluetoothChatService, change the UUID to
"00001101-0000-1000-8000-00805F9B34FB"
• Build and deploy to phone
58. Bluetooth: Connect to Android
5. To send a message
• Pair up: Settings > Wireless & network > Bluetooth > Select FireFly-xxxx.
Password is 1234.
• Connect: Open BluetoothChat app. Press Menu > Connect > FireFly-xxxx
• When connected, Bluetooth modem will have green light
• Type a message and send. If success, PIN 13 will light up for 5 sec.
59. Bluetooth: Connect to Android
6. Change baud rate to 57,000
• Refer to http://www.instructables.com/id/Change-BAUD-rate-on-BlueSMiRF-
Gold/
• Need FTDI Basic chip..
http://developer.android.com/guide/topics/fundamentals.html\nActivity - A single screen (UI)\nService - background activity eg. play music globally\nContentProvider - Even share data across apps. SQLite. eg. contacts. ContentResolver\nBroadcastReceiver - Responds to system wide broadcast announcements. Usually from the system eg. Send Message, Picture captured\n