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 - iOS

From Phidgets Legacy Support
Revision as of 19:33, 11 April 2012 by Mphi (talk | contribs)

Icon-iOS.png application are built using the Cocoa Touch framework and the Objective C programming language, both developed by Apple.


Introduction

If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use iOS specifically.

iOS is capable of using nearly the complete Phidget API, including events. We also provide example code in iOS for the Phidget InterfaceKit, Hello World examples and a Skeleton starter project.

iOS can be developed with Xcode on OS X..

You can compare iOS with our other supported languages.

Quick Downloads

Just need the iOS documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers


Getting Started with Cocoa Touch and Objective C

If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:

  • Make sure your libraries are properly linked
  • Go from source code to a test application as quickly as possible
  • Ensure your Phidget is hooked up properly

As iOS development is primarily done on xCode, we provide instructions for xCode, specifically version 4.0.

For basic differences on installing and setting up Eclipse on your development system, you can try the iOS 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 Eclipse support page or the Internet at large.

iOS devices can control a Phidget remotely over a network, by using the Phidget Webservice

Use Our Examples

Download and unpack the Phidget Examples for iOS. There are two Hello World examples: HelloWorldRemote (the one you want), and HelloWorld (for tablets with a USB port).

Import the HelloWorldRemote example into Eclipse:

  • File → Import... → General → Existing Projects Into Workspace → (Next)
  • Select HelloWorldRemote root directory → Select all files → Finish

Next, 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. This happens on the line that says something like:

device.open("192.168.3.133", 5001);

Also, note that the HelloWorldRemote example was written for iOS version 3.0 or greater. If needed, you can change this in: (Right Click on Project) → Properties → Resource (on left) → Android

To run the example: Right-click on project in Package Explorer (To open the Package Explorer, 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. You should run it directly on your Android device, unless you are comfortable setting up the emulator to run on your local network.

After the HelloWorldRemote application starts running on your Android device:

  1. Make sure the Phidget Webservice is running on the computer with the IP address you put into the code
  2. Plug a Phidget into that computer

And on your Android device, depending on which Phidget you plug in, you should see something like:

And when you unplug that Phidget from the remote computer, you should see something like this on your Android device:

If you have the specific Phidget for the other included Android examples (i.e. the InterfaceKit), you can run the other examples in the same way.

To envision the system in the background, you can refer to the Webservice section on the OS - Android page.

Write Your Own Code

To write your own code from scratch, start with an Android project in Eclipse (File → New → Android Project). Then....

Step One: Link the Phidget Libraries to your Project

When you download and unzip the Phidget Examples for Android, each project folder will (in addition to the project files themselves) contain three things:

  1. A libs/ folder (containing an armabi/ folder and a single file called libphidget21.so)
  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)

Copy the libs folder, the PhidgetsUSB.jar file, and the phidget21.jar file to your project folder.

To install libraries to run a Phidget remotely over a network using the Webservice:

  1. Add the libs/ folder to your project
    • To add the libs/ folder in Eclipse, simply place it in the root folder of your project
  2. Add the phidget21.jar file to your project
    • In Eclipse use the top-bar menu: Project → Properties → Java Build Path → Libraries (tab) → Add JAR
Step Two: Give your Program Permission to use the Internet

Add the following to your AndroidManifest.xml file:

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
Step Three: Include the Phidget Libraries in your Source

Then, in your code, include an import reference to the library. In Xcode:

  // This contains all of the devices and the exceptions
  import com.phidgets.*;
 
  // This contains all of the event listeners
  import com.phidgets.event.*;

Then you are ready to begin coding!

Follow the Examples

By following the instructions above, you probably now have a working example and want to understand it better so you can change it to do what you want. This section has resources for you to learn from the examples and write your own.

Programming with Phidgets in MATLAB makes extensive use of the mainstream Java Phidgets library, so the Java API reference will be helpful:

  • Java API (This is the complete set of functions you have available for all Phidgets)
  • Device Specific APIs - The one for your Phidget can be found in its user guide.

