...

Setting Up Android


SDK Setup:

Downloaded http://developer.android.com/sdk/index.html android-sdk_r16-linux.tgz. Unzipped into ~/opt/android-sdk-linux. Started the Android SDK Manager using ./android in android-sdk-linux/tools. This fires up a GUI, where the SDK and API can be added, updated and deleted.

Also wanted to do some work on the G-1, if possible. That runs 1.6, so put in the 1.6 SDK. Got the G1 skin from http://teavuihuang.com/android/HVGA-P-G1.zip and put the contents in android-sdk-linux/platforms/android4/skins/HVGA-P-G1.

Wanted to start developing for the HTC Comet. So installed the Android 2.2 (API 8) SDK platform. Now could not find an emulator skin for the HTC Comet, so created a virtual device to try to approximate it. Fire up the Android Virtual Device Manager from "Tools" -> "Manage AVDs". Created a new one with Target "Android 2.2 - API Level 8 and Skin "Built-in WVGA800".

To fire up an AVD from the command line, without eclipse
/opt/android-sdk-linux/tools$ ./emulator -avd htc_comet
where "htc_comet" is from where the AVD were created (~/.android/avd/htc_comet.avd)
see http://developer.android.com/tools/help/emulator.html and http://developer.android.com/tools/devices/managing-avds-cmdline.html for more details on how to use the emulator.


Installing SL4A and Python onto emulator:

You can use adb (in the ~/opt/android-sdk-linux/platform-tools directory) to install apks.
Started up the AVD instance for the Comet from the Android Virtual Device Manager.
Now, form the platform-tools directory

~/opt/android-sdk-linux/platform-tools$ ./adb start-server
~/opt/android-sdk-linux/platform-tools$ ./adb devices
List of devices attached 
emulator-5554 device

~/opt/android-sdk-linux/platform-tools$ ./adb install ~/Desktop/sl4a_r6.apk 
2845 KB/s (887749 bytes in 0.304s)
 pkg: /data/local/tmp/sl4a_r6.apk
Success
:~/opt/android-sdk-linux/platform-tools$ ./adb install ~/Desktop/PythonForAndroid_r4.apk 
1545 KB/s (127017 bytes in 0.080s)
 pkg: /data/local/tmp/PythonForAndroid_r4.apk
Success

http://android-scripting.googlecode.com/files/PythonForAndroid_r4.apk
In phone "Application Settings" allowed install of non-Market applications (form unknown sources)

then started up "Python For Android"
"Install"- it took a couple of tries, before it started downloading and installing some stuff
then the "Install" button became "Uninstall"

Installed SL4A r4.apk

Start "SL4A" from the applications. it shows a list of python scripts.


clicking on a script and then the gear button should run it.
but couldn't even run the dam test.py on the G1, it kept coming up
with "The application SL4A (process com.googl.code.android_scripting) has stopped unexpectedly."
had to force close.
tried to install on the G1, some notes below, but was not able to was not able to install successfully.
http://android-scripting.googlecode.com/files/PythonForAndroid_r4.apk
In phone "Application Settings" allowed install of non-Market applications (form unknown sources)
then started up "Python For Android"
"Install"- it took a couple of tries, before it started downloading and installing some stuff
then the "Install" button became "Uninstall"
Installed SL4A r4.apk
Start "SL4A" from the applications. it shows a list of python scripts.
clicking on a script and then the gear button should run it.
but couldn't even run the dam test.py on the G1, it kept coming up with 
"The application SL4A (process com.googl.code.android_scripting) has stopped unexpectedly."
had to force close.
so uninstalled SL4A, and installed this instead
http://dl.dropbox.com/u/1720529/ScriptingLayerForAndroid_r2_withissue432patch.apk
this allowed me to run the scripts without crashing.
Result: was not able to run my own simple webview scripts, some kind of RPC error. so gave up on 
trying to run this on the G1


so uninstalled SL4A, and installed this instead

http://dl.dropbox.com/u/1720529/ScriptingLayerForAndroid_r2_withissue432patch.apk

this allowed me to run the scripts without crashing.

But can't seem to do anything useful on 1.6, looks like I have to try out my stuff for atleast 2.2.


On the HTC Comet T-mobile phone, stock, running Android 2.2

first installed
http://android-scripting.googlecode.com/files/sl4a_r6.apk
then installed
http://android-scripting.googlecode.com/files/PythonForAndroid_r4.apk
then opened. in the Python For Android screen,  click the "install" button. it will install python_r16.zip, python_extras_r14.zip, python_scripts_r13.zip

was able to run through the sample scripts.


used notes from this
http://www.instructables.com/id/Androino-Talk-with-an-Arduino-from-your-Android-d/?ALLSTEPS

also see http://beta.appinventor.mit.edu/about/




ran this program on the arduino
void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  
  digitalWrite(13, LOW);
}
void loop() {
  if (Serial.available() > 0) {  
    char incoming = Serial.read();
    if (incoming == 'a')
      digitalWrite(13, HIGH);
    if (incoming == 'b')
      digitalWrite(13, LOW);      
  }
}

connected the blutooth device to the arduino
Vcc -> 5V
Gnd -> Gnd
Rx  -> Tx
Tx  -> Rx

on applying power, a red led keeps blinking (blinking for discoverable, solid for paired?)

ran this program on the laptop

#!/usr/bin/python
from bluetooth import *
print "performing inquiry..."
nearby_devices = discover_devices(lookup_names = True)
print "found %d devices" % len(nearby_devices)
for name, addr in nearby_devices:
     print " %s - %s" % (addr, name)

it printed out 

performing inquiry...
found 1 devices
 linvor - 00:12:05:11:90:32

then ran this program, to test out connectivity

#!/usr/bin/python
from bluetooth import *
server_address = "00:12:05:11:90:32"
port = 1
print "connecting to \"%s\" on %s" % (server_address, port)
sock = BluetoothSocket( RFCOMM )
sock.connect((server_address, port))
print "connected.  ready for input..."
while True:
    data = raw_input() # read stdin, stripping the trailing newline
    sock.send(data)
    if data == 'x':
        break
sock.close()

the first time, linux popped up a dialog asking for pin to pair, entered 1234
Or Linux, pair with the device on your computer, then use the arduino serial monitor to interact.



void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  
  pinMode(2, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(2, LOW);
}
void loop() {
  if (Serial.available() > 0) {  
    char incoming = Serial.read();
    if (incoming == 'a') {
      digitalWrite(13, HIGH);
      digitalWrite(2, HIGH);
      delay(180);
      digitalWrite(2, LOW);
    }
    if (incoming == 'b')
      digitalWrite(13, LOW);      
  }
}



Upload both files to the /sdcard/sl4a/scripts/ directory on your phone.
androino.html
androino.py

dump.py
----------
script which simply dumps anything that is send via serial out from your arduino.

import android
from sys import stdout

#BT_DEVICE_ID = '00:12:05:11:90:32'
BT_DEVICE_ID = None  # open the device selection popup

droid = android.Android()
droid.toggleBluetoothState(True)  # turn on bluetooth
print droid.bluetoothConnect('00001101-0000-1000-8000-00805F9B34FB', BT_DEVICE_ID)
print droid.bluetoothGetConnectedDeviceName()
while True:
    stdout.write(droid.bluetoothRead().result)

droid.exit()

to manage files to the emulator sdcard, tools/./ddms, then fire up the avc using tools/./emulator -avd htc-comet. the ddms wil then show the avd connected. do Device -> File Explorer. should be able to navigate to the mnt/sdcard folder. should then be able to "Pull File from Device" or "Push File onto Device".


Android related Links

Manning Unlocking Android (good book)
http://developer.android.com
http://developer.android.com/reference/packages.html (api documentation)

SDK setupe

command line tools:
--------------------
 - in the platform-tools directory
aapt: Android Asset Packaging Tool. packages the app components into an apk.
adb: Android Debug Bridge. provides access to emulators running on and devices connected the computer. 
    client/server program, server also automatically started by Eclipse ADT
    adb devices # list available Android emulator sessions
    adb shell # connect to 
    adb –d 1 shell # connect to the 1st emulator session (adb shell, if only one session).
    adb shell install someapplication.apk # install app to /data/app directory
    adb shell rm /data/app/someapplication.apk # remove app
    adb start-server
    adb kill-server
    adb shell dumpsys activity # see infor about running processses

telnet localhost 5554 # connect to console on emulator running on tcp 5555 (5554 + 1)

Installing app on device:
-------------------------
 - can install apps onto devices in debug mode, it automatically used debug keystore.
 - to install and run app outside of debug mode, the app needs to be signed.
 - can use java's keytool to generate self-signed private key, for signing apps
   keytool -genkey -v -keystore nr-android-testing.keystore -alias nr_android_testing -keyalg RSA -validity 1000
     keystore password: nrtest
     nr_android_testing password: nrtest
 - can sign app using sdk tool, or through eclipse
   Eclipse -> app -> Android Tools -> Export Signed Application Package
   (walk through, use the keystore, password, alias, and password that were setup using keytool)
 - can install app on device using adb
   ./adb start-server
   ./adb devices
   ./adb install app1.apk # installs the app onto the device



Development Setup:
------------------




install sdk starter package
http://developer.android.com/sdk/index.html android-sdk_r16-linux.tgz
unzipped into ~/opt/android-sdk-linux



Setting up Eclipse for Android


