My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.util.Hashtable;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

/**
* Implementation of DiscoveryAgent.selectService().
*
* <p>
* <b><u>Your application should not use this class directly.</u></b>
*
*/
public class SelectServiceHandler implements DiscoveryListener {

private DiscoveryAgent agent;

private Object inquiryCompletedEvent = new Object();

private boolean inquiryCompleted;

private Object serviceSearchCompletedEvent = new Object();

private boolean serviceSearchCompleted;

private Hashtable devicesProcessed = new Hashtable();

private Vector serviceSearchDeviceQueue = new Vector();

private ServiceRecord servRecordDiscovered;

private static int threadNumber;

private static synchronized int nextThreadNum() {
return threadNumber++;
}

public SelectServiceHandler(DiscoveryAgent agent) {
this.agent = agent;
}

/**
* Attempts to locate a service that contains <code>uuid</code> in the
* ServiceClassIDList of its service record. This method will return a
* string that may be used in <code>Connector.open()</code> to establish a
* connection to the service. How the service is selected if there are
* multiple services with <code>uuid</code> and which devices to search is
* implementation dependent.
*
* @see ServiceRecord#NOAUTHENTICATE_NOENCRYPT
* @see ServiceRecord#AUTHENTICATE_NOENCRYPT
* @see ServiceRecord#AUTHENTICATE_ENCRYPT
*
* @param uuid
* the UUID to search for in the ServiceClassIDList
*
* @param security
* specifies the security requirements for a connection to this
* service; must be one of
* <code>ServiceRecord.NOAUTHENTICATE_NOENCRYPT</code>,
* <code>ServiceRecord.AUTHENTICATE_NOENCRYPT</code>, or
* <code>ServiceRecord.AUTHENTICATE_ENCRYPT</code>
*
* @param master
* determines if this client must be the master of the
* connection; <code>true</code> if the client must be the
* master; <code>false</code> if the client can be the master
* or the slave
*
* @return the connection string used to connect to the service with a UUID
* of <code>uuid</code>; or <code>null</code> if no service
* could be found with a UUID of <code>uuid</code> in the
* ServiceClassIDList
*
* @exception BluetoothStateException
* if the Bluetooth system cannot start the request due to
* the current state of the Bluetooth system
*
* @exception NullPointerException
* if <code>uuid</code> is <code>null</code>
*
* @exception IllegalArgumentException
* if <code>security</code> is not
* <code>ServiceRecord.NOAUTHENTICATE_NOENCRYPT</code>,
* <code>ServiceRecord.AUTHENTICATE_NOENCRYPT</code>, or
* <code>ServiceRecord.AUTHENTICATE_ENCRYPT</code>
*/
public String selectService(UUID uuid, int security, boolean master) throws BluetoothStateException {
if (uuid == null) {
throw new NullPointerException("uuid is null");
}
switch (security) {
case ServiceRecord.NOAUTHENTICATE_NOENCRYPT:
case ServiceRecord.AUTHENTICATE_NOENCRYPT:
case ServiceRecord.AUTHENTICATE_ENCRYPT:
break;
default:
throw new IllegalArgumentException();
}

RemoteDevice[] devs = agent.retrieveDevices(DiscoveryAgent.PREKNOWN);
for (int i = 0; (devs != null) && (i < devs.length); i++) {
ServiceRecord sr = findServiceOnDevice(uuid, devs[i]);
if (sr != null) {
return sr.getConnectionURL(security, master);
}
}
devs = agent.retrieveDevices(DiscoveryAgent.CACHED);
for (int i = 0; (devs != null) && (i < devs.length); i++) {
ServiceRecord sr = findServiceOnDevice(uuid, devs[i]);
if (sr != null) {
return sr.getConnectionURL(security, master);
}
}
ParallelSearchServicesThread t = new ParallelSearchServicesThread(uuid);
t.start();

synchronized (inquiryCompletedEvent) {
if (!agent.startInquiry(DiscoveryAgent.GIAC, this)) {
return null;
}
while (!inquiryCompleted) {
try {
inquiryCompletedEvent.wait();
} catch (InterruptedException e) {
return null;
}
}
agent.cancelInquiry(this);
}

if ((servRecordDiscovered == null) && (!t.processedAll())) {
synchronized (serviceSearchDeviceQueue) {
serviceSearchDeviceQueue.notifyAll();
}
try {
t.join();
} catch (InterruptedException e) {
return null;
}
}
t.interrupt();

if (servRecordDiscovered != null) {
return servRecordDiscovered.getConnectionURL(security, master);
}

return null;
}

private class ParallelSearchServicesThread extends Thread {

private boolean stoped = false;

private int processedNext = 0;

private int processedSize = 0;

private UUID uuid;

ParallelSearchServicesThread(UUID uuid) {
super("SelectServiceThread-" + nextThreadNum());
this.uuid = uuid;
}

boolean processedAll() {
return (processedNext == serviceSearchDeviceQueue.size());
}

public void interrupt() {
stoped = true;
synchronized (serviceSearchDeviceQueue) {
serviceSearchDeviceQueue.notifyAll();
}
super.interrupt();
}

public void run() {
mainLoop: while ((!stoped) && (servRecordDiscovered == null)) {
synchronized (serviceSearchDeviceQueue) {
if ((inquiryCompleted) && (processedSize == serviceSearchDeviceQueue.size())) {
return;
}
if (processedSize == serviceSearchDeviceQueue.size()) {
try {
serviceSearchDeviceQueue.wait();
} catch (InterruptedException e) {
return;
}
}
processedSize = serviceSearchDeviceQueue.size();
}
for (int i = processedNext; i < processedSize; i++) {
RemoteDevice btDevice = (RemoteDevice) serviceSearchDeviceQueue.elementAt(i);
if (findServiceOnDevice(uuid, btDevice) != null) {
break mainLoop;
}
}
processedNext = processedSize + 1;
}
}

}

private ServiceRecord findServiceOnDevice(UUID uuid, RemoteDevice device) {
if (devicesProcessed.containsKey(device)) {
return null;
}
devicesProcessed.put(device, device);
DebugLog.debug("searchServices on ", device);
synchronized (serviceSearchCompletedEvent) {
try {
serviceSearchCompleted = false;
agent.searchServices(null, new UUID[] { uuid }, device, this);
} catch (BluetoothStateException e) {
DebugLog.error("searchServices", e);
return null;
}
while (!serviceSearchCompleted) {
try {
serviceSearchCompletedEvent.wait();
} catch (InterruptedException e) {
return null;
}
}
}
return servRecordDiscovered;
}

public void deviceDiscovered(final RemoteDevice btDevice, DeviceClass cod) {
if (devicesProcessed.containsKey(btDevice)) {
return;
}
synchronized (serviceSearchDeviceQueue) {
serviceSearchDeviceQueue.addElement(btDevice);
serviceSearchDeviceQueue.notifyAll();
}
}

public void inquiryCompleted(int discType) {
synchronized (inquiryCompletedEvent) {
inquiryCompleted = true;
inquiryCompletedEvent.notifyAll();
}
}

public void serviceSearchCompleted(int transID, int respCode) {
synchronized (serviceSearchCompletedEvent) {
serviceSearchCompleted = true;
serviceSearchCompletedEvent.notifyAll();
}
}

public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
if ((servRecord.length > 0) && (servRecordDiscovered == null)) {
servRecordDiscovered = servRecord[0];
synchronized (serviceSearchCompletedEvent) {
serviceSearchCompleted = true;
serviceSearchCompletedEvent.notifyAll();
}
synchronized (inquiryCompletedEvent) {
inquiryCompleted = true;
inquiryCompletedEvent.notifyAll();
}
}
}
}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
r2408 by skarzhevskyy on Oct 9, 2008   Diff
organize product to modules
All revisions of this file

File info

Size: 8985 bytes, 298 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting