View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-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: TestOBEXCilent.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import java.io.IOException;
28  import java.io.OutputStream;
29  
30  import javax.bluetooth.DiscoveryAgent;
31  import javax.bluetooth.LocalDevice;
32  import javax.microedition.io.Connector;
33  import javax.obex.ClientSession;
34  import javax.obex.HeaderSet;
35  import javax.obex.Operation;
36  import javax.obex.ResponseCodes;
37  
38  import org.bluecove.tester.log.Logger;
39  import org.bluecove.tester.util.RuntimeDetect;
40  
41  import net.sf.bluecove.util.BluetoothTypesInfo;
42  
43  /**
44   * 
45   */
46  public class TestOBEXCilent implements Runnable {
47  
48  	public static final boolean obexEnabled = true;
49  
50  	public static void obexPut() {
51  		Thread thread = RuntimeDetect.cldcStub.createNamedThread(new TestOBEXCilent(0), "ObexClinet");
52  		thread.start();
53  	}
54  
55  	private TestOBEXCilent(int type) {
56  
57  	}
58  
59  	public void run() {
60  		try {
61  			runObecPut();
62  		} catch (Throwable e) {
63  			Logger.error("obex", e);
64  		}
65  	}
66  
67  	private void runObecPut() throws IOException {
68  
69  		String serverURL;
70  		if (Configuration.testServerOBEX_TCP.booleanValue()) {
71  			serverURL = "tcpobex://127.1.1.1:650";
72  		} else {
73  			DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
74  			Logger.debug("Find OBEX_OBJECT_PUSH  service");
75  			serverURL = discoveryAgent.selectService(TestResponderServerOBEX.OBEX_OBJECT_PUSH, Configuration
76  					.getRequiredSecurity(), false);
77  			if (serverURL == null) {
78  				Logger.debug("no OBEX service found");
79  				return;
80  			}
81  		}
82  		Logger.debug("connect " + serverURL);
83  		ClientSession clientSession = (ClientSession) Connector.open(serverURL);
84  		HeaderSet hsConnectReply = clientSession.connect(null);
85  		if (hsConnectReply.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
86  			Logger.debug("Failed to connect");
87  			return;
88  		}
89  
90  		HeaderSet hsOperation = clientSession.createHeaderSet();
91  		hsOperation.setHeader(HeaderSet.NAME, "Hello.txt");
92  		hsOperation.setHeader(HeaderSet.TYPE, "text");
93  
94  		// Create PUT Operation
95  		Operation po = clientSession.put(hsOperation);
96  
97  		// Send some text to server
98  		byte data[] = "Hello world!".getBytes("iso-8859-1");
99  		OutputStream os = po.openOutputStream();
100 		os.write(data);
101 		os.close();
102 
103 		Logger.debug("put responseCode " + BluetoothTypesInfo.toStringObexResponseCodes(po.getResponseCode()));
104 
105 		po.close();
106 
107 		HeaderSet hsd = clientSession.disconnect(null);
108 
109 		Logger.debug("disconnect responseCode " + BluetoothTypesInfo.toStringObexResponseCodes(hsd.getResponseCode()));
110 
111 		clientSession.close();
112 	}
113 
114 }