<--
<code>
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class ServerChat
{
private static final String CHATTANDO_UUID =...
<--
// Create Service Bluetooth
01.
import
javax.bluetooth.DiscoveryAgent;
02.
import
javax.bluetooth.LocalDevice;
03.
import
javax.microedition.io.Connector;
04.
import
javax.microedition.io.StreamConnection;
05.
import
javax.microedition.io.StreamConnectionNotifier;
06.
07.
public
class
ServerChat
08.
{
09.
private
static
final
String CHATTANDO_UUID =
"A1A2A3A4A5A6A7A8A9A0B1B2B3B4B5B6"
;
10.
private
static
final
String CHATTANDO_SERVICE =
"Chattando"
;
11.
12.
private
boolean
isReady =
false
;
13.
14.
private
StreamConnection stream_connection;
15.
private
StreamConnectionNotifier stream_connection_notifier;
16.
17.
public
ServerChat()
18.
{
19.
startServerChatBluetooth();
20.
}
21.
22.
// Apre il servizio per la Chat
23.
public
void
startServerChatBluetooth()
24.
{
25.
new
Thread()
26.
{
27.
public
void
run()
28.
{
29.
try
30.
{
31.
LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
32.
}
33.
catch
(Exception error)
34.
{
35.
error.printStackTrace();
36.
}
37.
38.
try
39.
{
40.
stream_connection_notifier = (StreamConnectionNotifier) Connector.open(
"btspp://localhost:"
+ CHATTANDO_UUID +
";name="
+ CHATTANDO_SERVICE);
41.
}
42.
catch
(Exception error)
43.
{
44.
error.printStackTrace();
45.
}
46.
47.
stopServerChatBluetooth();
48.
49.
// Mette in ascolto il Server della Chat
50.
isReady =
true
;
51.
52.
try
53.
{
54.
while
(isReady)
55.
{
56.
System.out.println(
"Sono in ascolto..."
);
57.
58.
stream_connection = stream_connection_notifier.acceptAndOpen();
59.
60.
System.out.println(
"Client Connected"
);
61.
}
62.
}
63.
catch
(Exception error)
64.
{
65.
error.printStackTrace();
66.
}
67.
}
68.
69.
}.start();
70.
}
71.
72.
// Chiude il servizio per la Chat
73.
public
void
stopServerChatBluetooth()
74.
{
75.
if
(isReady)
76.
{
77.
isReady =
false
;
78.
79.
try
80.
{
81.
stream_connection_notifier.close();
82.
}
83.
catch
(Exception error)
84.
{
85.
error.printStackTrace();
86.
}
87.
}
88.
}
89.
}
-->
http://www.dzone.com/snippets/j2me-create-service-bluetooth
No comments:
Post a Comment