To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, although you can only use the event code in Android.

API Support

Most of the C API is supported in iOS, with the exception of the local open calls. This is because there are no USB ports on iOS devices, so only the remote open calls can be used.

The following open calls are unsupported:

int CPhidget_open(CPhidgetHandle phid, int serialNumber)
int CPhidget_openLabel(CPhidgetHandle phid, const char * label)

Code Snippets

Specific calls in iOS will differ in syntax from those on the General Phidget Programming page, but the concepts stay the same.

It may help to have the General Phidget Programming page and this section open at the same time, because they parallel each other and you can refer to the Android Java syntax. However, many additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.

For example, if we were using a Phidget Interface Kit as our device, the general calls would look like this:

Step One: Initialize and Open

For opening a remote Phidget over the network using the Webservice:

  InterfaceKitPhidget device;
  device = new InterfaceKitPhidget();
 
  // Open first detected Interface Kit, remotely with IP address and port
  device.open("192.168.3.33", 5001);

For opening a Phidget directly attached to the tablet's USB port:

 InterfaceKitPhidget device;
 com.phidgets.usb.Manager.Initialize(this);
 device = new InterfaceKitPhidget();

 // Open locally, with Phidget in tablet USB port
 ik.open("192.168.3.33", 5001);

The direct open call 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.

Both open calls 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....

Step Two: Wait for Attachment (plugging in) of the Phidget

To use the Phidget, it must be plugged in and a software event caught (i.e. attached).

Android Java is only event-driven, so you cannot use waitForAttachment() without hanging and being relatively unsafe with your threads. Instead, you should define an event handler function that you can then synchronize and tie in with the attachment event itself.

First, let's write our handler:

  class AttachEventHandler implements Runnable { 
    Phidget device; 
    TextView eventOutput;

    public AttachEventHandler(Phidget device, TextView eventOutput) {
      this.device = device;
      this.eventOutput = eventOutput; }

    public void run() {
      try {
        // The actual useful thing our handler does
	eventOutput.setText("Hello " + device.getDeviceName() + ", Serial " + Integer.toString(device.getSerialNumber()));
      } catch (PhidgetException e) { e.printStackTrace(); }
        
      // Notify whoever called us (and is waiting) that we're done
      synchronized(this) { this.notify(); }
    }
  }

This may seem complex, but really it is just:

  1. A class wrapper so we can work independently once we get permission from the main thread, and
  2. A way to call back to the synchronized() call to let them know we're done and stay thread safe

Now that we have our 'handler' we can hook it in as an event function to trigger on device attachment:

device.addAttachListener(new AttachListener() {
  public void attached(final AttachEvent attachEvent) {
    AttachEventHandler handler = new AttachEventHandler(attachEvent.getSource(), eventOutput);

    // This is synchronised in case more than one device is attached before one completes attaching
    synchronized(handler) {

      runOnUiThread(handler);
      try { handler.wait(); } catch (InterruptedException e) { e.printStackTrace(); }
    }
  }
});

Step Three: Do Things with the Phidget

Again, because Android is event driven, use buttons (or timers) to schedule events if you want to poll the device at a certain interval or user specification. Otherwise, simply set the sensitivity and/or data rate (depending on your device type) and catch events as they come in using the handler structure above.

Step Four: Close and Delete

At the end of your program, don’t forget to call close to free any locks on the Phidget.

We can put the close() call for Phidgets in Android within an overridden version of the onDestroy() Android application function. Within it, we simply close the device. For Phidgets directly attached to a USB Android tablet, you should also call Uninitialize():

  @Override
  protected void onDestroy() {
    super.onDestroy();
      try { device.close(); } catch (PhidgetException e) { e.printStackTrace(); }
      // Uninitialize should only be called for directly connected Phidgets
      com.phidgets.usb.Manager.Uninitialize();
  }

The complete set of functions you have available for all Phidgets can be found in the Java API. You can also find more description on any device-specific function either in the Device API page for calls available only on your specific Phidget.

Common Problems and Solutions/Workarounds