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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2004 Intel Corporation
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
* Copyright (C) 2010 Mina Shokry
*
* 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.security.AccessControlContext;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.bluetooth.BluetoothStateException;

import com.intel.bluetooth.BluetoothStack.LibraryInformation;

/**
*
* Singleton class used as holder for BluetoothStack.
*
* Under security manager all you need to do is initialize BlueCoveImpl inside Privileged
* context.
* <p>
* If automatic Bluetooth Stack detection is not enough Java System property
* "bluecove.stack" can be used to force desired Stack Initialization. Values "widcomm",
* "bluesoleil" or "winsock". By default winsock is selected if available.
* <p>
* Another property "bluecove.stack.first" is used optimize stack detection. If
* -Dbluecove.stack.first=widcomm then widcomm (bluecove.dll) stack is loaded first and if
* not available then BlueCove will switch to winsock. By default intelbth.dll is loaded
* first.
* <p>
* If multiple stacks are detected they are selected in following order: "winsock",
* "widcomm", "bluesoleil". Since BlueCove v2.0.1 "bluecove.stack.first" will alter the
* order of stack selection.
* <p>
* To use jsr-82 emulator set "bluecove.stack" value to "emulator".
* <p>
* If System property is not an option (e.g. when running in Webstart) create text file
* "bluecove.stack" or "bluecove.stack.first" containing stack name and add this file to
* BlueCove or Application jar. (Since v2.0.1)
* <p>
* Use `LocalDevice.getProperty("bluecove.stack")` to find out which stack is used.
*
*/
public class BlueCoveImpl {

public static final String BLUETOOTH_API_VERSION = "1.1.1";

public static final String OBEX_API_VERSION = BLUETOOTH_API_VERSION;

public static final int versionMajor1 = 2;

public static final int versionMajor2 = 1;

public static final int versionMinor = 1;

public static final int versionBuild = 0;

public static final String versionSufix = "-SNAPSHOT"; // SNAPSHOT

public static final String version = String.valueOf(versionMajor1) + "." + String.valueOf(versionMajor2) + "." + String.valueOf(versionMinor)
+ versionSufix;

public static final int nativeLibraryVersionExpected = versionMajor1 * 1000000 + versionMajor2 * 10000 + versionMinor * 100 + versionBuild;

public static final String STACK_WINSOCK = "winsock";

public static final String STACK_WIDCOMM = "widcomm";

public static final String STACK_BLUESOLEIL = "bluesoleil";

public static final String STACK_TOSHIBA = "toshiba";

public static final String STACK_BLUEZ = "bluez";

public static final String STACK_BLUEZ_DBUS = "bluez-dbus";

public static final String STACK_OSX = "mac";

public static final String STACK_EMULATOR = "emulator";

public static final String STACK_ANDROID_2_X = "android_2.x";

// We can't use the same DLL on windows for all implementations.
// Since WIDCOMM need to be compile /MD using VC6 and winsock /MT using
// VC2005
// This variable can be used to simplify development/test builds
private static final boolean oneDLLbuild = false;

public static final String NATIVE_LIB_MS = "intelbth";

public static final String NATIVE_LIB_WIDCOMM = oneDLLbuild ? NATIVE_LIB_MS : "bluecove";

public static final String NATIVE_LIB_TOSHIBA = "bluecove";

public static final String NATIVE_LIB_BLUEZ = "bluecove";

public static final String NATIVE_LIB_OSX = "bluecove";

/**
* To work on BlueSoleil version 2.3 we need to compile C++ code /MT the same as
* winsock.
*/
public static final String NATIVE_LIB_BLUESOLEIL = NATIVE_LIB_MS;

static final int BLUECOVE_STACK_DETECT_MICROSOFT = 1;

static final int BLUECOVE_STACK_DETECT_WIDCOMM = 1 << 1;

static final int BLUECOVE_STACK_DETECT_BLUESOLEIL = 1 << 2;

static final int BLUECOVE_STACK_DETECT_TOSHIBA = 1 << 3;

static final int BLUECOVE_STACK_DETECT_OSX = 1 << 4;

public static final int BLUECOVE_STACK_DETECT_BLUEZ = 1 << 5;

public static final int BLUECOVE_STACK_DETECT_EMULATOR = 1 << 6;

public static final int BLUECOVE_STACK_DETECT_BLUEZ_DBUS = 1 << 7;

public static final int BLUECOVE_STACK_DETECT_ANDROID_1_X = 1 << 8;

public static final int BLUECOVE_STACK_DETECT_ANDROID_2_X = 1 << 9;

static final String TRUE = "true";

static final String FALSE = "false";

private static final String FQCN = BlueCoveImpl.class.getName();

private static final Vector fqcnSet = new Vector();

/* The context to be used when loading native DLL */
private Object accessControlContext;

private static ShutdownHookThread shutdownHookRegistered;

private static BlueCoveImpl instance;

private static BluetoothStackHolder singleStack;

private static ThreadLocalWrapper threadStack;

private static BluetoothStackHolder threadStackIDDefault;

private static Hashtable resourceConfigProperties = new Hashtable();

private static Hashtable/* <BluetoothStack, BluetoothStackHolder> */stacks = new Hashtable();

private static Vector initializationProperties = new Vector();

static {
fqcnSet.addElement(FQCN);
for (int i = 0; i < BlueCoveConfigProperties.INITIALIZATION_PROPERTIES.length; i++) {
initializationProperties.addElement(BlueCoveConfigProperties.INITIALIZATION_PROPERTIES[i]);
}
}

/**
* Enables the use of Multiple Adapters and Bluetooth Stacks in parallel.
*/
private static class BluetoothStackHolder {

private BluetoothStack bluetoothStack;

Hashtable configProperties = new Hashtable();

private static BluetoothStack getBluetoothStack() throws BluetoothStateException {
return instance().getBluetoothStack();
}

public String toString() {
if (bluetoothStack == null) {
return "not initialized";
}
return bluetoothStack.toString();
}
}

/**
* bluetoothStack.destroy(); May stuck forever. Exit JVM anyway after timeout.
*/
private class AsynchronousShutdownThread extends Thread {

final Object monitor = new Object();

int shutdownStart = 0;

AsynchronousShutdownThread() {
super("BluecoveAsynchronousShutdownThread");
}

public void run() {
synchronized (monitor) {
while (shutdownStart == 0) {
try {
monitor.wait();
} catch (InterruptedException e) {
return;
}
}
}
if (shutdownStart == -1) {
return;
}
if (!stacks.isEmpty()) {
for (Enumeration en = stacks.elements(); en.hasMoreElements();) {
BluetoothStackHolder s = (BluetoothStackHolder) en.nextElement();
if (s.bluetoothStack != null) {
try {
s.bluetoothStack.destroy();
} finally {
s.bluetoothStack = null;
}
}
}
stacks.clear();
System.out.println("BlueCove stack shutdown completed");
}
synchronized (monitor) {
monitor.notifyAll();
}
}

void deRegister() {
shutdownStart = -1;
synchronized (monitor) {
monitor.notifyAll();
}
}
}

private class ShutdownHookThread extends Thread {

AsynchronousShutdownThread shutdownHookThread;

ShutdownHookThread(AsynchronousShutdownThread shutdownHookThread) {
super("BluecoveShutdownHookThread");
this.shutdownHookThread = shutdownHookThread;
}

public void run() {
final Object monitor = shutdownHookThread.monitor;
synchronized (monitor) {
shutdownHookThread.shutdownStart = 1;
monitor.notifyAll();
if (!stacks.isEmpty()) {
try {
monitor.wait(7000);
} catch (InterruptedException e) {
}
}
}
}

void deRegister() {
shutdownHookRegistered = null;
UtilsJavaSE.runtimeRemoveShutdownHook(this);
shutdownHookThread.deRegister();
}
}

/**
* Applications should not used this function. Allow default initialization. In Secure
* environment instance() should be called initially from secure context.
*
* @return Instance of the class, getBluetoothStack() can be called.
*/
public static synchronized BlueCoveImpl instance() {
if (instance == null) {
instance = new BlueCoveImpl();
}
return instance;
}

private BlueCoveImpl() {
try {
accessControlContext = AccessController.getContext();
} catch (Throwable javaME) {
}
// Initialization in WebStart.
DebugLog.isDebugEnabled();
copySystemProperties(null);
}

static int getNativeLibraryVersion() {
return nativeLibraryVersionExpected;
}

private synchronized void createShutdownHook() {
if (shutdownHookRegistered != null) {
return;
}
AsynchronousShutdownThread shutdownHookThread = new AsynchronousShutdownThread();
if (UtilsJavaSE.runtimeAddShutdownHook(shutdownHookRegistered = new ShutdownHookThread(shutdownHookThread))) {
UtilsJavaSE.threadSetDaemon(shutdownHookThread);
shutdownHookThread.start();
}
}

private int getStackId(String stack) {
if (STACK_WIDCOMM.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_WIDCOMM;
} else if (STACK_BLUESOLEIL.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_BLUESOLEIL;
} else if (STACK_TOSHIBA.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_TOSHIBA;
} else if (STACK_WINSOCK.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_MICROSOFT;
} else if (STACK_BLUEZ.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_BLUEZ;
} else if (STACK_BLUEZ_DBUS.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_BLUEZ_DBUS;
} else if (STACK_WINSOCK.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_OSX;
} else if (STACK_EMULATOR.equalsIgnoreCase(stack)) {
return BLUECOVE_STACK_DETECT_EMULATOR;
} else {
return 0;
}
}

private Class loadStackClass(String classPropertyName, String classNamesDefault) throws BluetoothStateException {
String classNames = getConfigProperty(classPropertyName);
if (classNames == null) {
classNames = classNamesDefault;
}
UtilsStringTokenizer tok = new UtilsStringTokenizer(classNames, "|");
while (tok.hasMoreTokens()) {
String className = tok.nextToken();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
DebugLog.error(className, e);
}
}
throw new BluetoothStateException("BlueCove " + classNames + " not available");
}

