1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48
49
50 public class BluetoothDiscoveryMojo extends AbstractMojo {
51
52
53
54
55
56
57
58
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
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 }