View Javadoc

1   /**
2    * Pyx4me framework
3    * Copyright (C) 2006-2008 pyx4j.com.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing,
12   * software distributed under the License is distributed on an
13   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14   * KIND, either express or implied.  See the License for the
15   * specific language governing permissions and limitations
16   * under the License.
17   * 
18   * @author vlads
19   * @version $Id: BluetoothDiscoveryMojo.java 2066 2008-06-21 23:16:18Z vlads $
20   */
21  package com.pyx4me.maven.obex;
22  
23  import java.io.IOException;
24  import java.util.Iterator;
25  import java.util.Vector;
26  
27  import javax.bluetooth.BluetoothStateException;
28  import javax.bluetooth.DeviceClass;
29  import javax.bluetooth.DiscoveryAgent;
30  import javax.bluetooth.DiscoveryListener;
31  import javax.bluetooth.LocalDevice;
32  import javax.bluetooth.RemoteDevice;
33  import javax.bluetooth.ServiceRecord;
34  
35  import org.apache.maven.plugin.AbstractMojo;
36  import org.apache.maven.plugin.MojoExecutionException;
37  import org.apache.maven.plugin.MojoFailureException;
38  import org.apache.maven.plugin.logging.Log;
39  import org.apache.maven.project.MavenProject;
40  
41  /**
42   * The obex:discovery task finds all Bluetooth devices that support OBEX Object
43   * Push.
44   * 
45   * @author vlads
46   * 
47   * @goal discovery
48   * @description Find all Bluetooth devices that support OBEX Object Push.
49   */
50  public class BluetoothDiscoveryMojo extends AbstractMojo {
51  
52  	/**
53  	 * The Maven project reference where the plugin is currently being executed.
54  	 * The default value is populated from maven.
55  	 * 
56  	 * @parameter expression="${project}"
57  	 * @readonly
58  	 * @required
59  	 */
60  	protected MavenProject mavenProject;
61  
62  	private Log log;
63  
64  	private Vector devices;
65  
66  	private class BluetoothInquirer implements DiscoveryListener {
67  
68  		boolean inquiring;
69  
70  		public boolean startInquiry() {
71  			inquiring = false;
72  			try {
73  				inquiring = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, this);
74  			} catch (BluetoothStateException e) {
75  				log.error("Cannot start inquiry", e);
76  				return false;
77  			}
78  			return inquiring;
79  		}
80  
81  		public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
82  			devices.add(btDevice);
83  			try {
84  				String name = btDevice.getFriendlyName(false);
85  				log
86  						.info("Found " + btDevice.getBluetoothAddress() + " " + name + " "
87  								+ DeviceClassConsts.toString(cod));
88  			} catch (IOException ioe) {
89  				log.info("Found " + btDevice.getBluetoothAddress() + " " + DeviceClassConsts.toString(cod));
90  			}
91  		}
92  
93  		public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
94  		}
95  
96  		public void serviceSearchCompleted(int transID, int respCode) {
97  		}
98  
99  		public void inquiryCompleted(int discType) {
100 			inquiring = false;
101 		}
102 
103 	}
104 
105 	public void execute() throws MojoExecutionException, MojoFailureException {
106 
107 		// System.setProperty("bluecove.debug", "true");
108 
109 		log = getLog();
110 		devices = new Vector();
111 
112 		BluetoothInquirer bi = new BluetoothInquirer();
113 		log.info("Starting Device inquiry");
114 		if (!bi.startInquiry()) {
115 			log.error("Cannot start inquiry");
116 		} else {
117 			while (bi.inquiring) {
118 				try {
119 					Thread.sleep(1000);
120 				} catch (Exception e) {
121 				}
122 			}
123 		}
124 
125 		for (Iterator iter = devices.iterator(); iter.hasNext();) {
126 			RemoteDevice dev = (RemoteDevice) iter.next();
127 			ServiceSearchMojo.findOBEX(dev.getBluetoothAddress(), null, log);
128 		}
129 	}
130 
131 }