private BluetoothStack newStackInstance(Class ctackClass) throws BluetoothStateException {
String className = ctackClass.getName();
try {
return (BluetoothStack) ctackClass.newInstance();
} catch (InstantiationException e) {
DebugLog.error(className, e);
} catch (IllegalAccessException e) {
DebugLog.error(className, e);
}
throw new BluetoothStateException("BlueCove " + className + " can't instantiate");
}

private BluetoothStack loadStack(String classPropertyName, String classNameDefault) throws BluetoothStateException {
return newStackInstance(loadStackClass(classPropertyName, classNameDefault));
}

static void loadNativeLibraries(BluetoothStack stack) throws BluetoothStateException {
// Check is libraries already loaded
try {
if ((UtilsJavaSE.canCallNotLoadedNativeMethod) && (stack.isNativeCodeLoaded())) {
return;
}
} catch (Error e) {
// We caught UnsatisfiedLinkError
}
LibraryInformation[] libs = stack.requireNativeLibraries();
if ((libs == null) || (libs.length == 0)) {
// No native libs for this stack
return;
}
for (int i = 0; i < libs.length; i++) {
Class c = libs[i].stackClass;
if (c == null) {
c = stack.getClass();
}
if (!NativeLibLoader.isAvailable(libs[i].libraryName, c, libs[i].required)) {
if (libs[i].required) {
throw new BluetoothStateException("BlueCove library " + libs[i].libraryName + " not available;"
+ NativeLibLoader.getLoadErrors(libs[i].libraryName));
} else {
DebugLog.debug("library " + libs[i].libraryName + " not available");
}
}
}
}

