-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPPServer.java
94 lines (72 loc) · 3.06 KB
/
SPPServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.bluetooth.*;
import javax.microedition.io.*;
/**
* Class that implements an SPP Server which accepts single line of
* message from an SPP client and sends a single line of response to the client.
*/
public class SPPServer extends Thread {
//Create a UUID for SPP / RFComm
UUID uuid = new UUID("1101", true);
//Create the Servicve URL
String connectionString = "btspp://localhost:" + uuid + ";name=BlutoothChat";
String partnerName;
public BufferedReader in;
public PrintWriter out;
StreamConnectionNotifier streamConnNotifier;
private ActionListener onConnectionSuccessful;
// Setter method for handling successful connections
public void setOnConnectionSuccessful(ActionListener onConnectionSuccessful) {
this.onConnectionSuccessful = onConnectionSuccessful;
}
//start server
public void run(){
try {
//open server url
streamConnNotifier = (StreamConnectionNotifier) Connector.open(connectionString);
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect…");
StreamConnection connection = streamConnNotifier.acceptAndOpen();
InputStream inStream = connection.openInputStream();
in = new BufferedReader(new InputStreamReader(inStream));
OutputStream outStream = connection.openOutputStream();
out = new PrintWriter(new OutputStreamWriter(outStream));
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: " + dev.getBluetoothAddress());
System.out.println("Remote device name: " + dev.getFriendlyName(true));
partnerName = dev.getFriendlyName(true);
// Notify successful connection if listener is set
if(onConnectionSuccessful != null)
onConnectionSuccessful.actionPerformed(new ActionEvent(this,ActionEvent.RESERVED_ID_MAX+1,""));
//init in/out streams
}catch (IOException e){
e.printStackTrace();
}
}
// Method to close the server connection
public void closeConnection(){
try {
streamConnNotifier.close();
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
//display local device address and name
/*LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SPPServer sampleSPPServer=new SPPServer();
sampleSPPServer.startServer();*/
}
}