Developing Applications with the Java APIs for Bluetooth (JSR-82)
Developing Applications with the Java APIs for Bluetooth (JSR-82) This paper covers the Java API for Bluetooth (JSR-82) with respect to Sony Ericsson devices. It starts by introducing the Bluetooth technology, followed by the Java APIs for Bluetooth, and how to use them. Currently, these APIs are currently available in the Sony Ericsson P900/P908 handsets. […]
Multi-Player MIDP Game Programming
Multi-Player MIDP Game Programming This tutorial explains how to write multi-player games for MIDP phones. It describes the technologies that are available to support multi-player games, and shows what kinds of games are possible with these technologies. Click here to view “Multi-Player MIDP Game Programming”
j2me:Socket connection
/*J2ME: The Complete ReferenceJames KeoghPublisher: McGraw-HillISBN 0072227109*/// jad file (Please verify the jar size first)/*MIDlet-Name: socketconnectionMIDlet-Version: 1.0MIDlet-Vendor: MyCompanyMIDlet-Jar-URL: socketconnection.jarMIDlet-1: socketconnection, , socketconnectionMicroEdition-Configuration: CLDC-1.0MicroEdition-Profile: MIDP-1.0MIDlet-JAR-SIZE: 100*/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.io.*;import javax.microedition.io.*;public class socketconnection extends MIDlet implements CommandListener { private Command exit, start; private Display display; private Form form; public socketconnection () { display = Display.getDisplay(this); exit = new Command(“Exit”, Command.EXIT, 1); start = new Command(“Start”, Command.EXIT, 1); form = new Form(“Read Write Socket”); form.addCommand(exit); form.addCommand(start); form.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(false); notifyDestroyed(); } else if (command == start) { try { StreamConnection connection = (StreamConnection) Connector.open(“socket://www.myserver.com:80”); PrintStream output = new PrintStream(connection.openOutputStream() ); output.println( “GET /my.html HTTP/0.9\n\n” ); output.flush(); InputStream in = connection.openInputStream(); int ch; while( ( ch = in.read() ) != –1 ) { System.out.print( (char) ch ); } in.close(); output.close(); connection.close(); } catch( ConnectionNotFoundException error ) { Alert alert = new Alert( “Error”, “Cannot access socket.”, null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.ERROR); display.setCurrent(alert); } catch( IOException error ) { Alert alert = new Alert(“Error”, error.toString(), null, null); alert.setTimeout(Alert.FOREVER); alert.setType(AlertType.ERROR); display.setCurrent(alert); } } }}
Demonstrates the functionality of DatagramConnection framework.
/*** Chapter 5 Sample Code for Datagram functionality ***/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.io.*;import java.util.*;public class DatagramTest extends MIDlet { // Port 9001 is used for datagram communication static final int receiveport = 9001; Receive receiveThread = new Receive(); public DatagramTest() { } public void startApp() { // Start the listening thread receiveThread.start(); // Send message Hello World! sendMessage(“Hello World!”); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } // This function sends a datagram message on port 9001. void sendMessage(String msg) { String destAddr = “datagram://localhost:” + receiveport; DatagramConnection dc = null; Datagram dgram; byte[] bMsg = msg.getBytes(); try { dc = (DatagramConnection)Connector.open(destAddr); // Create a datagram socket and send dgram= dc.newDatagram(bMsg,bMsg.length,destAddr); dc.send(dgram); System.out.println(“Sending Packet:” + msg); dc.close(); } catch (Exception e) { System.out.println(“Exception Connecting: ” + e.getMessage()); } finally { if (dc != null) { try { dc.close(); } catch (Exception e) { System.out.println(“Exception Closing: ” + e.getMessage()); } } } } // This function is a listener. It waits to receive datagram packets on 9001 port class Receive extends Thread { public void run() { doReceive(); } void doReceive() { DatagramConnection dc = null; Datagram dgram; try { // Open Server side datagram connection dc = (DatagramConnection)Connector.open(“datagram://:”+receiveport); String receivedMsg; while (true) { dgram = dc.newDatagram(dc.getMaximumLength()); try { dc.receive(dgram); } catch (Exception e) { System.out.println(“Exception in receiving message:” + e.getMessage()); } receivedMsg = new String(dgram.getData(), 0,dgram.getLength()); System.out.println(“Received Message: ” + receivedMsg); try { Thread.sleep(500); } catch (Exception e) { System.out.println(“Exception doReceive(): ” + e.getMessage()); } } } catch (Exception e) { System.out.println(“Exception doReceive(): ” + e.getMessage()); } finally { if (dc != null) { try { dc.close(); } catch (Exception e) { System.out.println(“Exception Closing: ” + e.getMessage()); } } } } } }
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 1)
Writing games for small devices is easy and fun with the new MIDP 2.0 API. The biggest difference for the game developer between MIDP 1.0 and MIDP 2.0 is enhanced graphics capabilities. With MIDP 1.0 you can certainly write some fun games–those of us who remember the Atari 2600 will remember that the games were […]
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 2)
The MIDlet Class Now it’s time to get started on the real game. If you copy the code from this section and all of the following sections, it should compile together to form the example game “Tumbleweed.” The MIDlet class controls some of the basic items that are common to all MIDlets (such as start […]
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 3)
The GameCanvas Class The GameCanvas class represents the area of the screen that the device has allotted to your game. The javax.microedition.lcdui.game.GameCanvas class differs from its superclass javax.microedition.lcdui.Canvas in two important ways: graphics buffering and the ability to query key states. Both of these changes give the game developer enhanced control over precisely when the […]
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 4)
Now that I’ve discussed the special functions of a GameCanvas, I’d like to go over the main function of a Canvas in general, namely painting the screen. This usually takes place in the method paint(Graphics g) which you can override. The Graphics object can be queried for screen dimensions and then can be used to […]
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 5)
The Sprite Class A Sprite is a graphical object represented by one image (at a time). The fact that a Sprite is composed of only one image is the principal difference between a Sprite and a TiledLayer, which is a region that is covered with images that can be manipulated. (The Sprite class has a […]
MIDP 2.0 Games: a Step-by-Step Tutorial with Code Samples (Step 6)
And here’s the code for Tumbleweed.java: package net.frog_parrot.jump; import java.util.Random; import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*; /** * This class represents the tumbleweeds that the player * must jump over. * * @author Carol Hamer */public class Tumbleweed extends Sprite { //——————————————————— // dimension fields /** * The width of the tumbleweed’s bounding square. */ static int WIDTH = 16; […]