private static boolean isNativeLibrariesAvailable(BluetoothStack stack) {
try {
if (UtilsJavaSE.canCallNotLoadedNativeMethod) {
return stack.isNativeCodeLoaded();
}
} catch (Error e) {
// We caught UnsatisfiedLinkError
}
LibraryInformation[] libs = stack.requireNativeLibraries();
if ((libs == null) || (libs.length == 0)) {
// No native libs for this stack
return true;
}
for (int i = 0; i < libs.length; i++) {
Class c = libs[i].stackClass;
if (c == null) {
c = stack.getClass();
}
if (!NativeLibLoader.isAvailable(libs[i].libraryName, c)) {
return false;
}
}
return true;
}

private BluetoothStack detectStack() throws BluetoothStateException {

BluetoothStack detectorStack = null;

String stackFirstDetector = getConfigProperty(BlueCoveConfigProperties.PROPERTY_STACK_FIRST);

String stackSelected = getConfigProperty(BlueCoveConfigProperties.PROPERTY_STACK);

if (stackFirstDetector == null) {
stackFirstDetector = stackSelected;
}
if (STACK_EMULATOR.equals(stackSelected)) {
detectorStack = loadStack(BlueCoveConfigProperties.PROPERTY_EMULATOR_CLASS, "com.intel.bluetooth.BluetoothEmulator");
} else {
switch (NativeLibLoader.getOS()) {
case NativeLibLoader.OS_LINUX:
case NativeLibLoader.OS_ANDROID_1_X:
Class stackClass = loadStackClass(BlueCoveConfigProperties.PROPERTY_BLUEZ_CLASS,
"com.intel.bluetooth.BluetoothStackBlueZ|com.intel.bluetooth.BluetoothStackBlueZDBus");
detectorStack = newStackInstance(stackClass);
loadNativeLibraries(detectorStack);
stackSelected = detectorStack.getStackID();
break;
case NativeLibLoader.OS_ANDROID_2_X:
String androidBluetoothStack = "com.intel.bluetooth.BluetoothStackAndroid";
try {
Class androidStackClass = Class.forName(androidBluetoothStack);
detectorStack = newStackInstance(androidStackClass);
stackSelected = detectorStack.getStackID();
break;
} catch (ClassNotFoundException ex) {
throw new BluetoothStateException("BlueCove " + androidBluetoothStack + " not available");
}
case NativeLibLoader.OS_MAC_OS_X:
detectorStack = new BluetoothStackOSX();
loadNativeLibraries(detectorStack);
stackSelected = detectorStack.getStackID();
break;
case NativeLibLoader.OS_WINDOWS:
case NativeLibLoader.OS_WINDOWS_CE:
detectorStack = createDetectorOnWindows(stackFirstDetector);
if (DebugLog.isDebugEnabled()) {
detectorStack.enableNativeDebug(DebugLog.class, true);
}
break;
default:
throw new BluetoothStateException("BlueCove not available");
}
}

int libraryVersion = detectorStack.getLibraryVersion();
if (nativeLibraryVersionExpected != libraryVersion) {
DebugLog.fatal("BlueCove native library version mismatch " + libraryVersion + " expected " + nativeLibraryVersionExpected);
throw new BluetoothStateException("BlueCove native library version mismatch");
}

if (stackSelected == null) {
// auto detect
int aval = detectorStack.detectBluetoothStack();
DebugLog.debug("BluetoothStack detected", aval);
int detectorID = getStackId(detectorStack.getStackID());
if ((aval & detectorID) != 0) {
stackSelected = detectorStack.getStackID();
} else if ((aval & BLUECOVE_STACK_DETECT_MICROSOFT) != 0) {
stackSelected = STACK_WINSOCK;
} else if ((aval & BLUECOVE_STACK_DETECT_WIDCOMM) != 0) {
stackSelected = STACK_WIDCOMM;
} else if ((aval & BLUECOVE_STACK_DETECT_BLUESOLEIL) != 0) {
stackSelected = STACK_BLUESOLEIL;
} else if ((aval & BLUECOVE_STACK_DETECT_TOSHIBA) != 0) {
stackSelected = STACK_TOSHIBA;
} else if ((aval & BLUECOVE_STACK_DETECT_OSX) != 0) {
stackSelected = STACK_OSX;
} else {
DebugLog.fatal("BluetoothStack not detected");
throw new BluetoothStateException("BluetoothStack not detected");
}
} else {
DebugLog.debug("BluetoothStack selected", stackSelected);
}

BluetoothStack stack = setBluetoothStack(stackSelected, detectorStack);
stackSelected = stack.getStackID();
copySystemProperties(stack);
if (!stackSelected.equals(STACK_EMULATOR)) {
System.out.println("BlueCove version " + version + " on " + stackSelected);
}
return stack;
}

