Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Language - Android Java: Difference between revisions

From Phidgets Legacy Support
Line 7: Line 7:
==Support==
==Support==


Phidgets can run directly plugged in to Android devices with a USB port and system 3.1 or later.  Otherwise, Android can serve as a driver for a Phidget via the [[Phidget Webservice]].
Phidgets can run directly plugged in to Android devices with a USB port and system 3.1 or later.  Otherwise, Android control a Phidget remotely using the [[Phidget Webservice]].


In addition to Android-specific examples for Phidgets, the more general [[Language - Java | Java]] documentation has further examples on running Phidgets.
In addition to Android-specific examples for Phidgets, the more general [[Language - Java | Java]] documentation has further examples on running Phidgets.

Revision as of 19:30, 31 October 2011

Preamble about the language and its general strengths and weaknesses.

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 control a Phidget remotely using the Phidget Webservice.

In addition to Android-specific examples for Phidgets, the more general Java documentation has further examples on running Phidgets.

For using Android to control Phidgets remotely, the Software Overview page gives information on the operating systems that can directly support a Phidget and allow access to it via the Phidget Webservice.

Restrictions

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

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.

Drivers, Libraries and Resources

Getting Started

Coding For Your Phidget

Before you can use the Phidget, you must include a reference in the code to the library. In Java:

  import com.phidgets.*;
  import com.phidgets.event.*;


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.

  ik.openAny();
  ik.waitForAttachment();

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.

  ik.close();
  ik = null;

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:

  ik.addSensorChangeListener(new SensorChangeListener()
  {
      public void sensorChanged(SensorChangeEvent se)
      {
        //Insert your code here
        System.out.println(se.getValue());
      }
  });

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

Here you can put various frequent problems and our recommended solutions.