Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to use USB cable connection in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code Example
Source file:
Tested with
Devices(s): Nokia N97
Created: jarmlaht (11 Nov 2009)
Last edited: hamishwillee (24 Jul 2013)
Featured Article
07 Feb

This article explains how to use the Java ME CommConnection to create a USB connection over cable in Nokia devices. Note that this is an update of the Nokia Developer document MIDP: Using Cable Connection In Nokia Devices v1.0.

There are differences in devices and their port numbers, and also on PC side. In practice using USB connection between PC and mobile device might need some testing.


How to create CommConnection

After creation of the Nokia Developer document there have been changes in connecting phone to PC. In older models CommConnection was created for example like this:

//Opens a connection on port "com0".
CommConnection comm = (CommConnection)"comm:com0");

In the newer devices the connection is opened for example like this:

//Opens a connection on port "USB2". Defines USB interface
CommConnection comm = (CommConnection)"comm:USB2");

The example MIDlet has been tested in Nokia N97 device, which is S60 5th Edition device. There might be differences in the port names in different devices. Ports can be queried from the system with:

String ports = System.getProperty("microedition.commports");

In case of N97 the system property returns a long list of COM, IR, USB and BT ports. There are two USB ports: USB1 and USB2. If USB1 is used in method, IOException is thrown with message "Access denied". The connection can be successfully opened when USB2 is used. Note also that if some port is in other use (other applications in the device, system usage), it cannot be accessed through CommConnection although it is listed by the system property.


Here are some practical instructions for creating CommConnection between Nokia N97 and PC:

  • For simple testing HyperTerminal (included with most of the Windows versions) or similar programs can be used
  • Connect your phone by using the USB cable (model CA-101 in case of N97) with the PC
    • Select "PC Suite" as USB mode
  • Start the HyperTerminal application and create a new connection
    • Change "Connect using:" field to have correct COM port (in this case COM5 was used)
    • The correct port can be found from Windows Control Panel -> System -> Device Manager -> Ports (COM & LPT)
    • There is no need to change the port settings, accept them -> connection is made between the PC and the phone
    • It might be a good idea to set "Echo typed characters locally" on in HyperTerminal from here: File -> Properties -> Settings -> "Ascii Setup..."

Connection creation in HyperTerminal Selecting the correct COM port in HyperTerminal

  • Install the USBTest MIDlet to your phone and launch it
    • When MIDlet is started, it automatically opens the connection to PC
    • You should see "Hello from USBTest MIDlet! Anybody there at USB?" message on the HyperTerminal screen
    • Select "Start" command for starting the USB port listening
  • Now write something in HyperTerminal window and press enter
    • The message should be shown on the phone's screen

Message from the phone has arrived and response has been sent

USBTest MIDlet

The USBTest MIDlet demonstrates, how a MIDlet can send and receive data between a phone and a PC by using USB cable and standard MIDP CommConnection. The whole NetBeans project with the MIDlet jad and jar files and the source code are available below. Two most important methods are also shown below. The openUSBConnection() method opens the CommConnection to the PC and listenUSB() method takes care of listening the data traffic.

The image below shows the MIDlet screen in Nokia N97 device after it has received a message from the connected PC.

USBTest MIDlet running in Nokia N97

Source code: openUSBConnection() method in

* This method checks by using the system property "microedition.commports", that
* there is a USB port in the device. If found, CommConnection is opened to
* remote device (PC) by using the USB port. A message is sent to the remote
* device for indicating the successful connection.

public void openUSBConnection() {
try {
String ports = System.getProperty("microedition.commports");
Tracer.outln("got ports="+ports, textbox);
int index = ports.indexOf("USB", 0);
if(index == -1) {
throw new RuntimeException("No USB port found in the device");
comm = (CommConnection)"comm:USB2");
int orgBaudRate = comm.getBaudRate();
os = comm.openOutputStream();
String text = "Hello from USBTest MIDlet! Anybody there at USB?\r\n";
is = comm.openInputStream();
} catch (IOException e) {
Tracer.outln("IOException: " + e.getMessage(), textbox);

Source code: listenUSB() method in

* This method listens to CommConnection traffic. When a message (a line of
* text with enter pressed in the end) is received, it is shown in the TextBox.
* If the message is "EXIT + enter key", the streams and the connection are
* closed.

public void listenUSB() {
Tracer.outln("Listening USB port...", textbox);
try {
byte[] buffer = new byte[500];
StringBuffer message = new StringBuffer();
for(int i = 0; i < 500;) {
try {
catch(InterruptedException ie) { }
int available = is.available();
if(available == 0) {
String outText = "";
int count =, i, available);
if(count > 0) {
outText = new String(buffer, i, count);
i = i + count;
if (outText.endsWith("\n")) {
String messageString = message.toString();
Tracer.outln("Message: " + messageString, textbox);
message.delete(0, message.length());
String total = new String(buffer,0,i);
if ((i > 3) && (-1 != total.indexOf("EXIT\r\n"))) {
Tracer.outln("Closing...", textbox);
catch (IOException ioe) {
Tracer.outln("IOException: " + ioe.getMessage(), textbox);
Tracer.outln("SUCCEEDED.", textbox);

Example application

See also

This page was last modified on 24 July 2013, at 04:59.
166 page views in the last 30 days.