/**
* List the local adapters that can be initialized using configuration property
* "bluecove.deviceID". (Linux BlueZ and Emulator)
*
* The first stack/adapter would be initialized if not initialized already, you can
* exclude it from the list.
*
* The function return empty list on non BlueZ environment.
*
* @return List of Strings
* @throws BluetoothStateException
* if stack interface can't be initialized
* @see com.intel.bluetooth.BlueCoveConfigProperties#PROPERTY_LOCAL_DEVICE_ID
* @see com.intel.bluetooth.BlueCoveLocalDeviceProperties#LOCAL_DEVICE_PROPERTY_DEVICE_ID
*/
public static Vector getLocalDevicesID() throws BluetoothStateException {
Vector v = new Vector();
String ids = BluetoothStackHolder.getBluetoothStack().getLocalDeviceProperty(BlueCoveLocalDeviceProperties.LOCAL_DEVICE_DEVICES_LIST);
if (ids != null) {
UtilsStringTokenizer tok = new UtilsStringTokenizer(ids, ",");
while (tok.hasMoreTokens()) {
v.addElement(tok.nextToken());
}
}
return v;
}

/**
* API that enables the use of Multiple Adapters and Bluetooth Stacks in parallel in
* the same JVM. Each thread should call setThreadBluetoothStackID() before using
* JSR-82 API.
*
* Affects the following JSR-82 API methods:
*
* <pre>
* LocalDevice.getLocalDevice();
* LocalDevice.getProperty(String);
* Connector.open(...);
* methods of RemoteDevice instance created by user.
* </pre>
*
* <STRONG>Example</STRONG>
* <P>
*
* <pre>
* BlueCoveImpl.useThreadLocalBluetoothStack();
* // On Windows
* BlueCoveImpl.setConfigProperty(&quot;bluecove.stack&quot;, &quot;widcomm&quot;);
* // On Linux or in Emulator
* // BlueCoveImpl.setConfigProperty(&quot;bluecove.deviceID&quot;, &quot;0&quot;);
*
* final Object id1 = BlueCoveImpl.getThreadBluetoothStackID();
* ... do some work with stack 1
*
* // Illustrates attaching thread to already initialized stack interface
* Thread t1 = new Thread() {
* public void run() {
* BlueCoveImpl.setThreadBluetoothStackID(id1);
* agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
* agent.startInquiry(...);
* .....
* }
* };
* t1.start();
*
* // Illustrates initialization of new/different stack interface in new thread
* // Start another thread that is using different stack
* Thread t2 = new Thread() {
* public void run() {
* // On Windows
* BlueCoveImpl.setConfigProperty(&quot;bluecove.stack&quot;, &quot;winsock&quot;);
* // On Linux or in Emulator
* // BlueCoveImpl.setConfigProperty(&quot;bluecove.deviceID&quot;, &quot;1&quot;);
* agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
* agent.startInquiry(...);
* .....
* }
* }
* t2.start();
*
* Thread t3 = new Thread() {
* public void run() {
* // Wrong, will produce error: Thread StackID not configured
* Connector.open(&quot;btspp://12345678:1&quot;);
* .....
* }
* };
* t3.start();
*
* </pre>
*
* @see #setConfigProperty
*/
public static synchronized void useThreadLocalBluetoothStack() {
if (threadStack == null) {
threadStack = new ThreadLocalWrapper();
}
BluetoothStackHolder s = ((BluetoothStackHolder) threadStack.get());
if (s == null) {
// Move initialized single stack to this thread
if (singleStack != null) {
s = singleStack;
singleStack = null;
} else {
s = new BluetoothStackHolder();
}
threadStack.set(s);
}
}

