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.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
95 Operation po = clientSession.put(hsOperation);
96
97
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 }