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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/**
* 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.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.microedition.io.Connection;

import com.intel.bluetooth.WeakVectorFactory.WeakVector;

/**
* Implementation of RemoteDevice.
*
* Instance of RemoteDevice can be created by User. BlueCove internaly should
* use only RemoteDeviceHelper class to create RemoteDevice instances.
*
* <p>
* <b><u>Your application should not use this class directly to be JSR-82
* compatible.</u></b>
*
* The exceptions are authenticate(RemoteDevice device, String passkey) and
* removeAuthentication(RemoteDevice device).
*/
public abstract class RemoteDeviceHelper {

private static class RemoteDeviceWithExtendedInfo extends RemoteDevice {

String name;

long addressLong;

BluetoothStack bluetoothStack;

private Hashtable stackAttributes;

private boolean paired;

/**
* Connections can be discarded by the garbage collector.
*/
private WeakVector connections;

private RemoteDeviceWithExtendedInfo(BluetoothStack bluetoothStack, long address, String name) {
super(RemoteDeviceHelper.getBluetoothAddress(address));
this.bluetoothStack = bluetoothStack;
this.name = name;
this.addressLong = address;
}

private void addConnection(Object connection) {
synchronized (this) {
if (connections == null) {
connections = WeakVectorFactory.createWeakVector();
}
}
synchronized (connections) {
connections.addElement(connection);
DebugLog.debug("connection open, open now", connections.size());
}
}

private void removeConnection(Object connection) {
if (connections == null) {
return;
}
synchronized (connections) {
connections.removeElement(connection);
DebugLog.debug("connection closed, open now", connections.size());
}
}

void shutdownConnections() {
if (!hasConnections()) {
return;
}
Vector c2shutdown = new Vector();
synchronized (connections) {
c2shutdown = Utils.clone(connections.elements());
}
for (Enumeration en = c2shutdown.elements(); en.hasMoreElements();) {
BluetoothConnectionAccess c = (BluetoothConnectionAccess) en.nextElement();
try {
c.shutdown();
} catch (IOException e) {
DebugLog.debug("connection shutdown", e);
}
}
synchronized (connections) {
connections.removeAllElements();
}
}

private void setStackAttributes(Object key, Object value) {
if (stackAttributes == null) {
stackAttributes = new Hashtable();
}
if (value == null) {
stackAttributes.remove(key);
} else {
stackAttributes.put(key, value);
}
}

private Object getStackAttributes(Object key) {
if (stackAttributes == null) {
return null;
}
return stackAttributes.get(key);
}

public String toString() {
return super.getBluetoothAddress();
}

int connectionsCount() {
if (connections == null) {
return 0;
}
return connections.size();
}

boolean hasConnections() {
return (connectionsCount() != 0);
}

/**
* @see javax.bluetooth.RemoteDevice#authenticate()
*/
public boolean authenticate() throws IOException {
if (!hasConnections()) {
throw new IOException("No open connections to this RemoteDevice");
}
if (this.isAuthenticated()) {
// has previously been authenticated
return true;
}
boolean authenticated = bluetoothStack.authenticateRemoteDevice(addressLong);
paired = authenticated;
if (authenticated) {
updateConnectionMarkAuthenticated();
}
return authenticated;
}

/**
* @see com.intel.bluetooth.RemoteDeviceHelper#authenticateRemoteDevice(RemoteDevice,
* java.lang.String)
*/
boolean authenticate(String passkey) throws IOException {
boolean authenticated = bluetoothStack.authenticateRemoteDevice(addressLong, passkey);
paired = authenticated;
if (authenticated) {
updateConnectionMarkAuthenticated();
}
return authenticated;
}

void removeAuthentication() throws IOException {
bluetoothStack.removeAuthenticationWithRemoteDevice(addressLong);
paired = false;
}

private void updateConnectionMarkAuthenticated() {
if (connections == null) {
return;
}
synchronized (connections) {
for (Enumeration en = connections.elements(); en.hasMoreElements();) {
BluetoothConnectionAccess c = (BluetoothConnectionAccess) en.nextElement();
c.markAuthenticated();
}
}
}

/**
* Determines if this RemoteDevice should be allowed to continue to
* access the local service provided by the Connection.
*
* @see javax.bluetooth.RemoteDevice#authorize(javax.microedition.io.Connection)
*/
public boolean authorize(Connection conn) throws IOException {
if (!(conn instanceof BluetoothConnectionAccess)) {
throw new IllegalArgumentException("Connection is not a Bluetooth connection");
}
if (((BluetoothConnectionAccess) conn).isClosed()) {
throw new IOException("Connection is already closed");
}
if (!(conn instanceof BluetoothServerConnection)) {
throw new IllegalArgumentException("Connection is not an incomming Bluetooth connection");
}
return isTrustedDevice() || isAuthenticated();
}

/**
*
* @see javax.bluetooth.RemoteDevice#isAuthorized(javax.microedition.io.Connection)
*/
public boolean isAuthorized(Connection conn) throws IOException {
if (!(conn instanceof BluetoothConnectionAccess)) {
throw new IllegalArgumentException("Connection is not a Bluetooth connection");
}
if (((BluetoothConnectionAccess) conn).isClosed()) {
throw new IOException("Connection is already closed");
}
if (!(conn instanceof BluetoothServerConnection)) {
throw new IllegalArgumentException("Connection is not an incomming Bluetooth connection");
}
return isTrustedDevice();
}

/**
* Attempts to turn encryption on or off for an existing connection.
*
* @see javax.bluetooth.RemoteDevice#encrypt(javax.microedition.io.Connection,
* boolean)
*/
public boolean encrypt(Connection conn, boolean on) throws IOException {
if (!(conn instanceof BluetoothConnectionAccess)) {
throw new IllegalArgumentException("Connection is not a Bluetooth connection");
}
if (((BluetoothConnectionAccess) conn).getRemoteAddress() != this.addressLong) {
throw new IllegalArgumentException("Connection is not to this device");
}
if ((((BluetoothConnectionAccess) conn).getSecurityOpt() == ServiceRecord.AUTHENTICATE_ENCRYPT) == on) {
return true;
}
return ((BluetoothConnectionAccess) conn).encrypt(this.addressLong, on);
}

/*
* (non-Javadoc)
*
* @see javax.bluetooth.RemoteDevice#isAuthenticated()
*/
public boolean isAuthenticated() {
if (!hasConnections()) {
DebugLog.debug("no connections, Authenticated = false");
return false;
}
Boolean authenticated = bluetoothStack.isRemoteDeviceAuthenticated(addressLong);
if (authenticated != null) {
return authenticated.booleanValue();
}
synchronized (connections) {
// Find first authenticated connection
for (Enumeration en = connections.elements(); en.hasMoreElements();) {
BluetoothConnectionAccess c = (BluetoothConnectionAccess) en.nextElement();
if (c.getSecurityOpt() != ServiceRecord.NOAUTHENTICATE_NOENCRYPT) {
return true;
}
}
}
return false;
}

/*
* (non-Javadoc)
*
* @see javax.bluetooth.RemoteDevice#isEncrypted()
*/
public boolean isEncrypted() {
if (!hasConnections()) {
return false;
}
synchronized (connections) {
// Find first encrypted connection
for (Enumeration en = connections.elements(); en.hasMoreElements();) {
BluetoothConnectionAccess c = (BluetoothConnectionAccess) en.nextElement();
if (c.getSecurityOpt() == ServiceRecord.AUTHENTICATE_ENCRYPT) {
return true;
}
}
}
return false;
}

/*
* (non-Javadoc)
*
* @see javax.bluetooth.RemoteDevice#isTrustedDevice()
*/
public boolean isTrustedDevice() {
Boolean trusted = bluetoothStack.isRemoteDeviceTrusted(addressLong);
if (trusted == null) {
return paired;
} else {
return trusted.booleanValue();
}
}
}

private static Hashtable stackDevicesCashed = new Hashtable();

private RemoteDeviceHelper() {

}

private static synchronized Hashtable devicesCashed(BluetoothStack bluetoothStack) {
Hashtable devicesCashed = (Hashtable) stackDevicesCashed.get(bluetoothStack);
if (devicesCashed == null) {
devicesCashed = new Hashtable();
stackDevicesCashed.put(bluetoothStack, devicesCashed);
}
return devicesCashed;
}

private static RemoteDeviceWithExtendedInfo getCashedDeviceWithExtendedInfo(BluetoothStack bluetoothStack, long address) {
Object key = new Long(address);
return (RemoteDeviceWithExtendedInfo) devicesCashed(bluetoothStack).get(key);
}

static RemoteDevice getCashedDevice(BluetoothStack bluetoothStack, long address) {
return getCashedDeviceWithExtendedInfo(bluetoothStack, address);
}

static RemoteDevice getStackBoundDevice(BluetoothStack bluetoothStack, RemoteDevice device) {
if ((device instanceof RemoteDeviceWithExtendedInfo) && (((RemoteDeviceWithExtendedInfo)device).bluetoothStack == bluetoothStack)) {
return device;
}
return createRemoteDevice(bluetoothStack, getAddress(device), null, false);
}

static RemoteDevice createRemoteDevice(BluetoothStack bluetoothStack, long address, String name, boolean paired) {
RemoteDeviceWithExtendedInfo dev = getCashedDeviceWithExtendedInfo(bluetoothStack, address);
if (dev == null) {
Object saveID = BlueCoveImpl.getCurrentThreadBluetoothStackID();
try {
BlueCoveImpl.setThreadBluetoothStack(bluetoothStack);
dev = new RemoteDeviceWithExtendedInfo(bluetoothStack, address, name);
} finally {
if (saveID != null) {
BlueCoveImpl.setThreadBluetoothStackID(saveID);
}
}
devicesCashed(bluetoothStack).put(new Long(address), dev);
DebugLog.debug0x("new devicesCashed", address);
} else if (!Utils.isStringSet(dev.name)) {
// name found
dev.name = name;
} else if (Utils.isStringSet(name)) {
// Update name if changed
dev.name = name;
}
if (paired) {
dev.paired = paired;
}
return dev;
}

private static BluetoothStack getBluetoothStack() throws RuntimeException {
try {
return BlueCoveImpl.instance().getBluetoothStack();
} catch (BluetoothStateException e) {
throw (RuntimeException) UtilsJavaSE.initCause(new RuntimeException("Can't initialize bluetooth support"), e);
}
}

private static RemoteDeviceWithExtendedInfo remoteDeviceImpl(RemoteDevice device) {
return (RemoteDeviceWithExtendedInfo) createRemoteDevice(null, device);
}

static RemoteDevice createRemoteDevice(BluetoothStack bluetoothStack, RemoteDevice device) throws RuntimeException {
if (device instanceof RemoteDeviceWithExtendedInfo) {
return device;
} else {
if (bluetoothStack == null) {
bluetoothStack = getBluetoothStack();
}
return createRemoteDevice(bluetoothStack, getAddress(device), null, false);
}
}

static RemoteDevice[] remoteDeviceListToArray(Vector devices) {
RemoteDevice[] devicesArray = new RemoteDevice[devices.size()];
int i = 0;
for (Enumeration en = devices.elements(); en.hasMoreElements();) {
devicesArray[i++] = (RemoteDevice) en.nextElement();
}
return devicesArray;
}

/**
* Count total number of open connections to all devices.
*
* @return number of connections
*/
public static int openConnections() {
int c = 0;
Hashtable devicesCashed = devicesCashed(getBluetoothStack());
synchronized (devicesCashed) {
for (Enumeration en = devicesCashed.elements(); en.hasMoreElements();) {
c += ((RemoteDeviceWithExtendedInfo) en.nextElement()).connectionsCount();
}
}
return c;
}

/**
* Count number of open connections to or from specific device.
*
* @return number of connections
*/
public static int openConnections(long address) {
RemoteDeviceWithExtendedInfo dev = getCashedDeviceWithExtendedInfo(getBluetoothStack(), address);
if (dev == null) {
return 0;
}
return dev.connectionsCount();
}

/**
* Count number of device that have open connections to or from them.
*
* @return number of connections
*/
public static int connectedDevices() {
int c = 0;
Hashtable devicesCashed = devicesCashed(getBluetoothStack());
synchronized (devicesCashed) {
for (Enumeration en = devicesCashed.elements(); en.hasMoreElements();) {
if (((RemoteDeviceWithExtendedInfo) en.nextElement()).hasConnections()) {
c++;
}
}
}
return c;
}

static void shutdownConnections(BluetoothStack bluetoothStack) {
Hashtable devicesCashed = devicesCashed(bluetoothStack);
synchronized (devicesCashed) {
for (Enumeration en = devicesCashed.elements(); en.hasMoreElements();) {
((RemoteDeviceWithExtendedInfo) en.nextElement()).shutdownConnections();
}
}
}

public static String formatBluetoothAddress(String address) {
String s = address.toUpperCase();
return "000000000000".substring(s.length()) + s;
}

/**
* Convert Bluetooth address long representation to String presentation.
*
* @param address
* @return the Bluetooth address of the device 12 characters long.
*/
public static String getBluetoothAddress(long address) {
return formatBluetoothAddress(Utils.toHexString(address));
}

/**
* Convert Bluetooth address String representation to long.
*
* @param bluetoothAddress
* @return long value of the address
*/
public static long getAddress(String bluetoothAddress) {
if (bluetoothAddress.indexOf('-') != -1) {
throw new IllegalArgumentException("Illegal bluetoothAddress {" + bluetoothAddress + "}");
}
try {
return Long.parseLong(bluetoothAddress, 0x10);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Illegal bluetoothAddress {" + bluetoothAddress + "}; should be hex number");
}
}

static long getAddress(RemoteDevice device) {
if (device instanceof RemoteDeviceWithExtendedInfo) {
return ((RemoteDeviceWithExtendedInfo) device).addressLong;
} else {
return getAddress(device.getBluetoothAddress());
}
}

static void setStackAttributes(BluetoothStack bluetoothStack, RemoteDevice device, Object key, Object value) {
RemoteDeviceWithExtendedInfo devInfo = (RemoteDeviceWithExtendedInfo) createRemoteDevice(bluetoothStack, device);
devInfo.setStackAttributes(key, value);
}

static Object getStackAttributes(BluetoothStack bluetoothStack, RemoteDevice device, Object key) {
RemoteDeviceWithExtendedInfo devInfo = null;
if (device instanceof RemoteDeviceWithExtendedInfo) {
devInfo = (RemoteDeviceWithExtendedInfo) device;
} else {
devInfo = getCashedDeviceWithExtendedInfo(bluetoothStack, getAddress(device));
}

if (devInfo != null) {
return devInfo.getStackAttributes(key);
} else {
return null;
}
}

static void connected(BluetoothConnectionAccess connection) throws IOException {
RemoteDeviceWithExtendedInfo device = (RemoteDeviceWithExtendedInfo) implGetRemoteDevice((Connection) connection);
connection.setRemoteDevice(device);
device.addConnection(connection);
}

static void disconnected(BluetoothConnectionAccess connection) {
RemoteDevice d = connection.getRemoteDevice();
if (d != null) {
((RemoteDeviceWithExtendedInfo) d).removeConnection(connection);
connection.setRemoteDevice(null);
}
}

/**
* Gets the RSSI (Receive Signal Strength Indicator) value for a remote
* Bluetooth device. Non JSR-82.
* <p>
* <b>PUBLIC JSR-82 extension</b>
* <p>
* Bluetooth connection with the peer device should exist to receive a
* value.
* <p>
* Value 0 indicates that the connected Bluetooth devices are at optimal
* separation, the golden zone.
* <p>
* Increasingly positive values are reported as the devices are moved closer
* to each other. Increasingly negative values are reported as the devices
* are moved apart.
* <p>
* RSSI ranges from -128 to 127.
*
* @param device
* Remote Device
* @return RSSI value
* @throws IOException
* if there are error reading value.
*/
public static int readRSSI(RemoteDevice device) throws IOException {
RemoteDeviceWithExtendedInfo deviceImpl = remoteDeviceImpl(device);
if (deviceImpl.bluetoothStack instanceof BluetoothStackExtension) {
return ((BluetoothStackExtension) deviceImpl.bluetoothStack).readRemoteDeviceRSSI(deviceImpl.addressLong);
} else {
throw new NotSupportedIOException(deviceImpl.bluetoothStack.getStackID());
}
}

/**
* Attempts to authenticate RemoteDevice. Return <code>false</code> if the
* stack does not support authentication.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.authenticate().
* <P>
*
* @see javax.bluetooth.RemoteDevice#authenticate()
*/
public static boolean authenticate(RemoteDevice device) throws IOException {
return remoteDeviceImpl(device).authenticate();
}

/**
*
* Sends an authentication request to a remote Bluetooth device. Non JSR-82,
* Return <code>false</code> if the stack does not support authentication.
* <p>
* <b>PUBLIC JSR-82 extension</b>
*
* On BlueZ will throw exception when authentication already exists.
*
* @param device
* Remote Device
* @param passkey
* A Personal Identification Number (PIN) to be used for device
* authentication.
* @return <code>true</code> if authentication is successful; otherwise
* <code>false</code>
* @throws IOException
* if there are error during authentication.
*/
public static boolean authenticate(RemoteDevice device, String passkey) throws IOException {
return remoteDeviceImpl(device).authenticate(passkey);
}

/**
*
* Removes authentication between local and remote bluetooth devices. Non
* JSR-82.
* <p>
* <b>PUBLIC JSR-82 extension</b>
*
* @param device
* Remote Device
*
* @throws IOException
* if there are errors or not implemented.
*/
public static void removeAuthentication(RemoteDevice device) throws IOException {
remoteDeviceImpl(device).removeAuthentication();
}

/**
* Returns the name of the device.
*
* Returns <code>null</code> if the Bluetooth system does not support this
* feature; If the remote device does not have a name then an empty string.
*
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.getFriendlyName(...).
* <P>
*/
public static String implGetFriendlyName(RemoteDevice device, long address, boolean alwaysAsk) throws IOException {
if (!(device instanceof RemoteDeviceWithExtendedInfo)) {
device = createRemoteDevice(null, device);
}
String name = ((RemoteDeviceWithExtendedInfo) device).name;
if (alwaysAsk || (name == null)) {
name = ((RemoteDeviceWithExtendedInfo) device).bluetoothStack.getRemoteDeviceFriendlyName(address);
if (name != null) {
((RemoteDeviceWithExtendedInfo) device).name = name;
}
}
return name;
}

/**
* Retrieves the Bluetooth device that is at the other end of the Bluetooth
* connection.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.getRemoteDevice(...).
* <P>
*
* @see javax.bluetooth.RemoteDevice#getRemoteDevice(Connection)
*/
public static RemoteDevice implGetRemoteDevice(Connection conn) throws IOException {
if (!(conn instanceof BluetoothConnectionAccess)) {
throw new IllegalArgumentException("Not a Bluetooth connection " + conn.getClass().getName());
}
return createRemoteDevice(((BluetoothConnectionAccess) conn).getBluetoothStack(), ((BluetoothConnectionAccess) conn).getRemoteAddress(), null, false);
}

/**
* Returns an array of Bluetooth devices.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.retrieveDevices(...).
* <P>
*
* @see javax.bluetooth.DiscoveryAgent#retrieveDevices(int)
*/
public static RemoteDevice[] implRetrieveDevices(BluetoothStack bluetoothStack, int option) {
if ((option != DiscoveryAgent.PREKNOWN) && (option != DiscoveryAgent.CACHED)) {
throw new IllegalArgumentException("invalid option");
}
RemoteDevice[] impl = bluetoothStack.retrieveDevices(option);
if (impl != null) {
if (impl.length == 0) {
// Spec: null if no devices meet the criteria
return null;
} else {
return impl;
}
}

Hashtable devicesCashed = devicesCashed(bluetoothStack);
switch (option) {
case DiscoveryAgent.PREKNOWN:
if (devicesCashed.size() == 0) {
// Spec: null if no devices meet the criteria
return null;
}
Vector devicesPaired = new Vector();
for (Enumeration en = devicesCashed.elements(); en.hasMoreElements();) {
RemoteDeviceWithExtendedInfo d = (RemoteDeviceWithExtendedInfo) en.nextElement();
if (d.isTrustedDevice()) {
devicesPaired.addElement(d);
}
}
if (devicesPaired.size() == 0) {
// Spec: null if no devices meet the criteria
return null;
}
return remoteDeviceListToArray(devicesPaired);
case DiscoveryAgent.CACHED:
if (devicesCashed.size() == 0) {
// Spec: null if no devices meet the criteria
return null;
}
RemoteDevice[] devices = new RemoteDevice[devicesCashed.size()];
int k = 0;
for (Enumeration en = devicesCashed.elements(); en.hasMoreElements();) {
devices[k++] = (RemoteDevice) en.nextElement();
}
return devices;
default:
throw new IllegalArgumentException("invalid option");
}
}

/**
* Determines if this RemoteDevice should be allowed to continue to access
* the local service provided by the Connection.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.authorize(...).
* <P>
*
* @see javax.bluetooth.RemoteDevice#authorize(javax.microedition.io.Connection)
*/
public static boolean implAuthorize(RemoteDevice device, Connection conn) throws IOException {
return remoteDeviceImpl(device).authorize(conn);
}

/**
* Attempts to turn encryption on or off for an existing connection.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.encrypt(...).
* <P>
*
* @see javax.bluetooth.RemoteDevice#encrypt(javax.microedition.io.Connection,
* boolean)
*/
public static boolean implEncrypt(RemoteDevice device, Connection conn, boolean on) throws IOException {
return remoteDeviceImpl(device).encrypt(conn, on);
}

/**
* Determines if this <code>RemoteDevice</code> has been authenticated.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.isAuthenticated().
* <P>
* A device may have been authenticated by this application or another
* application. Authentication applies to an ACL link between devices and
* not on a specific L2CAP, RFCOMM, or OBEX connection. Therefore, if
* <code>authenticate()</code> is performed when an L2CAP connection is made
* to device A, then <code>isAuthenticated()</code> may return
* <code>true</code> when tested as part of making an RFCOMM connection to
* device A.
*
* @return <code>true</code> if this <code>RemoteDevice</code> has
* previously been authenticated; <code>false</code> if it has not
* been authenticated or there are no open connections between the
* local device and this <code>RemoteDevice</code>
*/
public static boolean implIsAuthenticated(RemoteDevice device) {
return remoteDeviceImpl(device).isAuthenticated();
}

/**
* Determines if this <code>RemoteDevice</code> has been authorized.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.isAuthorized(
* javax.microedition.io.Connection).
* <P>
*/
public static boolean implIsAuthorized(RemoteDevice device, Connection conn) throws IOException {
return remoteDeviceImpl(device).isAuthorized(conn);
}

/**
* Determines if data exchanges with this <code>RemoteDevice</code> are
* currently being encrypted.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.isEncrypted().
* <P>
* Encryption may have been previously turned on by this or another
* application. Encryption applies to an ACL link between devices and not on
* a specific L2CAP, RFCOMM, or OBEX connection. Therefore, if
* <code>encrypt()</code> is performed with the <code>on</code> parameter
* set to <code>true</code> when an L2CAP connection is made to device A,
* then <code>isEncrypted()</code> may return <code>true</code> when tested
* as part of making an RFCOMM connection to device A.
*
* @return <code>true</code> if data exchanges with this
* <code>RemoteDevice</code> are being encrypted; <code>false</code>
* if they are not being encrypted, or there are no open connections
* between the local device and this <code>RemoteDevice</code>
*/
public static boolean implIsEncrypted(RemoteDevice device) {
return remoteDeviceImpl(device).isEncrypted();
}

/**
* Determines if this is a trusted device according to the BCC.
* <P>
* Internal BlueCove function. Use
* javax.bluetooth.RemoteDevice.isTrustedDevice().
* <P>
*/
public static boolean implIsTrustedDevice(RemoteDevice device) {
return remoteDeviceImpl(device).isTrustedDevice();
}
}

Change log

r2985 by skarzhevskyy on Apr 17, 2009   Diff
Use getStackBoundDevice
Go to: 
Project members, sign in to write a code review

Older revisions

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
r2894 by skarzhevskyy on Mar 11, 2009   Diff
On BlueZ will throw exception when
authentication already exists.
r2876 by skarzhevskyy on Mar 9, 2009   Diff
allow device name to be empty string.
All revisions of this file

File info

Size: 31003 bytes, 825 lines

File properties

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