/**
* Initialize BluetoothStack if not already done and returns the ID to be used in
* other threads accessing the same stack.
*
* @return an object that represents Adapter/BluetoothStack, stackID to be used in
* call to <code>setThreadBluetoothStackID</code>
* @throws BluetoothStateException
* if the Bluetooth system could not be initialized
*/
public static synchronized Object getThreadBluetoothStackID() throws BluetoothStateException {
useThreadLocalBluetoothStack();
BluetoothStackHolder.getBluetoothStack();
return threadStack.get();
}

/**
* Returns the ID to be used in other threads accessing the same stack.
*
* @return an object that represents Adapter/BluetoothStack, stackID to be used in
* call to <code>setThreadBluetoothStackID</code> or
* <code>null<code> if ThreadLocalBluetoothStack not used.
*/
public static synchronized Object getCurrentThreadBluetoothStackID() {
if (threadStack == null) {
return null;
}
return threadStack.get();
}

/**
* Updates the current Thread BluetoothStack. Updating is possible only if
* <code>stackID</code> was obtained using the
* <code>getThreadBluetoothStackID()</code> method. Should be called before connection
* is made or LocalDevice received from LocalDevice.getLocalDevice().
*
* @param stackID
* stackID to use or <code>null</code> to detach the current Thread
*/
public static synchronized void setThreadBluetoothStackID(Object stackID) {
if ((stackID != null) && (!(stackID instanceof BluetoothStackHolder))) {
throw new IllegalArgumentException("stackID is not valid");
}
if (threadStack == null) {
throw new IllegalArgumentException("ThreadLocal configuration is not initialized");
}
threadStack.set(stackID);
}

/**
* Detach BluetoothStack from ThreadLocal. Used for removing itself from container
* threads. Also can be use to initialize different stack in the same thread.
*/
public static synchronized void releaseThreadBluetoothStack() {
if (threadStack == null) {
throw new IllegalArgumentException("ThreadLocal configuration is not initialized");
}
threadStack.set(null);
}

/**
* Set default Thread BluetoothStack for Threads that do not call
* <code>setThreadBluetoothStackID(stackID)</code>. Updating is possible only if
* <code>stackID</code> was obtained using the
* <code>getThreadBluetoothStackID()</code> method.
*
* @param stackID
* stackID to use or <code>null</code> to remove default
*/
public static synchronized void setDefaultThreadBluetoothStackID(Object stackID) {
if ((stackID != null) && (!(stackID instanceof BluetoothStackHolder))) {
throw new IllegalArgumentException("stackID is not valid");
}
if (threadStack == null) {
throw new IllegalArgumentException("ThreadLocal configuration is not initialized");
}
threadStackIDDefault = (BluetoothStackHolder) stackID;
}