install ADT Plugin for Eclipse
eclipse (Juno) -> Help -> Install New Software -> Add Repository 
http://download.eclipse.org/releases/juno/
https://dl-ssl.google.com/android/eclipse/
(see http://developer.android.com/sdk/installing/installing-adt.html or more details)



on restarting eclipse, dialog popped up asking for Android sdk. since i had already downloaded, pointed it t
to ~/opt/android-sdk-linux
it said "SDK Platform Tools component is missing! Please use the SDK Manager to install it."
from console, launch the sdk manager
~/opt/android-sdk-linux/tools > ./android
(or eclipse -> Window -> Android SDK Manager)
from gui, chose to install "android sdk platform tools", "android 1.6", "android 2.2"
(1.6 for my old G1, 2.2 for munazza's phone)




Android settings in eclipse -> Window -> Preferences

Project in Eclipse:
-------------------
File -> New -> Android Project




Run -> Run Configuration -> Android Application -> New -> 




Use AVD manager to create and manage Android Virtual Devices for use with the emulator
got the G1 skin from http://teavuihuang.com/android/HVGA-P-G1.zip and put the contents in 
android-sdk-linux/platforms/android4/skins/HVGA-P-G1




To debug the app on the phone:
in manifest set 
set up run/debug configuration for Android Application, and for target, set "manual", so that you can pick the
attached device when debugging
also on the device itself, Settings -> Application -> Debug must be checked




Android Development - Application concepts and structure

simulator - offers source code level compatiblity, needs source code, cannot run the final device binary.
emulator - offers binary level compatibility, can run same binary on final device as well as emulator.



Intent: --------   - is the central Android app paradigm   - is a declaration of need, describes what you want to do.     eg. “I want to look up a contact record,”       or “Please launch this website,”       or “Show the Order Confirmation Screen.”   - has an action attribute (eg. verb like VIEW, PICK, or EDIT)    action can be built-in Intent (android.content.Intent.*) or user defined.  - has data component (form of a URI)    eg. Contact lookup content://contacts/people        Map lookup/search Geo:0,0?q=23+Route+206+Stanhope+NJ        Browser launch website http://www.google.com/  - implicit Intent depends on system to find best fit activity based on registered IntentFilters.  - explicit Intent specifically asks for a particular Activity. IntentFilter: --------------  - is a declaration of capability of servicing needs of intents.  - ties the Intent to the application  - can tie the action and/or data of the Intent  - has a category  - may be generic or specific to which Intents it can service.  - Intent to IntentFilter mapping (dispatching) is done at runtime (late binding) Activity: ---------  - corresponds to a ui in an application  - can have one or more views of ui elements  - startActivity(intent), or startActivityForResult(intent) moves between Activities (screens) in an app.   Service: --------  - is like an Activity that runs in te background  - may or may not have a ui  - started using context.startService(intent)  - extends Service implements Runnable BroadcastReceiver: ------------------  - is service like and non-ui.  - is extended by an app that wants to recv and service global events, like phone ringing, or SMS  - can register to receive Intents using n the AndroidManfest.xml file.    Android then starts the app when event occurs  - can register to receive Intents at runtime by Context.registerReceiver.  - implements onReceive(context, intent), which should usually fire off a Service ContentProvider: ----------------  - extended by an app that provides read and/or write access to data.  - can provide data access to the Activities, Services or BroadcastReceivers in the same or another app.  - can provide persistence by files, SQLLite db, or in-memory hash Application: -------------   - an Android app will contain at least one Activity, Service, BroadcastReceiver, or ContentProvider.  - the AndroidManifest.xml is the application configurator and descriptor.  - an app maps to a linux process on the android os  - processes (apps) may be unloaded or killed by os, to control resource and priority constraints  - source --javaCompiler--> .class(bytecode) --aapt?--> .dex + resources --aapt--> .apk --runsOn--> Dalvik vm --runsOn--> Linux      View: -----  - described in xml layout files   Resource: ---------- dev patterns: ------------- Activity:  - subclass of ApplicationContext, so has hooks into lifecycle  - override onCreate,      use setContentView to associate the xml layout to get the views (ui elements)     (btw, can also do this association programatically, instead of using a layout)     bind to local variables any views from the xml that will need programmatic access     (views will show up whether bound or not, it's just that if not bound, can't interact from code))     set event handlers for view elements  - override onCreateOptionsMenu, (menu is the controls that appear on the bottom of the screen for the Menu hard butyon)     add one or more menu items  - override onMenuItemSelected, to handle the above options menu item selections.  - can have a class in app the extends Application, with member variables    these member variables can be used to pass state info between Activities  - use startActivity(intent) to pass control to another Activity.  - use startActivityforResult(intent) if need to return back to this (calling) Activity from the called Activity. Activity lifecycle:  - os may pause, resume or kill apps, as it deems necessary. app is notified of these through the lifecycle methods.    |-onCreate(): when  created. do setup here.    |  |-onRestart(): when restarted, if it is still in the stack, rather than starting new.    |  |-onStart(): when being dislayed on screen to user.    |  |  |-onResume(): when it starts interacting with user. (always called,  whether starting or restarting.)    |  |  |-onPause(): when being paused (by user or os). important - save all state here.    |  |-onStop(): when it stops and transitions to nonvisible, and subsequent lifecycle events.    |-onDestroy(): when being completely removed from system memory (due to user triggered onFinish(), or eviction by os)

To check out:

http://hackaday.com/2011/09/26/bluetooth-communications-for-android-devices-via-processing/
http://hackaday.com/2013/02/15/beginners-androidarduino-example-shows-the-power-of-app-inventor/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+hackaday%2FLgoM+%28Hack+a+Day%29