1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Vlad Skarzhevskyy
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: ExampleTest.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
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 		// Find service
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 }