static synchronized void setThreadBluetoothStack(BluetoothStack bluetoothStack) {
if (threadStack == null) {
return;
}
BluetoothStackHolder s = ((BluetoothStackHolder) threadStack.get());
if ((s != null) && (s.bluetoothStack == bluetoothStack)) {
return;
}

BluetoothStackHolder sh = (BluetoothStackHolder) stacks.get(bluetoothStack);
if (sh == null) {
throw new RuntimeException("ThreadLocal not found for BluetoothStack");
}
threadStack.set(sh);
}

/**
* Shutdown BluetoothStack assigned for current Thread and clear configuration
* properties for this thread
*/
public static synchronized void shutdownThreadBluetoothStack() {
// ThreadLocal configuration is not initialized
if (threadStack == null) {
return;
}
BluetoothStackHolder s = ((BluetoothStackHolder) threadStack.get());
if (s == null) {
return;
}
if (threadStackIDDefault == s) {
threadStackIDDefault = null;
}
s.configProperties.clear();
if (s.bluetoothStack != null) {
BluetoothConnectionNotifierBase.shutdownConnections(s.bluetoothStack);
RemoteDeviceHelper.shutdownConnections(s.bluetoothStack);
s.bluetoothStack.destroy();
stacks.remove(s.bluetoothStack);
s.bluetoothStack = null;
}
}

/**
* Shutdown all BluetoothStacks interfaces initialized by BlueCove
*/
public static synchronized void shutdown() {
for (Enumeration en = stacks.elements(); en.hasMoreElements();) {
BluetoothStackHolder s = (BluetoothStackHolder) en.nextElement();
s.configProperties.clear();
if (s.bluetoothStack != null) {
BluetoothConnectionNotifierBase.shutdownConnections(s.bluetoothStack);
RemoteDeviceHelper.shutdownConnections(s.bluetoothStack);
try {
s.bluetoothStack.destroy();
} finally {
s.bluetoothStack = null;
}
}
}
stacks.clear();
singleStack = null;
threadStackIDDefault = null;
if (shutdownHookRegistered != null) {
shutdownHookRegistered.deRegister();
}
clearSystemProperties();
}

/**
* API that can be used to configure BlueCove properties instead of System properties.
* Initialization properties should be changed before stack initialized. If
* <code>null</code> is passed as the <code>value</code> then the property will be
* removed.
*
* @param name
* property name
* @param value
* property value
*
* @see com.intel.bluetooth.BlueCoveConfigProperties
* @see #setConfigObject(java.lang.String, java.lang.Object)
*
* @exception IllegalArgumentException
* if the stack already initialized and property can't be changed.
*/
public static void setConfigProperty(String name, String value) {
setConfigObject(name, value);
}

/**
* API that can be used to configure BlueCove properties that aren't just strings
* Initialization properties should be changed before stack initialized. If
* <code>null</code> is passed as the <code>value</code> then the property will be
* removed.
*
* @param name
* property name
* @param value
* property value
*
* @see com.intel.bluetooth.BlueCoveConfigProperties
*
* @exception IllegalArgumentException
* if the stack already initialized and property can't be changed.
*/
public static void setConfigObject(String name, Object value) {
if (name == null) {
throw new NullPointerException("key is null");
}
BluetoothStackHolder sh = currentStackHolder(true);
if ((sh.bluetoothStack != null) && (initializationProperties.contains(name))) {
throw new IllegalArgumentException("BlueCove Stack already initialized");
}
if (value == null) {
sh.configProperties.remove(name);
} else {
sh.configProperties.put(name, value);
}
}

public static Object getConfigObject(String key) {
if (key == null) {
throw new NullPointerException("key is null");
}
Object value = null;
BluetoothStackHolder sh = currentStackHolder(false);
if (sh != null) {
value = sh.configProperties.get(key);
}
if (value == null) {
try {
value = System.getProperty(key);
} catch (SecurityException webstart) {
}
}
if (value == null) {
synchronized (resourceConfigProperties) {
Object casheValue = resourceConfigProperties.get(key);
if (casheValue != null) {
if (casheValue instanceof String) {
value = (String) casheValue;
}
} else {
value = Utils.getResourceProperty(BlueCoveImpl.class, key);
if (value == null) {
resourceConfigProperties.put(key, new Object());
} else {
resourceConfigProperties.put(key, value);
}
}
}
}
return value;
}

public static String getConfigProperty(String key) {
return (String) getConfigObject(key);
}

