Language - Android Java: Difference between revisions

From Phidgets Support
 
(90 intermediate revisions by 7 users not shown)
Line 1: Line 1:
Preamble about the language and its general strengths and weaknesses.
#REDIRECT [[Language_-_Java_Android_Studio_Android]]
 
==Map==
 
Lost?  Here's how you can learn to write code for your Phidget:
 
# Pick your Operating System
#:Download core drivers
#:Learn about the general Phidget Library
#:Learn ways to design your code
# Pick your Programming Language
#;<span style="color:green;">(You are here)</span>
#:Learn about libraries for your specific language
#:Compile and Run examples
# Learn about functions for your specific device
# Ready to do anything with Phidgets!
 
==Assessment for use with Phidgets==
 
On the Android platform, Java is currently the only available language.
 
==Support==
 
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]].
 
==Development Environments and Compilers==
 
The Phidget examples given below -- as well as the Android SDK examples given by Google -- use Eclipse for a development environment.
 
To download and configure Eclipse to work with the Android SDK and the Phidget Libraries, please see the [[OS - Android]] page.
 
==Drivers, Libraries and Resources==
 
After setting up your [[OS - Android|Android Development computer]] (Windows, Mac, Linux), you will need the following setup files to write code for your Phidget:
 
* <span style="color:red;">Phidget Libraries</span> for Android
* The [[Phidget Webservice]] should be installed on the remote computer hosting a Phidget for an Android device to control
* <span style="color:red;">Phidget Examples</span> for Android
* <span style="color:red;">The Java API</span> for using Phidgets
* You might also find the [[Language - Java|General Java]] page helpful, as it has more Java examples.
 
We also recommend that you read the [[Software Overview#General Phidget Programming|General Programming]] guide to get a feel for how to design your 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)
 
How to use this library is described below.
 
===Remote Phidget Use===
 
To install libraries to run a Phidget '''remotely''':
* Add libs/ and phidget21.jar to your project
* Add the following to your <code>AndroidManifest.xml</code> file:
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=xml>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
</source>
</font>
</div>
 
===Direct Phidget Use===
 
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
* Add the following to your <code>AndroidManifest.xml</code> file:
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=xml>
    <uses-feature android:name="android.hardware.usb.host" />
</source>
</font>
</div>
 
===In Eclipse===
 
In Eclipse, jar files and library files can be added to a project by:
* Project &rarr; Properties &rarr; Java Build Path &rarr; Libraries (tab) &rarr; Add JAR
* Project &rarr; Properties &rarr; Java Build Path &rarr; Libraries (tab) &rarr; Add Library
 
==Getting Started==
 
===Import an Existing Example===
 
Unpack the <span style="color:red;">Phidget Examples</span> for Android.
 
Import one of the Existing Eclipse Projects:
* File &rarr; Import... &rarr; General &rarr; Existing Projects Into Workspace &rarr; (Next)
* Select root directory &rarr; 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 &rarr; Show View &rarr; Package Explorer)
 
Select Run As... &rarr; 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).
 
===Coding For Your Phidget===
 
Before you can use the Phidget, you must include a reference in the code to the library. In Android Java:
 
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source 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.*;
 
</source>
</font>
</div>
 
Remember to add your libraries and .jar files to where Java can find them.  For Eclipse, this is described above in the Libraries and Drivers section.
 
===Connecting to the Phidget===
 
Before writing code of your own, it may help to read about [[Event Driven Code]] before proceeding.
 
In your code, you can open and connect to a Phidget using open() or openAny().
 
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 style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.openAny();
  ik.waitForAttachment();
 
</source>
</font>
</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 style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.close();
  ik = null;
 
</source>
</font>
</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 style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=java>
 
  ik.addSensorChangeListener(new SensorChangeListener()
  {
      public void sensorChanged(SensorChangeEvent se)
      {
        //Insert your code here
        System.out.println(se.getValue());
      }
  });
 
</source>
</font>
</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===
 
 
===Compiling a .jar File===
 
==Building your Project==
Describe the different ways a project could be built using this language.
 
==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.
 
Error: <span style="color:#900;">Unable to get view server protocol version from device emulator</span><br>
Likely Fix: Project &rarr; Clean... &rarr; Clean All  (If that does not work, clean again and restart Eclipse)
 
Error: <span style="color:#900;">Android requires compiler compliance level 5.0 or 6.0. Found 'X.Y' instead</span><br>
Likely Fix: 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 &rarr; Show View &rarr; Package Explorer)
* Find the project, right-click and select Properties
* In Properties &rarr; Java Compiler &rarr; (Checkbox) Enable Project Specific Settings &rarr; Set Compiler Compliance Level = Java Version &rarr; Apply
* When told this requires rebuild, say Rebuild Now

Latest revision as of 20:10, 4 July 2019