Sviluppo di applicazioni J2ME Introduzione alla piattaforma Java 2

!
"
"#
::. Contenuti della lezione
$ %
* +
$
'%
&
'%(')) ' ) ' )
' )
& ,
! ! !$ !, ..
.
/+
&
::. Le diverse edizioni di Java: J2EE,J2SE,J2ME
!
#
$
"
"
::. Le diverse edizioni di Java: J2EE,J2SE,J2ME
(
%
)
&
*
'
+
,
(
,
0 )
+
.
/
*
1-
::. Profili e Configurazioni
!"
! #
"
!
$
%!&
' # (
)"&
%
*
#
+
(
*+
, -...
/
::. Profili e Configurazioni
!$/0&2
1
*
2
-,
1
$34#,
5(6-7,
34
$34#,
5(6-7,
34
. 63#,
0 2
/'0&)"$%1&/'
*
1
1
$
1
'
)*
::. Profili e Configurazioni
CLDC
Mobile phones
PDS
….
CDC
Set-top boxes
Personal video
recorders
….
KVM (160k-512k) CVM (512k-2MB)
Native Operating System
::. CDC vs. CLDC
0
0
'
82
$01$ /
$1$
$1$ $01$
)
'
82
0
#
( ) 0
::. Configurazione CLDC (JSR – 139)
!
!
"
0 )
2
#$%
$&' ()"
*
%
#
+
&
*
::. Configurazione CLDC (JSR – 139)
,
-
,
.
/
01
01 '
4, '
01
4
4
,
9
01
6
01
,
01
01 %!
&
+ +23
(molte classi derivate da Java.lang.error non esistono)
5
::. Configurazione CLDC (JSR – 139)
-
.
0
#778 /
01
01
6*,
+ +23 4 !
-
,3
:
+
'
)*
$01$
'
::. Configurazione CLDC (JSR – 139)
/
9
6 :
!
::. Configurazione CDC (JSR – 218)
!
3
$
;
2
,
%
&
$ )
;1 *
'
java.lang "
)
*
"
+
"
"
#
$ !
%
!
1- +
; +
+
+
),
! <+
)
::. Configurazione CDC (JSR – 218)
+
<
-<=+.
2
1
0 <=+
1
6
"<=+
; 5
2
4
0
<=+
:
9
5(,
$
5(,
?7+ 57@
5(,7
+
-=7
,
>
;
>
!
) 5
<=+
:
,
2
2
*&
&
("
'
&
("
'
(
-5(,
) !! !
::. Configurazione CDC (JSR – 218)
/
$* 09
9
public static void main(String[] args)
MainAppClass
#*java MainAppClass
2 0
103
::. Configurazione CDC (JSR – 218)
/ 66 32
=
>
A
,B !
8 ,
) 5
=
C
!
C 0 '
)*
0)/
D7*0
*
-
$
'
)*
+
<=+?
-
> 2.
::. Configurazione CDC (JSR – 218)
/@ 32
A
5(,,
4
-=7
/
+ E
F
5
0 ;
+
;
!!
( *
*
6*, 6
*
,
) !
::. I profili: Foundation Profile (JSR – 219)
$1$
%
&
#
,3
4
5(,
6 :
java.io, java.lang, java.lang.ref, java.lang.reflect, java.math, java.net,
java.security, java.security.acl, java.security.cert, java.security.interfaces,
java.security.spec, java.text, java.util, java.util.jar, java.util.zip,
javax.microedition.io
(
5
$
'
82
#
!
::. I profili: Personal Basis Profile (JSR – 217)
$1$
%
&
#
,
-=7
#
'
B
;
6 :
java.awt, java.awt.color, java.awt.event, java.awt.image
java.beans
2
java.rmi, java.rmi.registry
javax.microedition, javax.microedition.xlet.ixc
java.applet
G
#
2
;
::. I profili: Personal Profile (JSR – 216)
$1$
%
,
&
-=7
'
%
<&
'
::. I profili: Mobile Information Device (JSR – 118)
+6
2
(,
+
+G
2
2
+
+7
2
<
D77 2+ 220+ =702
=
7
0 < 2
<+
$
H IJK
+
!
+
+
+ 2*2+
+G
/
/
*,
/+
2
$1$ $10$ $1$ A JA+ $01$ A A
1
);=
!+0
"!
(,
; ;LM+ =
! $+0
; -6*+
+G
0
$
2
#! !
5
!
$
.
*
=
A A
$
/
)
-
0
'
2
3
32+
/
/
*,
/+)
2
1
);=
B
$1$ $10$ $1$ A JA+ $01$ A A
=
!+0
; ;LM+ =
! $+0
; -6*+
32+
+G
0
-
'
, 7
'
,
7
*
A A
*
'
)*
! !
1*.
5
$
%
!
4 )
"
-
3 7
$
$1$
$01$
2
&
3
+
!
!)
!
-37
/
/
*,
/
2
=
! $+0
; ;LM+ 0
; -6*
$0
$
'
,
7
5
$
*
A A
2
,
+
$1$
1
0
4
5
'
2
'
4
,
'
)*1,+ '
)
*
'
4,+ '
3
)
,
*
1
'
)* ,
>
5
'
)*
( 6#!
! !
!
7$ .8
/
/
*,
)
/
*
0
0
;+
32+ 2
AJ
4
'
,
7 -37
$
5
*
'
)*
1
#
A8L@B
+ 2<
-(, .
$0
$
5
,
$01$
1
'
4,
(
)
0
6-*
*
IJF LJ@B
'
/
/
*,
)
/
4
*
$01$
1
0
0
AA
;LM + 2<
4
$0
$
'
,
7%
2
-37
5
;-
+
$
&+
D
5
*
'
)*
,
0 '
)*
+
:
>
/
/
*,
/
4
+
2 :
,
2
A
% : &
0
)*
*
23
,
*
6
:
C
::. Application Models: MidLet
,
0 * 0
/
!
/
9
2 :
>
+
!!
0 * 0
*
/
/
%
5
&
$
* 0
0 * 0
-$7,
)
>
::. Ciclo di sviluppo di una MidLet
,
'
H0 7/
:
#*
F
'
8*
$* 6 ?,
F
$01$
D* 6 :
+
F
N
+
E*
2
* 0
F
'
-1
* 0
N
::. Java Application Descriptor (JAD)
$
!
O
O
9
*,
1
+
'
-6
>
<
>
,'
-1
O
!
,'
-1
+
!
+
*,
1
+
+
/
$ :
*,
1
*,
1
*,
1
*,
1
*,
1
4
)
)
' (60
'
-6
'
-1
::. Java Application Descriptor (JAD)
MIDlet-Version: 1.0.2
MIDlet-Vendor: Totore Consulting
MIDlet-Jar-URL:
http://www.mobilab.unina.it/hello.jar
MicroEdition-Configuration: CLDC-1.1
MicroEdition-Profile: MIDP-2.0
MIDlet-1: Demo1, /image/img1.png ,
org.mobilab.midlets.HelloWorld
MIDlet-Jar-Size: 10819
MIDlet-Name: HelloWorld
::. Sviluppo MIDP: Elementi Base
3
* 0
javax.microedition.midlet.MIDLet
,
(
9-*2
* 0
* 0
• void startApp()
• void pauseApp()
• void destroyApp(boolean unconditional)
+
+
::. Application Models: MidLet
import javax.microedition.midlet.*;
*
!
*
-*2F 2<
public class BasicMIDlet extends MIDlet
{
public BasicMIDlet() {
// constructor - don't do much here
}
protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
// the system destroys the MIDlet
}
09-*2
+
protected void pauseApp() {
// the system pauses the MIDlet
}
protected void startApp()
throws MIDletStateChangeException {
// the system activates the MIDlet
}
}
::. Application Models: MidLet
1
3
5
getProperty
9
9!
!
"
(
6
5
* 0
"$ '
!
$ !5
*
1
* 0
* 0
resumeRequest
!
notifyPaused
notifyDestroyed
:!
"
! 9
+
!
::. NetBeans
)
"
0
5
$
;
1
!*;! ' , ,*
1
>
1
'
7
5 F
(
GF
5(
(
(60
!
F
1
'
'
8*
)
-
0
*
.
3
' 4
! (
' 4
! (
4
4
!
+
www.netbeans.org
-
+
.
25
5
676
8
6 6
9
::. NetBeans
/
,
3
"!
'
& - 0<.
!
!
!
4
"#
=
!
" #'
1 - >? '
!
7 ! *
@1 - =
#
A
! (
::. NetBeans – Screen Designer
6
59
:
:
;
6
=+
9
H
::. NetBeans – Flow Designer
"
::. NetBeans – Source Editor
?
<
.
3
::. NetBeans – Configurazione Emulatore
$
( $
9
*
9
$1$
3
$1$ $01$
-
9
9
!
<
::. NetBeans – Esecuzione
I5
0$ 4
6
"
&
- 0<.
0$ 4
&
- 00 1 -
'7 3
B?8C !
(D
$#
(
::. Applicazioni di rete con J2ME
0
!
'% *.
1
3
+4
2,
0!15 +
,
'67
-
::. Generic Connection Framework (GCF)
H
4
2*2
+
**2
B
= #
(*72
7$ + ( 1+ 67 + G G
D77 + D77 2+ #7 + G
::. Generic Connection Framework (GCF)
H
'
8*
!
9
:
9
,3
,3
/
7
,
!
!
/
<303;+
$01$
5$#/
%5
&
1003 2+10 9;
AJ
3> 1;(
$1$+
'
82
'
26 AKP
.
::. Generic Connection Framework (GCF)
J
< 9K
9
,3
!
-
#
-,
E
!
8
<
7;
2
$
*
B
2
2
::. Generic Connection Framework (GCF)
+
:!
<
+
3
#
!
(
!
9
"
!
!
=
;
!
-=;
!
+
.
::. Generic Connection Framework (GCF)
<
+
::. Generic Connection Framework (GCF)
+
• Connection
6
,
• DatagramConnection F ,
(1 +
7$
6
1
• InputConnection+ OutputConnection F ,
2
6
1
• StreamConnection F $
• ContentConnection F 2
• StreamConnectionNotifier
• Connector F#
StreamConnection+
(
<
,
(60
(60
::. Generic Connection Framework (GCF)
3
H
< 9
L
4226
M
)
N
0
O
5
:
:* I
::. Generic Connection Framework (GCF)
=;
2
6#$ APCL 8CKM
scheme://user:password@host:port/url-path;parameters
F
, 5$#
F
6
5
+
F
F
!
+
#?14
,
F
?
:
(60
+
F%
&
,
>
::. Generic Connection Framework (GCF)
<
import
import
import
import
java.io.*;
javax.microedition.midlet.*;
javax.microedition.io.*;
javax.microedition.lcdui.*;
public class GCFConn extends MIDlet {
private Display display;
private String url = "http://wpage.unina.it/saorland/helloCMS.txt";
public GCFConn() {
display = Display.getDisplay(this);
}
public void startApp() {
try {
downloadPage(url);
} catch(IOException e) {
alert = new Alert(message,e.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
[CONTINUA ...]
-#FE.
::. Generic Connection Framework (GCF)
<
private void downloadPage(String url) throws IOException {
StringBuffer b = new StringBuffer();
StringBuffer h = new StringBuffer();
String hf = ""; // not null
InputStream is = null;
HttpConnection c = null;
TextBox t = null;
try {
long len = 0 ;
int ch = 0;
c = (HttpConnection)Connector.open(url);
is = c.openInputStream();
// Fetch the HTTP Header
for (int i = 0 ; hf != null; i++) {
hf = c.getHeaderFieldKey(i);
h.append(i+": "+hf);
hf = c.getHeaderField(i);
if (hf != null && hf != "")
h.append("="+hf+"\n");
}
[CONTINUA ...]
-$FE.
::. Generic Connection Framework (GCF)
<
[CONTINUA METODO downloadPage]
len = c.getLength();
if ( len != -1) {
// Read exactly Content-Length bytes
for (int i =0 ; i < len ; i++ )
if ((ch = is.read()) != -1)
b.append((char) ch);
} else {
// Read till the connection is closed.
while ((ch = is.read()) != -1) {
len = is.available() ;
b.append((char)ch);
}
}
}
[CONTINUA ...]
-DFE.
::. Generic Connection Framework (GCF)
<
[CONTINUA METODO downloadPage]
// Dump file data to the device screen
t = new TextBox("hello again....",h.toString()+b.toString(),2048,0);
// Dump file data to kToolBar
System.out.println("Headers");
System.out.println(h.toString());
System.out.println("Body");
System.out.println(b.toString());
} finally {
is.close();
c.close();
}
display.setCurrent(t);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
-EFE.
::. Java APIs for Bluetooth (JSR-82)
-,
+
B
'
0
'
B
'
26 L8/
8
1) javax.bluetooth
4
- ,B
2) javax.obex
-,
Q
B
%3 N
R
;
&
::. Java APIs for Bluetooth (JSR-82)
; M
*
IA8@ 63*
$
B
,
'
8*
6-*
$01$
6:
B
5
-
2
1
<-
2
1
<
6
# :
2
0
B
0
6.
<$3**
$
$
-
$
-)
.
-;9 1
.
- $
6.
::. Java APIs for Bluetooth (JSR-82)
6+
6
;?%$
B
6
2
6#$3**+ 08$-
3B H
,3
5
#
%
<&
:
3
'
-,
@4,
9
'
)*
4
::. Java APIs for Bluetooth (JSR-82)
)
0
#* +
$* <
D* ;
E* ;
&* ;
'*
'
:
B
>
M
::. Java APIs for Bluetooth (JSR-82)
+
:
:)
H
A
:)
M
M
A
J
-)
+
(
:
6+
::
)
-
!
:
!
;?%$
!
6+
A
.
::. Java APIs for Bluetooth (JSR-82)
<
3
;
!
!!
!
...
// retrieve the local Bluetooth device object
LocalDevice local = LocalDevice.getLocalDevice();
// retrieve the Bluetooth address and name of the local device
String address = local.getBluetoothAddress();
String name = local.getFriendlyName();
...
;
:
::
...
// retrieve the device that is at the other end of the Bluetooth connection
RemoteDevice remote = RemoteDevice.getRemoteDevice(Connection c);
// retrieve the Bluetooth address and name of the remote device
String remoteAddress = remote.getBluetoothAddress();
String remoteName = local.getFriendlyName(true);
...
::. Java APIs for Bluetooth (JSR-82)
;
-#F$.
+
/
0
J
M
DiscoveryAgent
;
DiscoveryListener
A
DiscoveryAgent
startInquiry.
1
M
deviceDiscovered
-
DiscoveryListener
A
inquiryCompleted
DiscoveryListener
•
DiscoveryAgent
retrieveDevices
!!
!!
@
!
A
+
!
+
!
::. Java APIs for Bluetooth (JSR-82)
;
-$F$.
3,+ 3+0I=+;P
...
// retrieve the discovery agent
DiscoveryAgent agent = local.getDiscoveryAgent();
// place the device in inquiry mode
boolean complete = agent.startInquiry();
...
;3 =63;1 + 2
+ 61 +2+,+012+
...
// retrieve the discovery agent
DiscoveryAgent agent = local.getDiscoveryAgent();
// return an array of pre-known devices
RemoteDevice[] devices =
agent.retrieveDevices(DiscoveryAgent.PREKNOWN); //usare CACHED per
//risultati inquiries precedenti
...
::. Java APIs for Bluetooth (JSR-82)
;
J
+
DiscoveryAgent
/
6+
;?%$
M
::. Java APIs for Bluetooth (JSR-82)
;
J
)
#*
$*
"
-
)).
D* ;
+
/
)
;
))
!
::. Java APIs for Bluetooth (JSR-82)
)
0 '
26 L8
$
6F 0
B
9
:
9
A
*
;
8 2
;9 1
;
-
6
<
F
628C8
08$-
1)3@
=
B
S
B
+
::. Java APIs for Bluetooth (JSR-82)
6
6
-#F$.
;9 1
$
6
3
-; ?$D$.
#
;9 1
G'7
!
;9 1
=
1 D7.
=
;9 1
)
Q
Q
GD7
;9 1
- 1 #L
::. Java APIs for Bluetooth (JSR-82)
6
6
-$F$.
;
/
=;
*
=;
)
;
-
3
N.
=; /
btspp://102030405060740A1B1C1D1E100:5
Proto
Address
Port (porta COM)
::. Java APIs for Bluetooth (JSR-82)
3
6
6
-#F$. ?
...
// service URL
String ServiceURL =
"btspp://localhost:10203040607040A1B1C1DE100;name=SPP
Server1";
try {
// create a server connection
StreamConnectionNotifier notifier =
(StreamConnectionNotifier) Connector.open(serviceURL);
// accept client connections
StreamConnection connection = notifier.acceptAndOpen();
// prepare to send/receive data
byte buffer[] = new byte[100];
String msg = "hello there, client";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
// send data to the client
os.write(msg.getBytes());
// read data from client
is.read(buffer);
connection.close();
} catch(IOException e) {
e.printStackTrace();
}
...
::. Java APIs for Bluetooth (JSR-82)
3
6
6
-$F$. ?
...
// (assuming we have the service record)
// use record to retrieve a connection URL
String url =
record.getConnectionURL(
record.NOAUTHENTICATE_NOENCRYPT, false);
// open a connection to the server
StreamConnection connection =
(StreamConnection) Connector.open(url);
// Send/receive data
try {
byte buffer[] = new byte[100];
String msg = "hello there, server";
InputStream is = connection.openInputStream();
OutputStream os = connection.openOutputStream();
// read data from the server
is.read(buffer);
// send data to the server
os.write(msg.getBytes);
connection.close();
} catch(IOException e) {
e.printStackTrace();
}
...