public static boolean getConfigProperty(String key, boolean defaultValue) {
String value = getConfigProperty(key);
if (value != null) {
return TRUE.equals(value) || "1".equals(value);
} else {
return defaultValue;
}
}

static int getConfigProperty(String key, int defaultValue) {
String value = getConfigProperty(key);
if (value != null) {
return Integer.parseInt(value);
} else {
return defaultValue;
}
}

static String[] getSystemPropertiesList() {
String[] p = { BluetoothConsts.PROPERTY_BLUETOOTH_MASTER_SWITCH, BluetoothConsts.PROPERTY_BLUETOOTH_SD_ATTR_RETRIEVABLE_MAX,
BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_DEVICES_MAX, BluetoothConsts.PROPERTY_BLUETOOTH_L2CAP_RECEIVEMTU_MAX,
BluetoothConsts.PROPERTY_BLUETOOTH_SD_TRANS_MAX, BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY_SCAN,
BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE_SCAN, BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY,
BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE };
return p;
}

static void clearSystemProperties() {
UtilsJavaSE.setSystemProperty(BluetoothConsts.PROPERTY_BLUETOOTH_API_VERSION, null);
UtilsJavaSE.setSystemProperty(BluetoothConsts.PROPERTY_OBEX_API_VERSION, null);
String[] property = getSystemPropertiesList();
for (int i = 0; i < property.length; i++) {
UtilsJavaSE.setSystemProperty(property[i], null);
}
}

void copySystemProperties(BluetoothStack bluetoothStack) {
UtilsJavaSE.setSystemProperty(BluetoothConsts.PROPERTY_BLUETOOTH_API_VERSION, BlueCoveImpl.BLUETOOTH_API_VERSION);
UtilsJavaSE.setSystemProperty(BluetoothConsts.PROPERTY_OBEX_API_VERSION, BlueCoveImpl.OBEX_API_VERSION);
if (bluetoothStack != null) {
String[] property = getSystemPropertiesList();
for (int i = 0; i < property.length; i++) {
UtilsJavaSE.setSystemProperty(property[i], bluetoothStack.getLocalDeviceProperty(property[i]));
}
}
}

public String getLocalDeviceFeature(int featureID) throws BluetoothStateException {
return ((BluetoothStackHolder.getBluetoothStack().getFeatureSet() & featureID) != 0) ? TRUE : FALSE;
}

private BluetoothStack createDetectorOnWindows(String stackFirst) throws BluetoothStateException {
if (stackFirst != null) {
DebugLog.debug("detector stack", stackFirst);
BluetoothStack detectorStack;
if (STACK_WIDCOMM.equalsIgnoreCase(stackFirst)) {
detectorStack = new BluetoothStackWIDCOMM();
if (isNativeLibrariesAvailable(detectorStack)) {
return detectorStack;
}
} else if (STACK_BLUESOLEIL.equalsIgnoreCase(stackFirst)) {
detectorStack = new BluetoothStackBlueSoleil();
if (isNativeLibrariesAvailable(detectorStack)) {
return detectorStack;
}
} else if (STACK_WINSOCK.equalsIgnoreCase(stackFirst)) {
detectorStack = new BluetoothStackMicrosoft();
if (isNativeLibrariesAvailable(detectorStack)) {
return detectorStack;
}
} else if (STACK_TOSHIBA.equalsIgnoreCase(stackFirst)) {
detectorStack = new BluetoothStackToshiba();
if (isNativeLibrariesAvailable(detectorStack)) {
return detectorStack;
}
} else {
throw new IllegalArgumentException("Invalid BlueCove detector stack [" + stackFirst + "]");
}
}
BluetoothStack stack = new BluetoothStackMicrosoft();
if (isNativeLibrariesAvailable(stack)) {
return stack;
}

stack = new BluetoothStackWIDCOMM();
if (isNativeLibrariesAvailable(stack)) {
return stack;
}

throw new BluetoothStateException("BlueCove libraries not available");
}

/**
* @deprecated use setConfigProperty("bluecove.stack", ...);
*
* @param stack
* @return stack ID
* @throws BluetoothStateException
*/
public String setBluetoothStack(String stack) throws BluetoothStateException {
return setBluetoothStack(stack, null).getStackID();
}

