Lập trình trên thiết bị di động

Vào nhӳng năm 1990, Java ÿѭӧc ra ÿӡi tӯdӵán xanh và ban ÿҫu ÿѭӧc xây dӵng ÿӇkiiӇm

sóat các thí bӏdân dөng và mӝt sӕthi Ӄt bӏ cҫm tay. Java ÿѭӧc xây dӵng chӫyӃu dӵa trong bӝ

công cөphát triӇn (Java Development Kit – JDK) nhѭlà thѭviӋn chuҭn trong ÿó chӭa trình

biên dӏch, trình thông dӏ ch, Ĉây chính là nӅn tҧng cho viӋc phát triӇn ӭng dөng Java. HiӋn

nay các nhà phát triӇn ÿã xây dӵng nhiӅu nhánh mӟi cho Java nhѭ:

 J2EE ( Java 2 Platform Enterprise Edition): Phiên bҧn dành cho máy chӫlӟn vӟi sӭc

Pҥnh xӱlý và dung l ѭӧng bӝnhӟl ӟn.

 J2SE ( Java 2 Platform Standard Edition): Phiên bҧn chuҭn chҥy trên các máy PC và

laptop.

 J2ME ( Java 2 Platform Micro Edition): là phiên bҧn rút gӑn cӫa Java cho các thiӃt di

ÿӝng giӟi hҥn vӅbӝnhӟvà bӝxӱlý.

pdf20 trang | Chuyên mục: Lập Trình Trên Thiết Bị Di Động | Chia sẻ: dkS00TYs | Lượt xem: 2153 | Lượt tải: 1download
Tóm tắt nội dung Lập trình trên thiết bị di động, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ic 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 sách các l?a ch?n, ng??i s? d?ng có th? t??ng tác v?i List
và ch?n m?t hay nhi?u item.
5package 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("Thóat",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;
 }
 }
 }
 }
}
63.2 ?? h?a m?c th?p
?? h?a m?c th?p là l?p con c?a l?p Canvas. L?p này cung c?p các ph??ng th?c ?? h?a cho
phép v? lên màn hình hay vào m?t ??m hình cùng v?i các ph??ng th?c x? lý s? ki?n bàn
phím. L?p này dùng cho các ?ng d?ng trò ch?i c?n ???u khi?n v? màn hình.
Canvas và Graphics là hai l?p ch? y?u c?a các hàm API c?p th?p. Canvas là m?t khung cho
phép ng??i l?p trình có th? v? lên thi?t b? trình bày c?ng nh? vi?c x? lý s? ki?n. Còn l?p Graphics
cung c?p các ??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 các canvas c?a riêng ?? v? trên l?p m? r?ng
này và l?p Canvas cung c?p ph??ng th?c:
- paint (Graphics g) ?? th?c hi?n v? lên canvas.
- repaint() ?? th?c hi?n v? l?i các hình ?ã hi?n th? trên màn hình.
 M?i l?p canvas t????ng nh?n s? ki?n c?a phím b?ng cách s? d?ng các ph??ng th?c:
- void keyPressed (int keyCode): ???c g?i khi m?t phím ???c nh?n
- void keyReleased( int keyCode): ???c g?i khi m?t phím ???c th?.
- void keyRepeated( int keyCode): ???c g?i khi m?t phím b? gi?.
- int getKeyCode(int gameAction): L?y m?t mã phím ph?n h?i l?i hành ??ng game xác ??nh
trên thi?t b?
- String getKeyName (int keyCode): L?y tên c?a phím có giá tr? mã phím là keyCode
- int getGameAction(int keyCode): L?y m?t hành ??ng game liên quan ??n mã phím ???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?: hình vuông, hình ch? nh?t, cung tròn,
???ng th?ng, ký t?, ?nh, v?n b?n trên ??i t??ng Canvas.
Các ph??ng th?c:
- void setColor(int red, int green, int blue)
- void drawArc(…): v? cung tròn.
- void drawRect(…): v? hình ch? nh?t
- void drawString(…): V? chu?i.
- void drawImage(…): v???nh.
7package 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) {
 }
