View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager.discoveryservice;
2   
3   import java.io.IOException;
4   import java.util.HashSet;
5   import java.util.Set;
6   import java.util.Vector;
7   import java.util.logging.Level;
8   import java.util.logging.Logger;
9   
10  import javax.bluetooth.BluetoothStateException;
11  import javax.bluetooth.DeviceClass;
12  import javax.bluetooth.DiscoveryAgent;
13  import javax.bluetooth.DiscoveryListener;
14  import javax.bluetooth.LocalDevice;
15  import javax.bluetooth.RemoteDevice;
16  import javax.bluetooth.ServiceRecord;
17  
18  import org.ximtec.igesture.io.AbstractGestureDevice;
19  import org.ximtec.igesture.io.DeviceDiscoveryService;
20  
21  /**
22   * A Bluetooth device discovery service. It implements the {@link org.ximtec.igesture.io.DeviceDiscoveryService} and {@link javax.bluetooth.DiscoveryListener} interfaces.
23   * @author Bjorn Puype, bpuype@gmail.com
24   *
25   */
26  public class BluetoothDeviceDiscoveryService implements DeviceDiscoveryService, DiscoveryListener {
27  
28  	private static final Logger LOGGER = Logger.getLogger(BluetoothDeviceDiscoveryService.class.getName());
29  	
30  	private Object lock;
31  	/**
32  	 * found bluetooth devices
33  	 */
34  	private Vector<RemoteDevice> remoteDevices;
35  	/**
36  	 * created devices
37  	 */
38  	private Set<AbstractGestureDevice<?,?>> devices;
39  	private BluetoothDeviceConverter convertor;
40  	
41  	/**
42  	 * Constructor
43  	 */
44  	public BluetoothDeviceDiscoveryService()
45  	{
46  		devices = new HashSet<AbstractGestureDevice<?,?>>();
47  		remoteDevices = new Vector<RemoteDevice>();
48  		lock = new Object();
49  		convertor = new BluetoothDeviceConverter();
50  		
51  	}
52  	
53  	@Override
54  	public Set<AbstractGestureDevice<?, ?>> discover() {
55  		
56  		LOGGER.log(Level.INFO,"Device discovery started!");
57  		
58  		String bluecoveVersion = LocalDevice.getProperty("bluecove");
59          if(!bluecoveVersion.equals("")) {
60              String l2capFeature = LocalDevice.getProperty("bluecove.feature.l2cap");
61  
62              if(l2capFeature.equals("true")) {
63                  // set min id for Bluecove
64                  System.setProperty("bluecove.jsr82.psm_minimum_off", "true");
65              }
66          }
67  		
68  		
69  		LocalDevice localDevice;
70  		try {
71  			localDevice = LocalDevice.getLocalDevice();
72  			LOGGER.log(Level.INFO,"Your Computers Bluetooth MAC: " + localDevice.getBluetoothAddress());
73  			LOGGER.log(Level.INFO,"Starting device inquiry...");
74  	        DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
75  			discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
76  		} catch (BluetoothStateException e) {
77  			LOGGER.log(Level.SEVERE,"Problem with BlueTooth connection.",e);
78  		}
79  
80          try {
81              synchronized (lock) {
82                  lock.wait();
83              }
84          } catch (InterruptedException e) {
85          	LOGGER.log(Level.SEVERE,"Problems during device discovery.",e);
86          }
87  
88          LOGGER.log(Level.INFO,"Device discovery completed!");
89          
90  		return devices;
91  	}
92  
93  	@Override
94  	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass deviceClass) {
95  		LOGGER.log(Level.INFO,"Device discovered: " + btDevice.getBluetoothAddress());
96  		try {
97  			LOGGER.log(Level.INFO,"Device: "+btDevice.getFriendlyName(false)
98  					+", minor: "+ deviceClass.getMinorDeviceClass()
99  					+", major: "+deviceClass.getMajorDeviceClass());
100 		} catch (IOException e) {
101 			LOGGER.log(Level.WARNING,"Could not get friendly name from BlueTooth device",e);
102 			LOGGER.log(Level.INFO,"Device: minor: "+ deviceClass.getMinorDeviceClass()
103 					+", major: "+deviceClass.getMajorDeviceClass());
104 		}
105 		
106 		//if the devices was not found yet
107         if (!remoteDevices.contains(btDevice))
108         {
109         	//add it to the found devices
110             remoteDevices.addElement(btDevice);
111             //create a AbstractGestureDevice
112             AbstractGestureDevice<?, ?> dev = convertor.createDevice(btDevice, deviceClass);
113             //add it to the created devices
114             if(dev != null)
115             	devices.add(dev);
116         }
117 		
118 	}
119 
120 	@Override
121 	public void inquiryCompleted(int discType) {
122 		synchronized (this.lock) {
123             this.lock.notify();
124         }
125 		
126 		switch (discType) 
127 		{
128         case DiscoveryListener.INQUIRY_COMPLETED :
129             LOGGER.log(Level.INFO,"INQUIRY_COMPLETED");
130             break;
131             
132         case DiscoveryListener.INQUIRY_TERMINATED :
133         	LOGGER.log(Level.INFO,"INQUIRY_TERMINATED");
134             break;
135             
136         case DiscoveryListener.INQUIRY_ERROR :
137         	LOGGER.log(Level.WARNING,"INQUIRY_ERROR");
138             break;
139 
140         default :
141         	LOGGER.log(Level.WARNING,"Unknown Response Code");
142             break;
143 		}
144 	}
145 
146 	@Override
147 	public void serviceSearchCompleted(int transID, int respCode) {
148 		//not necessary
149 	}
150 
151 	@Override
152 	public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
153 		//not necessary
154 	}
155 
156 	@Override
157 	public void dispose() {
158 		devices.clear();
159 		remoteDevices.clear();	
160 	}
161 
162 }