|
|
(53 intermediate revisions by 7 users not shown) |
Line 1: |
Line 1: |
| [[Category:Language]] | | #REDIRECT [[Language_-_Java_Android_Studio_Android]] |
| [[File:Icon-Android.png|64x64px|link=]] Android Java is the main language used to program for the [[OS - Android|Android OS]].
| |
| | |
| | |
| __TOC__
| |
| | |
| ==Introduction==
| |
| | |
| {{LanguageSupport|Android|nearly the complete Phidget API, including events, and only excepting a few open() calls outlined later|the Phidget Interface Kit and two Hello World examples.|Eclipse on Windows, Mac OS, and Linux|}}
| |
| | |
| ==Quick Downloads==
| |
| {{QuickDownloads|Android|
| |
| {{APIQuickDownloads|http://www.phidgets.com/documentation/JavaDoc.zip}}|
| |
| {{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/android-examples_2.1.8.20120216.tar.gz|(and Phidget Android Library)}}|
| |
| {{ExtraLibraryQuickDownloads|http://www.phidgets.com/downloads/examples/android-examples_2.1.8.20120216.tar.gz||(Phidget21.jar included in Examples)}}
| |
| {{WindowsQuickDownloads}}
| |
| {{MacQuickDownloads}}
| |
| {{LinuxQuickDownloads}}
| |
| }}
| |
| | |
| ==Getting Started with Android Java (Eclipse)==
| |
| | |
| If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
| |
| {{ExampleCodeReasons}}
| |
| | |
| As Android development is primarily done on Eclipse, and Eclipse is relatively platform-independent, we provide instructions for Eclipse rather than by operating system.
| |
| | |
| For basic differences on installing and setting up Eclipse on your development system, you can try the [[OS - Android|Android Operating System]] page. However, your best bet for information about general use of Eclipse on your development operating system (Windows, Mac, or Linux) will be the [http://www.eclipse.org/documentation/ Eclipse support page] or the Internet at large.
| |
| | |
| Phidgets can run directly plugged in to Android devices with a USB port and system 3.1 or later.
| |
| | |
| Otherwise, Android can control a Phidget remotely over a network, by using the [[Phidget Webservice]]
| |
| | |
| Hence, we split instructions up by Android OS version number.
| |
| | |
| ===Android Under 3.1 (and/or no USB Port)===
| |
| | |
| These types of phones and tablets can use Phidgets only remotely over a network using the [[#Webservice|Phidget Webservice]].
| |
| | |
| ====Use Our Examples====
| |
| | |
| Download and unpack the [http://www.phidgets.com/downloads/examples/android-examples_2.1.8.20120216.tar.gz Phidget Examples for Android]. There are two Hello World examples: {{Code|HelloWorldRemote}} (the one you want), and {{Code|HelloWorldUSB}} (for tablets with a USB port).
| |
| | |
| Import the {{Code|HelloWorldRemote}} example into Eclipse:
| |
| * File → Import... → General → Existing Projects Into Workspace → (Next)
| |
| * Select {{Code|HelloWorldRemote}} root directory → Select all files → Finish
| |
| | |
| Note that you '''must change''' the IP address within the example code to the IP address of the computer directly connected to the Phidget you are trying to control.
| |
| | |
| Also note that the HelloWorldRemote example was written for Android 2.1. You can change this in:
| |
| :(Right Click on Project) → Properties → Resource (on left) → Android
| |
| | |
| Right-click on project in Package Explorer (To open this, use Window → Show View → Package Explorer)
| |
| | |
| Select Run As... → Android Application
| |
| | |
| This will bring up the Android Virtual Device manager window. If your Android hardware is plugged in to your debugging computer, you will see the hardware as an option on which to run the example. Otherwise, make sure you have an Android Virtual Device (AVD) set up to run the example on (you can do this by clicking <code>New...</code> on the right side).
| |
| | |
| ====Write Your Own Code====
| |
| | |
| When you download and unzip the <span style="color:red;">Phidget Library</span> for Android, it will contain three things:
| |
| # A libs/ folder
| |
| # A jar file containing the general Phidget java library (phidget21.jar)
| |
| # A jar file for directly driving USB devices from a USB port on the Android device (PhidgetsUSB.jar)
| |
| | |
| To install libraries to run a Phidget '''remotely''' over a network using the Webservice:
| |
| * Add libs/ and phidget21.jar to your project
| |
| * Add the following to your <code>AndroidManifest.xml</code> file:
| |
| | |
| <div class="source"><syntaxhighlight lang=xml>
| |
| <uses-permission android:name="android.permission.INTERNET"></uses-permission>
| |
|
| |
| </syntaxhighlight></div>
| |
| | |
| Before you can use the Phidget, you must include a reference in the code to the library. In Android Java:
| |
| | |
| <div class="source"><syntaxhighlight lang=java>
| |
| // This contains all of the devices and the exceptions
| |
| import com.phidgets.*;
| |
|
| |
| // This contains all of the event listeners
| |
| import com.phidgets.event.*;
| |
| </syntaxhighlight></div>
| |
| | |
| Remember to add your libraries and .jar files to where Java can find them.
| |
| In Eclipse, jar files and library files can be added to a project by:
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add JAR
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add Library
| |
| | |
| ===Android 3.1+ with USB Port===
| |
| | |
| | |
| ====Use Our Examples====
| |
| | |
| Unpack the <span style="color:red;">Phidget Examples</span> for Android.
| |
| | |
| Import one of the Existing Eclipse Projects:
| |
| * File → Import... → General → Existing Projects Into Workspace → (Next)
| |
| * Select root directory → Browse to find and select an existing example such as the InterfaceKitExample
| |
| | |
| Select all files, click Finish
| |
| | |
| Right-click on project in Package Explorer (To open this, use Window → Show View → Package Explorer)
| |
| | |
| Select Run As... → Android Application
| |
| | |
| This will bring up the Android Virtual Device manager window. If your Android hardware is plugged in to your debugging computer, you will see the hardware as an option on which to run the example. Otherwise, make sure you have an Android Virtual Device (AVD) set up to run the example on (you can do this by clicking <code>New...</code> on the right side).
| |
| | |
| | |
| ====Write Your Own Code====
| |
| | |
| =====Step One: Include the Phidget Libraries in your Source=====
| |
| | |
| <div class="source">
| |
| <syntaxhighlight lang=java>
| |
| import com.phidgets.*;
| |
| import com.phidgets.event.*;
| |
| </syntaxhighlight>
| |
| </div>
| |
| | |
| =====Step Two: Link the Phidget Libraries to your Project=====
| |
| | |
| When you download and unzip the <span style="color:red;">Phidget Library</span> for Android, it will contain three things:
| |
| # A libs/ folder
| |
| # A jar file containing the general Phidget java library (phidget21.jar)
| |
| # A jar file for directly driving USB devices from a USB port on the Android device (PhidgetsUSB.jar)
| |
| | |
| Move the {{Code|libs}} folder and the {{Code|phidget21.jar}} file to your project folder.
| |
| | |
| In Eclipse, jar files and library files can be added to a project by:
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add JAR
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add Library
| |
| | |
| To install libraries to run a Phidget '''directly''' (Android 3.1 and later, with USB port on device):
| |
| * Add libs/, phidget21.jar, and PhidgetsUSB.jar to your project
| |
| | |
| =====Step Three: Give your Program Permission to use USB=====
| |
| | |
| Add the following to your <code>AndroidManifest.xml</code> file:
| |
| | |
| <div class="source"><syntaxhighlight lang=xml>
| |
| <uses-feature android:name="android.hardware.usb.host" />
| |
| </syntaxhighlight></div>
| |
| | |
| Before you can use the Phidget, you must include a reference in the code to the library. In Android Java:
| |
| | |
| <div class="source"><syntaxhighlight lang=java>
| |
| // This contains all of the devices and the exceptions
| |
| import com.phidgets.*;
| |
|
| |
| // This contains all of the event listeners
| |
| import com.phidgets.event.*;
| |
| </syntaxhighlight></div>
| |
| | |
| Remember to add your libraries and .jar files to where Java can find them.
| |
| In Eclipse, jar files and library files can be added to a project by:
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add JAR
| |
| * Project → Properties → Java Build Path → Libraries (tab) → Add Library
| |
| | |
| ==Follow the Examples==
| |
| | |
| | |
| | |
| | |
| | |
| ===Code Snippets===
| |
| | |
| Before writing code of your own, it may help to read about [[General Phidget Programming]] before proceeding. We cover all of the following concepts (open, attach, etc) there in-depth.
| |
| | |
| | |
| ===Step One: Initialize and Open===
| |
| | |
| | |
| | |
| In your code, you can open and connect to a Phidget using open() or openAny().
| |
| | |
| When using a Phidget over a network, you can open the remote Phidget using one of the supported Java API calls:
| |
| <div class="source"><syntaxhighlight lang=java>
| |
| void open(int serial, java.lang.String ipAddress, int port)
| |
| void open(int serial, java.lang.String ipAddress, int port, java.lang.String password)
| |
| void openAny(java.lang.String ipAddress, int port)
| |
| void openAny(java.lang.String ipAddress, int port, java.lang.String password)
| |
| void openLabel(java.lang.String label, java.lang.String ipAddress, int port)
| |
| void openLabel(java.lang.String label, java.lang.String ipAddress, int port, java.lang.String password)
| |
| </syntaxhighlight></div>
| |
| | |
| When using a Phidget when it is directly plugged in to an Android 3.1 or later tablet, you can use one of these supported Java API calls:
| |
| <div class="source"><syntaxhighlight lang=java>
| |
| void open(int serial)
| |
| void openAny()
| |
| void openLabel(java.lang.String label)
| |
| </syntaxhighlight></div>
| |
| | |
| This leaves these Java API calls, which are '''unsupported''' on Android:
| |
| <div class="source"><syntaxhighlight lang=java>
| |
| void open(int serial, java.lang.String serverID)
| |
| void open(int serial, java.lang.String serverID, java.lang.String password)
| |
| void openAny(java.lang.String serverID)
| |
| void openAny(java.lang.String serverID, java.lang.String password)
| |
| void openLabel(java.lang.String label, java.lang.String serverID)
| |
| void openLabel(java.lang.String label, java.lang.String serverID,
| |
| java.lang.String password)
| |
| </syntaxhighlight></div>
| |
| | |
| Next, the program needs to try and connect to the Phidget through an open call.
| |
| The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.
| |
| This means that simply calling open does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or by calling waitForAttachment.
| |
| WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.
| |
| | |
| <div class="source"><syntaxhighlight lang=java>
| |
|
| |
| ik.openAny();
| |
| ik.waitForAttachment();
| |
|
| |
| </syntaxhighlight></div>
| |
| | |
| The parameters can be used to open the first Phidget of a type it can find, open based on its serial number, or even open across the network.
| |
| The API manual lists all of the available modes that open provides.
| |
| One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed.
| |
| This prevents any other instances from retrieving data from the Phidget, including other programs.
| |
| The one connection per device limit does not apply when exclusively using the Phidget Webservice.
| |
| At the end of your program, don’t forget to call close to free any locks on the Phidget.
| |
| | |
| <div class="source"><syntaxhighlight lang=java>
| |
|
| |
| ik.close();
| |
| ik = null;
| |
|
| |
| </syntaxhighlight></div>
| |
| | |
| ===Event Driven Programming===
| |
| | |
| We recommend the use of event driven programming when working with Phidgets. In Java, we hook
| |
| an event handler with the following code:
| |
| | |
| <div class="source"><source lang=java>
| |
|
| |
| ik.addSensorChangeListener(new SensorChangeListener()
| |
| {
| |
| public void sensorChanged(SensorChangeEvent se)
| |
| {
| |
| //Insert your code here
| |
| System.out.println(se.getValue());
| |
| }
| |
| });
| |
|
| |
| </source></div>
| |
| | |
| With this method, the code inside sensorChanged will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
| |
| The items from the event, such as the index or reported value, can be accessed from the SensorChangeEvent object properties.
| |
| Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets.
| |
| Please refer to the API manual for a full list of events and their usage.
| |
| | |
| ===Working Directly With the Phidget===
| |
| | |
| Some values can be directly read and set on the Phidget. Simply use the instance’s properties or call member functions such as getSensorValue(int index) or setOutputState(int index, boolean newVal) for PhidgetInterfaceKits.
| |
| These methods can be used inside a polling loop as an alternative to event driven programming.
| |
| | |
| ===Working With Multiple Phidgets===
| |
| | |
| | |
| ===Other Phidgets===
| |
| | |
| ==Common Problems and Solutions/Workarounds==
| |
| | |
| This section contains some Android Java and Eclipse-specific common problems. For more answers about using Phidgets, visit our forums, FAQ, or contact us.
| |
| | |
| {{ProblemSolution|Eclipse Error|Unable to get view server protocol version from device emulator}}
| |
| | |
| Likely Fix: Project → Clean... → Clean All (If that does not work, clean again and restart Eclipse)
| |
| | |
| {{ProblemSolution|Eclipse Error|Android requires compiler compliance level 5.0 or 6.0. Found 'X.Y' instead}}
| |
| | |
| Likely Fix: This may happen when running the examples. The javac and java version on the example do not match those on your computer.
| |
| * Find the version of java and javac on your computer (for example, <code>java -version</code> on the command line)
| |
| * In Eclipse, open the Package Explorer (Window → Show View → Package Explorer)
| |
| * Find the project, right-click and select Properties
| |
| * In Properties → Java Compiler → (Checkbox) Enable Project Specific Settings → Set Compiler Compliance Level = Java Version → Apply
| |
| * When told this requires rebuild, say Rebuild Now
| |