protected void pauseApp() {
 }
protected void startApp() throws
MIDletStateChangeException {
display.setCurrent(canvas);
 }
public void exitMIDlet(){
 destroyApp(false);
 notifyDestroyed();
 }
}
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("Thóat", 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();
 }
 }
84. ??p trình Bluetooth
?? thu?t bluetooth ???c dùng ?? truy?n thông gi?a ???n th?ai di ??ng v?i ???n th?ai di ?ông,
ho?c gi?a ???n th?ai di ??ng và mán tính.
Có 3 ph??ng th?c truy?n thông ???c cài ??t trong k? thu?t bluetooth:
- OBEX - Object Exchange: dùng ?? chuy?n d? li?u d?ng file, hình ?nh.
- L2CAP- Logical Link Control and Adaptation Protocol: Dùng chuy?n các gói d? li?u
gi?a client và server.
- RFCOMM- Radio Frequency COMMunication: Dùng ?? chuy?n các lu?ng d? li?u ??n
gi?n.
Sun Java cung c?p th? vi?n hàm API bluetooth: Bluetooth JSR82 API package và bao g?m t?t c?
các ph??ng th?c truy?n thông nói trên.
??t ?ng d?ng bluetooth bao g?m hai ph?n: server dùng ?? t?o d?ch v? bluetooth và ph?n client
??t n?i ??n server và th?c hi?n truy?n thông.
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ó giúp cho vi?c truy?n
thông gi?a các thi?t b?. Trong JSR82 ph??ng th?c LocalDevice.getLocalDevice() tr? v????i t??ng
này.
?? 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 này 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 xác ??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() );
}
9Ph?n Client:
? ?? t?o ???c client s? d?ng các ph??ng th?c sau ?? tìm thi?t b? bluetooth và tìm d?ch v?????c
?? trên nó:
void deviceDiscovered(RemoteDevice btDevice, DeviceClass od)
void servicesDiscovered(int transID, ServiceRecord[] records)
 Sau khi k?t n?i ???c thi?t b? bluetooth s? d?ng các ph??ng th?c write () và read() ?? g?i và
nh?n d? li?u.
catch (BluetoothStateException e)
{
 System.err.println( "BluetoothStateException: " + e.getMessage() );
}
catch (IOException ex)
{
 ex.printStackTrace();
}
catch(Exception e)
{
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;
10
5. Phát tri?n ?ng d?ng MIDlet
5.1 Chu?n b? các ph?n m?m
Eclipse 3.1
Sun Wireless Toolkit
EclipseME Plugin for Eclipse
5.2 Cài ??t
???c 1: Cài các ph?n m?m Eclipse và Wireless Toolkit
???c 2: Cài ??t EclipseME plugin
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;
 }
}
11
???c 3:
???c 4:
???c 5:
12
???c 5:
???c 6:
13
???c 7:
???c 8:
14
???c 9:
???c 10:
15
???c 11:
???c 12:
???c 13:
16
???c 14:
???c 15:
17
???c 16:
???c 17:
18
???c 18:
???c 19:
19
5.3 ?óng gói ch??ng trình
20
5.4 Cài ??t ch??ng trình lên ???n th?ai di ??ng
Sau khi ?óng gói ch??ng trình s? d?ng trình PC Suite ?? cài ??t ch??ng trình lên ???n th?ai
di ??ng
6. ??t lu?n
- ???c ??u ?ã tìm hi?u ki?n trúc t? ch?c l?p trình J2ME trên thi?t b? di ??ng.
- ?ng v?i m?i k? thu?t l?p trình trên thi?t b? di ??ng xây d?ng ???c các ??an mã J2ME
???ng ?ng.
Tài li?u tham kh?o:
1. 
2. 
3. 
4. Bluetooth – Link manager and J2ME programming, Kjell Jorgen Hole

File đính kèm:

  • pdfLập trình trên thiết bị di động.pdf
Tài liệu liên quan