1 | package bluesoleiltest;
|
2 |
|
3 | import javax.bluetooth.*;
|
4 | import com.intel.bluetooth.*;
|
5 | import com.sun.org.apache.xpath.internal.FoundIndex;
|
6 |
|
7 | import java.io.DataInputStream;
|
8 | import java.io.DataOutputStream;
|
9 | import java.io.IOException;
|
10 | import java.io.InputStream;
|
11 | import java.io.OutputStream;
|
12 |
|
13 | import javax.bluetooth.RemoteDevice;
|
14 | import javax.bluetooth.ServiceRecord;
|
15 | import javax.microedition.io.StreamConnection;
|
16 | import javax.microedition.io.Connector;
|
17 | import javax.microedition.io.*;
|
18 | import java.io.*;
|
19 | import java.util.Hashtable;
|
20 | import java.util.Map;
|
21 | import javax.bluetooth.UUID;
|
22 |
|
23 | import java.lang.*;
|
24 | import java.util.Vector;
|
25 | import javax.microedition.midlet.MIDlet;
|
26 | import javax.microedition.lcdui.CommandListener;
|
27 | import java.util.List;
|
28 | import javax.bluetooth.*;
|
29 |
|
30 |
|
31 |
|
32 | public class Main
|
33 | {
|
34 |
|
35 | /**
|
36 | * @param args the command line arguments
|
37 | */
|
38 | private static LocalDevice localdevice; //local Bluetooth Manager
|
39 | private static DiscoveryAgent discoveryagent; //discovery agent for clients
|
40 |
|
41 | //Bluetooth Service UUID of interest
|
42 | private static final String myServiceUUID = "000BCE059FFF"; //Adresse von dem Free2Move Modul
|
43 | private static UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, true);
|
44 | //Define the client connecntion URL
|
45 | private static String connURL = "btspp://"+ MYSERVICEUUID_UUID.toString() + ":1" + ";master=false" + ";encrypt=false" + ";authenticate=false";
|
46 | private static UUID[] uuids = {MYSERVICEUUID_UUID};
|
47 | private static int[] attrSet = {0x0000,0x0001,0x0002,0x0003};
|
48 |
|
49 | private static final Object serviceSearchSemaphore = new Object();
|
50 |
|
51 | public static void main(String[] args)
|
52 | {
|
53 | Bluetooth bt = new Bluetooth();
|
54 | BtClient btc = new BtClient();
|
55 |
|
56 | try
|
57 | {
|
58 | bt.btInit();
|
59 | }
|
60 | catch(Exception ex)
|
61 | {
|
62 | System.out.println("XXX");
|
63 | }
|
64 | btc.btinitiateDeviceSearch();
|
65 | btc.btInitiateServiceSearch();
|
66 | btc.btConnect2(MYSERVICEUUID_UUID);
|
67 |
|
68 | System.out.println("start");
|
69 | getBluetoothInfo();
|
70 | System.out.println(LocalDevice.getProperty("bluecove.stack"));
|
71 |
|
72 | }
|
73 |
|
74 | public static class Bluetooth
|
75 | {
|
76 | public void btInit() throws BluetoothStateException
|
77 | {
|
78 | localdevice = null;
|
79 | discoveryagent = null;
|
80 | //Retrieve the local device to get to the Bluetooth Manager
|
81 | localdevice = LocalDevice.getLocalDevice();
|
82 | //Servers set the discoverable mode to GIAC
|
83 | localdevice.setDiscoverable(DiscoveryAgent.GIAC);
|
84 | discoveryagent = localdevice.getDiscoveryAgent();
|
85 | //discoveryagent.startInquiry(DiscoveryAgent.GIAC, this);
|
86 | }
|
87 |
|
88 | public void btServer()
|
89 | {
|
90 | try{
|
91 | //create a server connection ( a notifier)
|
92 | StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(connURL);
|
93 | //Accept a new client connection
|
94 | StreamConnection sc = scn.acceptAndOpen();
|
95 | //New client connection accepted; get a handle on it
|
96 | RemoteDevice rd = RemoteDevice.getRemoteDevice(sc);
|
97 | System.out.println("New Client connection...." + rd.getFriendlyName(false));
|
98 | //Read input message, in this example a String
|
99 | DataInputStream dataIn = sc.openDataInputStream();
|
100 | String s = dataIn.readUTF();
|
101 | //Pass received message to incoming message listener
|
102 | System.out.println(s);
|
103 | scn.close();
|
104 | } catch(IOException e)
|
105 | {
|
106 | System.out.println(e);
|
107 | }
|
108 | }
|
109 |
|
110 | /*
|
111 | public void btupdateServiceRecord()
|
112 | {
|
113 | try{
|
114 | //retrieve service record and set/update optional attributes
|
115 | //for example ServiceAvailability, indicating service is available
|
116 | sr = localdevice.getRecord(scn);
|
117 | }
|
118 | }*/
|
119 |
|
120 | }
|
121 |
|
122 | public static class BtClient implements DiscoveryListener
|
123 | {
|
124 | Map discoveredDevices = new Hashtable();
|
125 | Map remoteDevices = new Hashtable();
|
126 |
|
127 | //DiscoveryListener Callbacks
|
128 | /**
|
129 | * deviceDiscovered() is called by the DiscoverAgent when
|
130 | * it discovers a device during an inquiry
|
131 | */
|
132 |
|
133 | /**
|
134 | * btInitiateDeviceSearch() kicks off the device discovery
|
135 | *
|
136 | */
|
137 | public void btinitiateDeviceSearch()
|
138 | {
|
139 | System.out.println("BTMIDLET.btinitiateDeviceSearch");
|
140 | int i,s;
|
141 | boolean inquiryStarted;
|
142 |
|
143 | remoteDevices.clear();
|
144 | discoveredDevices.clear();
|
145 |
|
146 | RemoteDevice[] cachedDevices = discoveryagent.retrieveDevices(DiscoveryAgent.CACHED);
|
147 | if(cachedDevices != null)
|
148 | {
|
149 | s = cachedDevices.length;
|
150 | for(i=0; i<s ; i++)
|
151 | {
|
152 | remoteDevices.put(cachedDevices[i].getBluetoothAddress(), cachedDevices[i]);
|
153 | }
|
154 | }
|
155 | RemoteDevice[] preknownDevices = discoveryagent.retrieveDevices(DiscoveryAgent.PREKNOWN);
|
156 | if(preknownDevices != null)
|
157 | {
|
158 | s = preknownDevices.length;
|
159 | for(i=0; i<s ; i++)
|
160 | {
|
161 | remoteDevices.put(preknownDevices[i].getBluetoothAddress(), preknownDevices[i]);
|
162 | }
|
163 | }
|
164 | try{
|
165 | inquiryStarted = discoveryagent.startInquiry(DiscoveryAgent.GIAC, this);
|
166 | } catch(BluetoothStateException bse){
|
167 | System.out.println("Inquiry Failed " + bse);
|
168 | return;
|
169 | }
|
170 | if(inquiryStarted)
|
171 | {
|
172 | //Display progress message
|
173 | System.out.println("Inquiry in Progress");
|
174 | } else {
|
175 | //Display error message
|
176 | System.out.println("Inquiry Failed");
|
177 | }
|
178 | }
|
179 |
|
180 | /**
|
181 | * btInitiateServiceSearch() kicks off the service discovery
|
182 | */
|
183 | public void btInitiateServiceSearch()
|
184 | {
|
185 | System.out.println("BTMIDlet.btInitiateServiceSearch");
|
186 | int transID,s;
|
187 |
|
188 | discoveredDevices.clear();
|
189 |
|
190 | //Initiate the service search in the remote device
|
191 | s = remoteDevices.size();
|
192 | if(s == 0)
|
193 | {
|
194 | System.out.println("No devices to search...");
|
195 | } else
|
196 | {
|
197 | for(Object obj : remoteDevices.values())
|
198 | {
|
199 | RemoteDevice rd = (RemoteDevice) obj;
|
200 | try
|
201 | {
|
202 | transID = discoveryagent.searchServices(attrSet, uuids, rd, this);
|
203 | //Display progress message
|
204 | System.out.println("Service Search in Progress ("+transID+")");
|
205 | synchronized (serviceSearchSemaphore){
|
206 | serviceSearchSemaphore.wait();
|
207 | }
|
208 | } catch(InterruptedException ie)
|
209 | {
|
210 | System.out.println(ie);
|
211 | } catch(BluetoothStateException bse)
|
212 | {
|
213 | System.out.println("Service Search Failed " + bse);
|
214 | return;
|
215 | }
|
216 | }
|
217 | }
|
218 | }
|
219 |
|
220 | public void btConnect(final ServiceRecord sr)
|
221 | {
|
222 | final String test = "World";
|
223 | Thread th = new Thread()
|
224 | {
|
225 | public void run()
|
226 | {
|
227 | System.out.println("BTMIDlet.btConnect.run()");
|
228 | RemoteDevice rd = sr.getHostDevice();
|
229 | String connectionURL = sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
|
230 | try
|
231 | {
|
232 | System.out.println("Connecting to " + rd.getFriendlyName(false));
|
233 | System.out.println("Connecting to " + rd.getFriendlyName(false) + ", " + connectionURL);
|
234 | StreamConnection streamconnection = (StreamConnection)Connector.open(connectionURL);
|
235 | DataOutputStream dataout = streamconnection.openDataOutputStream();
|
236 | dataout.writeUTF(test);
|
237 | System.out.println("Closing");
|
238 | streamconnection.close();
|
239 | } catch(IOException ioe)
|
240 | {
|
241 | System.out.println("BTMIDlet.btConnect, exception & " + ioe);
|
242 | }
|
243 | }
|
244 | };
|
245 | th.start();
|
246 | }
|
247 |
|
248 | public void btConnect2(final UUID uuid)
|
249 | {
|
250 | final String test = "World";
|
251 | Thread th = new Thread()
|
252 | {
|
253 | public void run()
|
254 | {
|
255 | System.out.println("BTMIDlet.btConnect2.run()");
|
256 | try
|
257 | {
|
258 | //Select the Service. indicate no authentication or encryption is required
|
259 | String connectionURL = discoveryagent.selectService(uuid, ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
|
260 | System.out.println("Connecting to " + uuid.toString());
|
261 | StreamConnection streamconnection = (StreamConnection)Connector.open(connectionURL);
|
262 | System.out.println("Sending message out");
|
263 | DataOutputStream dataout = streamconnection.openDataOutputStream();
|
264 | dataout.writeUTF(test);
|
265 | System.out.println("Closing");
|
266 | streamconnection.close();
|
267 | } catch(IOException ioe)
|
268 | {
|
269 | System.out.println("BTMIDlet.btConnect2, exception & " + ioe);
|
270 | }
|
271 | }
|
272 | };
|
273 | th.start();
|
274 | try { th.join(); } catch (InterruptedException ex) { ex.printStackTrace(); }
|
275 | }
|
276 |
|
277 | public void inquiryCompleted(int type)
|
278 | {
|
279 | int s;
|
280 |
|
281 | //After each inquiry is completed, update thee screen
|
282 | //now that the inquiry has been completed move newly discovered
|
283 | //devices into remoteDevices Map
|
284 | s = discoveredDevices.size();
|
285 | if(s > 0)
|
286 | {
|
287 | System.out.println(s + " device(s) found ");
|
288 | for(Object obj : discoveredDevices.values())
|
289 | {
|
290 | RemoteDevice rd = (RemoteDevice) obj;
|
291 | //add device only if not already knwon or cached
|
292 | RemoteDevice rd2 =(RemoteDevice)remoteDevices.get(rd.getBluetoothAddress());
|
293 | if(rd2==null)
|
294 | {
|
295 | remoteDevices.put(rd.getBluetoothAddress(), rd);
|
296 | }
|
297 | }
|
298 | }
|
299 | else
|
300 | {
|
301 | System.out.println("No devices found");
|
302 | }
|
303 | //Show remote devices
|
304 | String friendlyName;
|
305 | s = remoteDevices.size();
|
306 | if( s > 0)
|
307 | {
|
308 | System.out.println(s + " device(s) found ");
|
309 | for(Object obj : remoteDevices.values())
|
310 | {
|
311 | RemoteDevice rd = (RemoteDevice) obj;
|
312 | try
|
313 | {
|
314 | friendlyName = rd.getFriendlyName(false);
|
315 | } catch(IOException ioe)
|
316 | {
|
317 | friendlyName = null;
|
318 | }
|
319 | if(friendlyName == null)
|
320 | {
|
321 | System.out.println(rd.getBluetoothAddress());
|
322 | } else
|
323 | {
|
324 | System.out.println(friendlyName);
|
325 | }
|
326 | }
|
327 | }
|
328 | }
|
329 |
|
330 | public void servicesDiscovered(int transID,ServiceRecord[] record){
|
331 | }
|
332 |
|
333 | public void serviceSearchCompleted(int transID,int type){
|
334 | }
|
335 |
|
336 | public void deviceDiscovered(RemoteDevice device, DeviceClass cod)
|
337 | {
|
338 | try{
|
339 | String friendlyName = device.getFriendlyName(true);
|
340 | System.out.println("Device Discovered: " + friendlyName);
|
341 | discoveredDevices.put(device.getBluetoothAddress(), device);
|
342 | }
|
343 | catch(IOException e)
|
344 | {
|
345 | System.out.println(e);
|
346 | }
|
347 |
|
348 | }
|
349 | }
|
350 | private static void getBluetoothInfo()
|
351 | {
|
352 | StringBuilder f = new StringBuilder();
|
353 |
|
354 | LocalDevice local = null;
|
355 | try
|
356 | {
|
357 | local = LocalDevice.getLocalDevice();
|
358 | }
|
359 | catch (BluetoothStateException e)
|
360 | {
|
361 | System.out.println(e.toString());
|
362 | return;
|
363 | }
|
364 |
|
365 |
|
366 | f.append("Adresse: " + local.getBluetoothAddress() + "\n");
|
367 | String name = local.getFriendlyName();
|
368 | if (name == null)
|
369 | f.append("Kein Name vorhanden\n");
|
370 | else
|
371 | f.append("Name: " + name + "\n");
|
372 |
|
373 | int mode = local.getDiscoverable();
|
374 | StringBuffer text = new StringBuffer("Modus: ");
|
375 |
|
376 | switch (mode)
|
377 | {
|
378 | case DiscoveryAgent.NOT_DISCOVERABLE:
|
379 | text.append("Nicht aufsuchbar");
|
380 | break;
|
381 | case DiscoveryAgent.GIAC:
|
382 | text.append("General");
|
383 | break;
|
384 | case DiscoveryAgent.LIAC:
|
385 | text.append("Limited");
|
386 | break;
|
387 | default:
|
388 | text.append("0x" + Integer.toString(mode,16));
|
389 | }
|
390 |
|
391 | f.append(text.toString() + "\n");
|
392 |
|
393 | System.out.print(f.toString());
|
394 |
|
395 | }
|
396 | }
|