private synchronized BluetoothStack setBluetoothStack(String stack, BluetoothStack detectorStack) throws BluetoothStateException {
if (singleStack != null) {
if (singleStack.bluetoothStack != null) {
singleStack.bluetoothStack.destroy();
stacks.remove(singleStack.bluetoothStack);
singleStack.bluetoothStack = null;
}
} else if (threadStack != null) {
BluetoothStackHolder s = ((BluetoothStackHolder) threadStack.get());
if ((s != null) && (s.bluetoothStack != null)) {
s.bluetoothStack.destroy();
stacks.remove(s.bluetoothStack);
s.bluetoothStack = null;
}
}
BluetoothStack newStack;
if ((detectorStack != null) && (detectorStack.getStackID()).equalsIgnoreCase(stack)) {
newStack = detectorStack;
} else if (STACK_WIDCOMM.equalsIgnoreCase(stack)) {
newStack = new BluetoothStackWIDCOMM();
} else if (STACK_BLUESOLEIL.equalsIgnoreCase(stack)) {
newStack = new BluetoothStackBlueSoleil();
} else if (STACK_TOSHIBA.equalsIgnoreCase(stack)) {
newStack = new BluetoothStackToshiba();
} else {
newStack = new BluetoothStackMicrosoft();
}
loadNativeLibraries(newStack);
int libraryVersion = newStack.getLibraryVersion();
if (nativeLibraryVersionExpected != libraryVersion) {
DebugLog.fatal("BlueCove native library version mismatch " + libraryVersion + " expected " + nativeLibraryVersionExpected);
throw new BluetoothStateException("BlueCove native library version mismatch");
}

if (DebugLog.isDebugEnabled()) {
newStack.enableNativeDebug(DebugLog.class, true);
}
newStack.initialize();
createShutdownHook();

// Store stack in Thread or static variables
BluetoothStackHolder sh = currentStackHolder(true);
sh.bluetoothStack = newStack;
stacks.put(newStack, sh);
if (threadStack != null) {
threadStack.set(sh);
}
return newStack;
}

public void enableNativeDebug(boolean on) {
BluetoothStackHolder s = currentStackHolder(false);
if ((s != null) && (s.bluetoothStack != null)) {
s.bluetoothStack.enableNativeDebug(DebugLog.class, on);
}
}

private static BluetoothStackHolder currentStackHolder(boolean create) {
if (threadStack != null) {
BluetoothStackHolder s = ((BluetoothStackHolder) threadStack.get());
if ((s == null) && (threadStackIDDefault != null)) {
return threadStackIDDefault;
}
if ((s == null) && create) {
s = new BluetoothStackHolder();
threadStack.set(s);
}
return s;
} else {
if ((singleStack == null) && create) {
singleStack = new BluetoothStackHolder();
}
return singleStack;
}
}

/**
* Applications should not used this function.
*
* @return current BluetoothStack implementation
* @throws BluetoothStateException
* when BluetoothStack not detected. If one connected the hardware later,
* BlueCove would be able to recover and start correctly
* @exception Error
* if called from outside of BlueCove internal code.
*/
public synchronized BluetoothStack getBluetoothStack() throws BluetoothStateException {
Utils.isLegalAPICall(fqcnSet);
BluetoothStackHolder sh = currentStackHolder(false);
if ((sh != null) && (sh.bluetoothStack != null)) {
return sh.bluetoothStack;
} else if ((sh == null) && (threadStack != null)) {
throw new BluetoothStateException("No BluetoothStack or Adapter for current thread");
}

BluetoothStack stack;
if (accessControlContext == null) {
stack = detectStack();
} else {
stack = detectStackPrivileged();
}
return stack;
}

private BluetoothStack detectStackPrivileged() throws BluetoothStateException {
try {
return (BluetoothStack) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws BluetoothStateException {
return detectStack();
}
}, (AccessControlContext) accessControlContext);
} catch (PrivilegedActionException e) {
Throwable cause = UtilsJavaSE.getCause(e);
if (cause instanceof BluetoothStateException) {
throw (BluetoothStateException) cause;
}
throw (BluetoothStateException) UtilsJavaSE.initCause(new BluetoothStateException(e.getMessage()), cause);
}
}

}

Change log

r3049 by minashokry on Jul 25, 2010   Diff
more support of android stack. changes are
mainly in changing discovery mode
Go to: 
Project members, sign in to write a code review

Older revisions

r3046 by minashokry on Jul 24, 2010   Diff
+ Initial draft of bluecove-android2
module.
+ Adding bluecove-android2 module to
main bluecove project.
+ Defining new BluetoothStackAndroid
...
r2994 by skarzhevskyy on Jun 22, 2009   Diff
Android patches by Dennis Munsie
r2926 by skarzhevskyy on Mar 15, 2009   Diff
hide extra warning for optional
library
All revisions of this file

File info

Size: 42152 bytes, 1097 lines

File properties

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