Professional Documents
Culture Documents
lập trình di động 2 PDF
lập trình di động 2 PDF
NG
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
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
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.
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";
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()
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;
}
}
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
-
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