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

OS - Android

From Phidgets Legacy Support
Revision as of 17:12, 3 April 2012 by Cora (talk | contribs)


Icon-Android.png Android is a mobile OS commonly used on smartphones and tablet computers.

Tablets with a USB port and Android version 3.1 or greater can control Phidgets directly plugged in to them. Earlier Android versions (tested down to 2.1) can control Phidgets over a network using the Webservice. We do not currently support Android devices acting as a Phidget Webservice server, but if you are looking for a compact and cheaper-than-a-tablet way to host Phidgets over a network, take a look at our Single Board Computer.

Getting Started (Libraries and Drivers)

If this is your first Phidget, we highly recommend working through the Getting Started guide for your specific Phidget device.

Android code is developed on an external platform anyway (i.e. Windows, Mac OS, or Linux), and so getting your Phidget to work locally on that platform first will help you distinguish any issues from network ones later.

Near the end of the Windows, Mac OS, or Linux setup process, we direct you to choose a language. At that point, please remember to return here to this Android Java page.

Alternately, you can first try using mainstream Java. On the mainstream Java page, we provide example code - including code that works on the Android development platform of Eclipse - to test your Phidget directly from your development computer.

Installing

Eclipse (Android Java Development Platform)

Development for your Android OS Phidget application can occur on Linux, Mac OSX, or Windows. We recommend using Eclipse so you can run our provided Phidget examples.

You will need the following:

  1. The JDK and Java on your development system
    • See the mainstream Java page for details on Java for Windows, Mac, and Linux
  2. Eclipse (a Java Integrated Development Environment) on your development system
    • http://www.eclipse.org/downloads/ (for Windows or MacOS)
    • sudo apt-get install eclipse (for Linux)
    • For 64-bit Linux, you will need the ia32-libs package as well, try:
      sudo apt-get install ia32-libs

Android SDK (and ADT Eclipse Plugin)

After installing Java and Eclipse, you can install the Software Developer's Toolkit (SDK) and the plugin for Eclipse (ADT).

  1. Download and install the Android SDK package for your development system:
  2. Download and install the ADT Eclipse Plugin for Android

To check that the JDK, the Android SDK, and Eclipse have all been configured correctly, use the Google HelloAndroid example:

http://developer.android.com/resources/tutorials/hello-world.html

Once you have confirmed that the Android SDK has been correctly installed, you are ready to begin developing applications with the Android Java libraries and language.

Android SDK and ADT on Linux

Run the Android SDK manager. If android-sdk-linux is the unpacked directory that was downloaded, try:

android-sdk-linux/tools/android

This will give you the option to download the Android versions you want to support. Running the SDK from within Eclipse (WindowAndroid SDK) will let you add those Android versions to Eclipse, for emulating them before downloading to an Android device and using with Phidgets.

Then, to add Android support to Eclipse, first make sure adb is in your path (e.g. by adding it to /etc/environment

Phidget Libraries

The library phidget21.jar from the Android examples (not the mainstream Java examples, despite the same name) are the libraries for including with your Android code. You can download the examples and extract it specifically if needed:

When you download and unzip the Phidget Library for Android, it will contain three things:

  1. A libs/ folder
  2. A jar file containing the general Phidget java library (phidget21.jar)
  3. A jar file for directly driving USB devices from a USB port on the Android device (PhidgetsUSB.jar)

Feel free to browse around to get a sense of what will be going on the Android OS side. We describe how to link and use this library later on the Write your own Android code section of the Android Java page.

First, though, it will be useful to check to make sure Phidgets work with your Android system.


Checking

When you run an Phidgets Android example, you transfer and link the libraries and code all at the same time. This should 'just work' with our examples, but if problems arise this section gives more detail on pinpointing the source of the problem. We recommend starting with running the software examples right away - if the software works, you know the hardware works too.

Software

The easiest way to see whether your libraries are set up correctly within our examples or your own project is just to download them to the Android device and run them. Detailed instructions for this (including choosing the right HelloWorld project to run) are on the Language - Android Java page. If the examples do not run using the instructions, return here to debug your hardware.

Hardware

If you are having problems running the examples, you should check the hardware of the host computer if your Android device is controlling the Phidget over the Webservice. Or, you should check the hardware of your Android device if the Phidget is directly connected to the tablet's USB port.

Remote Phidget

When using the Webservice to control a Phidget, the problem may be with the USB connection on the remote computer. Make sure both the server-side of the webservice and the USB connection are working by using the instructions on the page specific to the operating system

Directly Connected Phidget

To check whether a Phidget is correctly connecting in hardware (even if our examples do not work), you can use the kernel logs on your Android tablet in a similar way as you would on Linux.

You need to access the logs from your debugging computer - e.g. the computer where you have installed Google's ADB as described above. Plug a USB Phidget into your Android device. Then, as soon as possible, from a command line on that computer, use the ADB (Android Debugging Bridge) program to access the program dmesg on your Android device:

adb shell dmesg

This will print out the entirety of the kernel logs on your Android device. Even if you have been running your device for only a little while, this could be thousands of lines of output. If you plugged the Phidget in and then ran dmesg right away, the kernel detection of the Phidget should be almost at the bottom of the logs. If you are on Linux or Mac OS, you can mitigate the length of this output using tail -n 50 or so. If you are on Windows, you may consider using a command redirection operator like > to write to a file and then view the last 50 or so lines.

Near the end of the output you will find something like this for a successful registration of a Phidget in the Android kernel:

<6>[ 3282.554249] usb 1-1.2: new full speed USB device using tegra-ehci and address 5
<6>[ 3282.612359] usb 1-1.2: New USB device found, idVendor=06c2, idProduct=0032
<6>[ 3282.612418] usb 1-1.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
<6>[ 3282.612466] usb 1-1.2: Product: PhidgetTemperatureSensor
<6>[ 3282.612504] usb 1-1.2: Manufacturer: Phidgets Inc.
<6>[ 3282.612540] usb 1-1.2: SerialNumber: 287638
<4>[ 3282.612576] device: '1-1.2': device_add
<4>[ 3282.615639] device: '1-1.2:1.0': device_add
<4>[ 3282.618632] device: '0003:06C2:0032.0003': device_add
<3>[ 3282.631412] generic-usb 0003:06C2:0032.0003: claimed by neither input, hiddev nor hidraw

If the kernel log attachment does not appear, make sure that other devices that are supposed to work with your tablet (USB data keys, keyboards, etc. - the list depending entirely on which tablet) do indeed work. Not having the Phidget appear in your kernel logs indicates there is a problem with your USB hardware on your Android device.

Note that in order to consider a port on your tablet to be a USB Port it must be able to host USB devices. Simply finding and using a USB cable to connect the Phidget to the power charging port on your phone or tablet is not enough! A host USB port should either say so explicitly, or at least act like a 'normal' USB port such as ones you would find on your PC, and be able to use many common USB devices like memory sticks.

If the kernel log attachment does appear, but your code doesn't run, the problem is probably with your code. Work through the Language - Android Java page to make sure you have all the libraries, jar files, and permissions set and linked properly to use your Phidget.

Troubleshooting

Programming Languages

Android OS programs are written in Android Java using the Android SDK and the Phidgets library.

Note that Android Java is NOT the same as mainstream Java. Any Java programs you have will probably need significant modification before they run on Android, including our mainstream Java Phidget Examples

Webservice

Setting Up the Webservice

Using the Webservice

Advanced Uses

Common Problems and Solutions

If you are having trouble, ensure your Phidget libraries are up to date.