You are on page 1of 20

P TRNH TRN THI T BI DI

NG

1. Gi i thi u Java v ng d ng l p trnh trn thi t b di ng


1.1 Gi i thi u v Java
Vo nh ng n m 1990, Java
c ra i t d n xanh v ban u
c xy d ng
kii m
sat cc th b dn d ng v m t s thi t b c m tay. Java
c xy d ng ch y u d a trong b
cng c pht tri n (Java Development Kit JDK) nh l th vi n chu n trong ch a trnh
bin d ch, trnh thng d ch, y chnh l n n t ng cho vi c pht tri n ng d ng Java. Hi n
nay cc nh pht tri n xy d ng nhi u nhnh m i cho Java nh :
J2EE ( Java 2 Platform Enterprise Edition): Phin b n dnh cho my ch l n v i s c
nh x l v dung l ng b nh l n.
J2SE ( Java 2 Platform Standard Edition): Phin b n chu n ch y trn cc my PC v
laptop.
J2ME ( Java 2 Platform Micro Edition): l phin b n rt g n c a Java cho cc thi t di
ng gi i h n v b nh v b x l.
1.2

ng d ng l p trnh trn thi t b di ng b ng J2ME


c tiu c a c a J2ME l cho php ng i l p trnh vi t cc ch ng trnh ng d ng ng l p
i thi t b di ng, khng c n quan tm n ph n c ng. J2ME
c xy d ng b ng cc t ng
khc nhau gi u i vi c th c hi n ph n c ng kh i nh pht tri n. Sau y l cc t ng c a
j2ME
c xy d ng trn CLDC:
Hi n tr ng
MIDP- Mobile
Information
DeviceProfile

Cc API khc

u hnh
CLCD-( Connected Limited Device Configuration)
My o Java
Ph n c ng thi t b
ng ph n c ng thi t b : y l thi t b di ng v i c u hnh ph n c ng c a n nh b nh ,
c
x l. Cc thi t b di ng khc nhau c th c cc b x l khc nhau v i cc m l nh khc
nhau. M c tiu c a J2ME l cung c p m t chu n cho t t c cc l ai thi t b di ng khc nhau.
ng my o Java: Khi m ngu n Java
c bin d ch n
c chuy n i thnh m
bytecode. M bytecode ny sau
c chuy n thnh m ngn ng my c a thi t b di ng. T ng
my o java bao g m KVM ( K virtual Machine) l b bin d ch m bytecode c nhi m v chuy n
m bytecode c a ch ng trnh Java thnh ngn ng my ch y trn thi t b di ng. t ng ny cung
p m t s chu n ha cho cc thi t b di ng
ng d ng J2ME sau khi bin d ch c th h at
ng trn b t k thi t b di ng no c J2ME KVM.

ng c u hnh : ng c u hnh c a CLDC nh ngh a giao di n ngn ng Java c b n


cho php ch ng trnh java ch y trn thi t b di ng. y l m t t p cc API nh ngh a li c a
ngn ng J2ME . Ng i l p trnh c th s d ng cc l p v ph ng th c c a cc API ny tuy nhin
cc API h u d ng h n
c ch a trong t ng hi n tr ng.
ng hi n tr ng: T ng ny cung c p cc API h u d ng h n cho ng i l p trnh. M c ch
a hi n tr ng l xy trn l p c u hnh v cung c p nhi u th vi n ng d ng h n. MIDP nh ngh a
cc API ring bi t cho thi t b di ng. C ng c th c cc hi n tr ng v cc API khc ngai MIDP
c dng cho ng d ng, c ng c th c m t hi n tr ng nh ngh a cc API cho vi c t o cc ng
ng Bluetooth.
Hi n nay, MIDP c hai phin b n l MIDP 1.0 v MIDP 2.0 v cung c p:
- Cc l p v cc ki u d li u
tr
i t ng Display
tr Form v giao di n ng i dng
tr Timer v Alert
tr Record Management System( RMS) cho vi c l u tr d li u.
Cc tnh n ng n i b t c a MIDP 2.0 so v i MIDP 1.0:
- Nng c p tnh n ng b o m t, h tr giao th c HTTPS, ki m sat vi c k t n i my di
i server.
- Thm cc API h tr Multimedia.
r ng cc tnh n ng c a Form

ng

2. MIDlet
Cc ng d ng J2ME
c g i l MIDlet ( Mobile Information Device Applet). MIDlet l m t
p java m r ng c a l p tr u t ng javax.microedition.midlet.MIDlet v th c thi cc ph ng th c
startApp(), pauseApp(), v destroyApp().
an ch

ng trnh t i thi u c a MIDlet:

