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 package net.sf.bluecove;
26
27 import java.io.DataInputStream;
28 import java.io.DataOutputStream;
29 import java.io.IOException;
30
31 import javax.bluetooth.DiscoveryAgent;
32 import javax.bluetooth.LocalDevice;
33 import javax.bluetooth.ServiceRecord;
34 import javax.bluetooth.UUID;
35 import javax.microedition.io.Connector;
36 import javax.microedition.io.StreamConnection;
37 import javax.microedition.io.StreamConnectionNotifier;
38
39 import junit.framework.Assert;
40 import junit.framework.TestCase;
41
42 import com.intel.bluetooth.EmulatorTestsHelper;
43
44
45
46
47 public class ExampleTest extends TestCase {
48
49 private static final UUID uuid = new UUID(0x2108);
50
51 private Thread serverThread;
52
53 private static final String echoGreeting = "I echo";
54
55 protected void setUp() throws Exception {
56 super.setUp();
57 EmulatorTestsHelper.startInProcessServer();
58 EmulatorTestsHelper.useThreadLocalEmulator();
59 serverThread = EmulatorTestsHelper.runNewEmulatorStack(new EchoServerRunnable());
60 }
61
62 protected void tearDown() throws Exception {
63 super.tearDown();
64 if ((serverThread != null) && (serverThread.isAlive())) {
65 serverThread.interrupt();
66 serverThread.join();
67 }
68 EmulatorTestsHelper.stopInProcessServer();
69 }
70
71 private class EchoServerRunnable implements Runnable {
72
73 public void run() {
74
75 StreamConnectionNotifier service = null;
76
77 try {
78 String url = "btspp://localhost:" + uuid.toString() + ";name=TServer";
79 service = (StreamConnectionNotifier) Connector.open(url);
80
81 StreamConnection conn = (StreamConnection) service.acceptAndOpen();
82
83 System.out.println("Server received connection");
84
85 DataOutputStream dos = conn.openDataOutputStream();
86 DataInputStream dis = conn.openDataInputStream();
87
88 dos.writeUTF(echoGreeting);
89 dos.flush();
90
91 String received = dis.readUTF();
92 System.out.println("Server received:" + received);
93
94 dos.writeUTF(received);
95 dos.flush();
96
97 dos.close();
98 dis.close();
99
100 conn.close();
101 } catch (Throwable e) {
102 System.err.print(e.toString());
103 e.printStackTrace();
104 } finally {
105 if (service != null) {
106 try {
107 service.close();
108 } catch (IOException ignore) {
109 }
110 }
111 }
112 }
113 }
114
115 public void testConnection() throws Exception {
116 DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
117
118 String serverURL = discoveryAgent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
119 Assert.assertNotNull("service not found", serverURL);
120
121 StreamConnection conn = null;
122 try {
123 conn = (StreamConnection) Connector.open(serverURL);
124 DataOutputStream dos = conn.openDataOutputStream();
125 DataInputStream dis = conn.openDataInputStream();
126
127 String received = dis.readUTF();
128 Assert.assertEquals("handshake", echoGreeting, received);
129
130 String message = "TestMe";
131 System.out.println("Client Sending message:" + message);
132 dos.writeUTF(message);
133
134 received = dis.readUTF();
135 Assert.assertEquals("echo", received, message);
136
137 dos.close();
138 dis.close();
139
140 } catch (IOException e) {
141 System.err.print(e.toString());
142 e.printStackTrace();
143 } finally {
144 if (conn != null) {
145 try {
146 conn.close();
147 } catch (IOException ignore) {
148 }
149 }
150 }
151 }
152 }