|
|
(274 intermediate revisions by 10 users not shown) |
Line 1: |
Line 1: |
| [[File:icon-Java.png|64x64px]] Preamble about the language's origin and its main characteristics. | | [[Category:Language]] |
| | __NOTOC__ |
|
| |
|
| ==Support==
| | We provide support for the Java language in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets. |
| Java has a complete API and code samples for all Phidgets devices. | |
|
| |
|
| For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
| | If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with javac as the simplest path to getting your code running. |
|
| |
|
| * Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
| | Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on [[Phidget Programming Basics]]. The guide will showcase the fundamentals of programming with Phidgets, with examples in Java. |
| * In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.
| |
|
| |
|
| ==Development Environments and Compilers== | | == Setup Guide == |
|
| |
|
| Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.
| | <div class="phd-deck-sequence"> |
| | | {{PT3_JAVA_CHOOSE}}{{PT3_JAVA_WIN_JAVAC}}{{PT3_JAVA_MAC_JAVAC}}{{PT3_JAVA_LNX_JAVAC}}{{PT3_JAVA_SBC_JAVAC}}{{PT3_JAVA_WIN_NETBEANS}}{{PT3_JAVA_MAC_NETBEANS}}{{PT3_JAVA_LNX_NETBEANS}}{{PT3_JAVA_WIN_ECLIPSE}}{{PT3_JAVA_MAC_ECLIPSE}}{{PT3_JAVA_LNX_ECLIPSE}}{{PT3_JAVA_ANDROID_AS}} |
| ==Quick Downloads==
| |
| | |
| ''For using Java in Android, please refer to the [[Language - Android Java]] page.''
| |
| | |
| Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
| |
| Visit the drivers section at www.phidgets.com and get the latest:
| |
| * [http://www.phidgets.com/drivers.php Phidget Framework]
| |
| * [http://www.phidgets.com/downloads/libraries/phidget21jar_2.1.8.20110615.zip Phidget21.jar]
| |
| You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
| |
| * [http://www.phidgets.com/documentation/JavaDoc.zip API Manual]
| |
| * [http://www.phidgets.com/documentation/web/javadoc/index.html API Reference]
| |
| * [http://www.phidgets.com/downloads/examples/JavaJNI_2.1.8.20110615.zip Java Sample Code]
| |
| * You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
| |
| * The [[Device Functionality]] page explains the general operational information for your device.
| |
| | |
| You may want to have these pages open while working through these instructions.
| |
| | |
| ==Getting Started (Windows, Linux, and Mac OS)==
| |
| | |
| The Phidget full examples were written using NetBeans and this tutorial assumes its use.
| |
| Other environments such as Eclipse work as well and would be set up in a similar manner.
| |
| For Java command line compilers, include the phidget21.jar during compilation.
| |
| For example, you can use “javac -classpath phidget21.jar MyPhidgetProgram.java” from the Java SDK.
| |
| In NetBeans, the Phidget .jar library can be added to the project from the project explorer window.
| |
| Simply right click the “Libraries” item in the project explorer and then select “Add JAR/Folder”.
| |
| Navigate to the location where the phidget21.jar was extracted and then add it to the project.
| |
| You are now ready to begin coding with Phidgets.
| |
| | |
| ===Coding For Your Phidget===
| |
| | |
| Before you can use the Phidget, you must include a reference in the code to the library. In Java:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;"> | |
| <font size="3">
| |
| <source lang=java>
| |
| | |
| import com.phidgets.*;
| |
| import com.phidgets.event.*;
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| Now in the main body of code, the Phidget object will need to be declared.
| |
| For example, we can declare a PhidgetInterfaceKit with:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=java>
| |
| | |
| static InterfaceKitPhidget ik;
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| The object name for any type of Phidget is listed in the API manual.
| |
| Every type of Phidget also inherits functionality from the Phidget base class.
| |
| All Phidgets can throw PhidgetExceptions if something unexpected happens during operation.
| |
| Make sure to catch or declare the exception, even in a generic way.
| |
| This is what the main function looks like where we initialize the Phidget object without any constructors:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=java>
| |
| | |
| public static final void main(String args[]) throws Exception
| |
| {
| |
| ik = new InterfaceKitPhidget();
| |
| }
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| ===Connecting to the Phidget===
| |
| | |
| 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> | | </div> |
|
| |
|
| ===Event Driven Programming=== | | == Quick Downloads == |
| | |
| 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===
| |
| | |
| Multiple Phidgets of the same type can easily be run inside the same program.
| |
| In our case, it requires another PhidgetInterfaceKit instance to be defined and initialized.
| |
| The new instance can then be set up, opened and used in the same process as the previous one.
| |
| If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.
| |
| | |
| ===Other Phidgets===
| |
| | |
| The design given in this document can also be followed for almost all Phidgets.
| |
| For example, if you were using a PhidgetRFID instead of an PhidgetInterfaceKit, you would declare an RFID object instead of an InterfaceKit object.
| |
| The methods and events available would change but they can be accessed in a similar manner.
| |
| | |
| ===Compiling a .jar File===
| |
| | |
| Finally, when the project is completed it is recommend to compile the project as a .jar.
| |
| This will reduce the number of extra flies created into a single package that is easier to manage.
| |
| In NetBeans a .jar file is automatically created during compilation.
| |
| Under the command line, you can use the jar utility from the Java SDK to package the .class files.
| |
| For example: “jar -cf MyProgram.jar *.class”. You will also need to provide a Manifest file for the jar program to indicate the program entry point via the Main-Class and the Class-Path which will point to the phidget21.jar.
| |
| Simply create a file, for example MyProgram.mf, and enter the following lines:
| |
|
| |
|
| <font size="3">
| | If you already know what you're doing and just need the files, you can find them all below. |
|
| |
|
| Manifest-Version: 1.0
| | === Documentation === |
| Class-Path: phidget21.jar
| |
| Main-Class: MyProgram
| |
|
| |
|
| | *{{Phidget22API}} (select Java from the drop-down menu) |
|
| |
|
| </font>
| | === Example Code === |
|
| |
|
| Please note that the manifest file must end with a new line or a carriage return.
| | *{{SampleCode|Java|Java Examples}} |
| Now run the jar utility to package the files. For example: “jar –cfm MyProgram.jar MyProgram.mf *.class”.
| |
|
| |
|
| ==Building your Project== | | ===Libraries=== |
| Describe the different ways a project could be built using this language.
| |
|
| |
|
| ==Common Problems and Solutions/Workarounds==
| | *[https://cdn.phidgets.com/downloads/phidget22/libraries/any/Phidget22Java.zip phidget22.jar] |
| Here you can put various frequent problems and our recommended solutions.
| | {{AllQuickDownloads}} |