import javax.microedition.midlet.*;
public class exMIDlet extend MIDlet {
public exMIDlet( ) { }
public void startApp( ) { }
public void pauseApp( ) { }
public void destroyApp( boolean unconditional ) { }
}
Trong :
o Ph ng th c exMIDlet l hm constructor .
o Ph ng th c startApp( ) :
c g i b i b qu n l ng d ng khi MIDlet
c
kh i t o hay khi MIDlet tr v tr ng thi t m d ng.
o Ph ng th c pauseApp( ) :
c g i b i b qu n l ng d ng m i khi ng d ng
n
c t m d ng.
o Ph ng th c destroyApp( ):
c g i khi thoat kh i MIDlet.
3.

h a trong J2ME
Cc l p MIDP cung c p hai m c
cao dng cho v n b n hay form.

h a:
h a m c th p v
h a m c cao.
h am c
h a m c th p dng cho cc ng d ng games.

3.1

h a m c cao
h a m c cao l l p con c a l p Screen. N cung c p cc thnh ph n nh text box, form,
list v alert.
3.1.1 TextBox
p Textbox cho php ng i dng nh p v s an th o v n b n. Ng i l p trnh c th
nh ngh a s k t t i a, gi i h n d li u nh p ( s , m t kh u, imail,) v hi u ch nh
i dung c a textbox.
package Edu.TextBox;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import
javax.microedition.midlet.MIDletStateChangeException;
public class TextBoxMIDlet extends MIDlet implements
CommandListener {
private Display display;
private TextBox textBox;
private Command quitCommand;
public TextBoxMIDlet() {
}
public void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
protected void startApp() throws
MIDletStateChangeException {
display = Display.getDisplay(this);
quitCommand = new
Command("That",Command.SCREEN,1);
textBox=new TextBox("Soan Thao:","",100,0);
textBox.addCommand(quitCommand);
textBox.setCommandListener(this);
display.setCurrent(textBox);
}
public void commandAction(Command choice, Displayable
displayable) {
if(choice==quitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}

3.1.2 Form
Form l l p h u d ng nh t c a cc l p Sreen b i v n cho php ch a nhi u item trn
cng m t mn hnh. Cc item c th l datafield, TextField, ImageItem.

package Edu.Form;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import
javax.microedition.midlet.MIDletStateChangeExceptio
n;
public class FormMIDlet extends MIDlet implements
CommandListener {
private Display display;
private Form fr;
private Command themCommand;
private Command quitCommand;
public FormMIDlet() {
}
public void destroyApp(boolean unconditional)
{
}
protected void pauseApp() {
}
protected void startApp() throws
MIDletStateChangeException {
display = Display.getDisplay(this);
quitCommand = new
Command("That",Command.SCREEN,1);
themCommand = new
Command("Thm",Command.BACK,1);
fr=new Form("Form: ");
fr.append("Muc 1");
fr.addCommand(quitCommand);
fr.addCommand(themCommand);
fr.setCommandListener(this);
display.setCurrent(fr);
}
public void commandAction(Command choice,
Displayable displayable) {
if(choice==quitCommand){
destroyApp(false);
notifyDestroyed();
}
if(choice==themCommand){
fr.append("\nMuc moi");
}
}
}

3.1.3 List
p List l m t Screen ch a danh sch cc l a ch n, ng
v ch n m t hay nhi u item.

i s d ng c th t

ng tc v i List

package Edu.listbox;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import
javax.microedition.midlet.MIDletStateChangeExcepti
on;
public class listbox extends MIDlet implements
CommandListener {
private Display display;
private List ls;
private Command chonCommand;
private Command quitCommand;
public listbox() {
}
public void destroyApp(boolean
unconditional) {
}
protected void pauseApp() {
}
protected void startApp() throws
MIDletStateChangeException {
display = Display.getDisplay(this);
quitCommand = new
Command("That",Command.SCREEN,1);
chonCommand = new
Command("Chon",Command.BACK,1);
ls=new List(null, Choice.IMPLICIT, new
String[]{ "Muc chon 1", "Muc chon 2", "Muc chon 3"
}, new Image[]{null, null, null });
ls.addCommand(quitCommand);
ls.addCommand(chonCommand);
ls.setCommandListener(this);
display.setCurrent(ls);
}
public void commandAction(Command choice,
Displayable displayable) {
if(choice==quitCommand){
destroyApp(false);
notifyDestroyed();
}
if(displayable == ls){
if( choice ==ls.SELECT_COMMAND)
{
switch( ls.getSelectedIndex()){
case 0:
/////..........
break;
case 1:
//////.........
break;
case 2:
//////.........
break;
}
}
}
}
}

3.2

h a m c th p
h a m c th p l l p con c a l p Canvas. L p ny cung c p cc ph ng th c
h a cho
php v ln mn hnh hay vo m t m hnh cng v i cc ph ng th c x l s ki n bn
phm. L p ny dng cho cc ng d ng tr ch i c n u khi n v mn hnh.
Canvas v Graphics l hai l p ch y u c a cc hm API c p th p. Canvas l m t khung cho
php ng i l p trnh c th v ln thi t b trnh by c ng nh vi c x l s ki n. Cn l p Graphics
cung c p cc i t ng v .
3.2.1 L p Canvas
p Canvas l m t l p tr u t ng, ta ph i t o cc canvas c a ring
v trn l p m r ng
ny v l p Canvas cung c p ph ng th c:
- paint (Graphics g) th c hi n v ln canvas.
- repaint() th c hi n v l i cc hnh hi n th trn mn hnh.
M i l p canvas t
ng nh n s ki n c a phm b ng cch s d ng cc ph ng th c:
- void keyPressed (int keyCode):
c g i khi m t phm
c nh n
- void keyReleased( int keyCode):
c g i khi m t phm
c th .
- void keyRepeated( int keyCode):
c g i khi m t phm b gi .
- int getKeyCode(int gameAction): L y m t m phm ph n h i l i hnh ng game xc nh
trn thi t b
- String getKeyName (int keyCode): L y tn c a phm c gi tr m phm l keyCode
- int getGameAction(int keyCode): L y m t hnh ng game lin quan n m phm
c
nh n.
- void pointerDragged( int x, int y):
c g i khi r chu t n t a x, y m i.
- void pointerPressed ( int x, int y) :
c g i khi nh n chu t t i t a
x, y
- void pointerReleased ( int x, int y):
c g i khi th chu t t i t a
x, y.
3.2.2 L p Graphics
i t ng Graphics c th
c s d ng v : hnh vung, hnh ch nh t, cung trn,
ng th ng, k t , nh, v n b n trn i t ng Canvas.
Cc ph ng th c:
- void setColor(int red, int green, int blue)
- void drawArc(): v cung trn.
- void drawRect(): v hnh ch nh t
- void drawString(): V chu i.
- void drawImage(): v nh.

protected void pauseApp() {


}
protected void startApp() throws
MIDletStateChangeException {
display.setCurrent(canvas);
}
public void exitMIDlet(){
destroyApp(false);
notifyDestroyed();
}

package Edu.KeyCodes;
import
javax.microedition.lcdui.*;
import
javax.microedition.midlet.*;
import
javax.microedition.midlet.
MIDletStateChangeException;
public class KeyCodes extends
MIDlet {
private Display display;
private KeyCodeCanvas
canvas;
public KeyCodes() {
display=
Display.getDisplay(this);
canvas = new
KeyCodeCanvas(this);
}
public void
destroyApp(boolean
unconditional) {
}

}
class KeyCodeCanvas extends Canvas implements
CommandListener{
private Command quitCommand;
private KeyCodes midlet;
private int x=getWidth()/2,
y=getHeight()/2;
public KeyCodeCanvas(KeyCodes midlet)
{
this.midlet= midlet;
quitCommand = new
Command("That", Command.SCREEN, 1);
addCommand(quitCommand);
setCommandListener(this);
}
protected void paint (Graphics g){
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(),
getHeight());
g.setColor(0, 0, 0);
g.drawArc(x, y,30, 30, 0, 360);
}
public void commandAction (Command c,
Displayable d) {
if (c== quitCommand){
midlet.exitMIDlet();
}
}
protected void keyPressed(int
keyCode) {
if (keyCode ==
getKeyCode(RIGHT)){
x=x+10;
}
if (keyCode ==
getKeyCode(LEFT)){
x=x-10;
}
if (keyCode == getKeyCode(UP)){
y=y-10;
}
if (keyCode ==
getKeyCode(DOWN)){
y=y+10;
}
repaint();
}
}

4.

p trnh Bluetooth
thu t bluetooth
c dng truy n thng gi a n th ai di ng v i n th ai di ng,
ho c gi a n th ai di ng v mn tnh.
C 3 ph ng th c truy n thng
c ci t trong k thu t bluetooth:
- OBEX - Object Exchange: dng chuy n d li u d ng file, hnh nh.
- L2CAP- Logical Link Control and Adaptation Protocol: Dng chuy n cc gi d li u
gi a client v server.
- RFCOMM- Radio Frequency COMMunication: Dng chuy n cc lu ng d li u n
gi n.
Sun Java cung c p th vi n hm API bluetooth: Bluetooth JSR82 API package v bao g m t t c
cc ph ng th c truy n thng ni trn.
t ng d ng bluetooth bao g m hai ph n: server dng
t n i n server v th c hi n truy n thng.

t o d ch v bluetooth v ph n client

Ph n server:
M i m t thi t b bluetooth bao g m m t i t ng thi t b c c b m n gip cho vi c truy n
thng gi a cc thi t b . Trong JSR82 ph ng th c LocalDevice.getLocalDevice() tr v
i t ng
ny.
m
c k t n i bluetooth ph i s d ng chu i URL b ng ph ng th c Connector.open(URL)
v ph ng th c ny tr v lu ng chuy n d li u StreamConnectionNotifier.
d ng hai ph ng th c openOutputStream() ho c openInputStream() g i ho c nh n d
li u.
URL
c xc
nh: URL = "btspp://localhost:" + UUID +
";name=rfcommtest;authorize=true";

Trong UUID c th l : OBEX, L2CAP, RFCOMM


m_strUrl= "btspp://localhost:" + RFCOMM_UUID + ";
name=rfcommtest;authorize=true";
// m_StrmConn = BTFACADE.waitForClient(SERVICE_NBR);
try
{
m_LclDevice = LocalDevice.getLocalDevice();
m_LclDevice.setDiscoverable(DiscoveryAgent.GIAC);
m_StrmNotf = (StreamConnectionNotifier)Connector.open(m_strUrl);
//Now it will start waiting for the client connection
m_StrmConn = m_StrmNotf.acceptAndOpen();
m_bInitServer = true;
m_Output = m_StrmConn.openOutputStream();
m_Input = m_StrmConn.openInputStream();
}
catch (BluetoothStateException e)
{
System.err.println( "BluetoothStateException: " + e.getMessage() );
}

catch (BluetoothStateException e)
{
System.err.println( "BluetoothStateException: " + e.getMessage() );
}
catch (IOException ex)
{
ex.printStackTrace();
}
catch(Exception e)
{

Ph n Client:
t o
c client s d ng cc ph ng th c sau tm thi t b bluetooth v tm d ch v
trn n:
void deviceDiscovered(RemoteDevice btDevice, DeviceClass od)
void servicesDiscovered(int transID, ServiceRecord[] records)
Sau khi k t n i
nh n d li u.

c thi t b bluetooth s d ng cc ph

ng th c write () v read()

public void SearchAvailDevices()


{
try
{
//First, get the local device and obtain the discovery agent.
m_LclDevice = LocalDevice.getLocalDevice();
m_DscrAgent=

m_LclDevice.getDiscoveryAgent();

m_DscrAgent.startInquiry(DiscoveryAgent.GIAC,this);
}
catch (BluetoothStateException ex)
{
System.out.println("Problem in searching the Bluetooth devices");
ex.printStackTrace();
}
}
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
{
try
{
// Device information
System.out.println("Major Device Class and information : " +
cod.getMajorDeviceClass() +
" Minor Device Class: " +
cod.getMinorDeviceClass());
System.out.println("Bluetooth Address of the device: " +
btDevice.getBluetoothAddress());
System.out.println("Friendly Name: " +
btDevice.getFriendlyName(true));
// Now its our responsibility to search its services
UUID uuidSet[] = new UUID[1];
uuidSet[0]
= RFCOMM_UUID;

g i v

int searchID

= m_DscrAgent.searchServices(null,uuidSet,
btDevice,this);

}
catch (Exception e)
{
System.out.println("Device Discovered Error: " + e);
}
}
public void servicesDiscovered(int transID, ServiceRecord[] records)
{
for (int i = 0; i < records.length; i++)
{
m_strUrl = records[i].getConnectionURL(ServiceRecord.
AUTHENTICATE_ENCRYPT, false);
System.out.println(m_strUrl);
//we have found our service protocol
if(m_strUrl.startsWith("btspp"))
{
m_bServerFound = true;
m_bInitClient=true;
break;
}
}

5. Pht tri n ng d ng MIDlet


5.1 Chu n b cc ph n m m
Eclipse 3.1
Sun Wireless Toolkit
EclipseME Plugin for Eclipse
5.2 Ci

t
c 1: Ci cc ph n m m Eclipse v Wireless Toolkit
c 2: Ci t EclipseME plugin

10

c 3:

c 4:

c 5:

11

c 5:

c 6:

12

c 7:

c 8:

13

c 9:

c 10:

14

c 11:

c 12:

c 13:

15

c 14:

c 15:

16

c 16:

c 17:

17

c 18:

c 19:

18

5.3 ng gi ch

ng trnh

19

5.4 Ci t ch ng trnh ln
n th ai di ng
Sau khi ng gi ch ng trnh s d ng trnh PC Suite
di ng

6.

ci

t ch

ng trnh ln

n th ai

t lu n
-

c u tm hi u ki n trc t ch c l p trnh J2ME trn thi t b di ng.


ng v i m i k thu t l p trnh trn thi t b di ng xy d ng
c cc an m J2ME
ng ng.

Ti li u tham kh o:
1.
2.
3.
4.

http://www.codeguru.com/java
http://www.benhui.net
http://www.diendantinhoc.org
Bluetooth Link manager and J2ME programming, Kjell Jorgen Hole

20

You might also like