My favorites | Sign in
Logo
                
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
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2007 Eric Wagner
* 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.
*
* @version $Id$
*/
package com.intel.bluetooth;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

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

class BluetoothStackOSX implements BluetoothStack, BluetoothStackExtension {

public static final boolean debug = false;

private static BluetoothStackOSX singleInstance = null;

// TODO what is the real number for Attributes retrivable ?
private final static int ATTR_RETRIEVABLE_MAX = 256;

private Vector deviceDiscoveryListeners = new Vector/* <DiscoveryListener> */();

private Hashtable deviceDiscoveryListenerReportedDevices = new Hashtable();

private int receive_mtu_max = -1;

private int localDeviceSupportedSoftwareVersion;

private long lastDeviceDiscoveryTime = 0;

private int localDeviceServiceClasses = 0;

private Thread localDeviceServiceClassMaintainer = null;

private static final int BLUETOOTH_SOFTWARE_VERSION_2_0_0 = 20000;

BluetoothStackOSX() {

}

// ---------------------- Library initialization

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#isNativeCodeLoaded()
*/
public native boolean isNativeCodeLoaded();

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#requireNativeLibraries()
*/
public LibraryInformation[] requireNativeLibraries() {
return LibraryInformation.library(BlueCoveImpl.NATIVE_LIB_OSX);
}

public String getStackID() {
return BlueCoveImpl.STACK_OSX;
}

public String toString() {
return getStackID();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#getFeatureSet()
*/
public int getFeatureSet() {
if (localDeviceSupportedSoftwareVersion >= BLUETOOTH_SOFTWARE_VERSION_2_0_0) {
return FEATURE_L2CAP | FEATURE_SERVICE_ATTRIBUTES | FEATURE_SET_DEVICE_SERVICE_CLASSES | (isLocalDeviceFeatureRSSI()?FEATURE_RSSI:0);
} else {
return FEATURE_L2CAP | FEATURE_SERVICE_ATTRIBUTES;
}
}

public native int getLibraryVersion();

public native int detectBluetoothStack();

private native boolean initializeImpl();

public void initialize() throws BluetoothStateException {
if (singleInstance != null) {
throw new BluetoothStateException("Only one instance of " + getStackID() + " stack supported");
}
localDeviceSupportedSoftwareVersion = getLocalDeviceSupportedSoftwareVersion();
DebugLog.debug("localDeviceSupportedSoftwareVersion", localDeviceSupportedSoftwareVersion);
if (!initializeImpl()) {
throw new BluetoothStateException("OS X BluetoothStack not found");
}
singleInstance = this;
}

public void destroy() {
if (localDeviceSupportedSoftwareVersion >= BLUETOOTH_SOFTWARE_VERSION_2_0_0) {
setLocalDeviceServiceClassesImpl(0);
}
singleInstance = null;
}

public native void enableNativeDebug(Class nativeDebugCallback, boolean on);

/*
* (non-Javadoc)
*
* @see
* com.intel.bluetooth.BluetoothStack#isCurrentThreadInterruptedCallback()
*/
public boolean isCurrentThreadInterruptedCallback() {
return UtilsJavaSE.isCurrentThreadInterrupted();
}

// ---------------------- LocalDevice

public native String getLocalDeviceBluetoothAddress() throws BluetoothStateException;

public native String getLocalDeviceName();

private native int getDeviceClassImpl();

public DeviceClass getLocalDeviceClass() {
return new DeviceClass(getDeviceClassImpl());
}

private native boolean setLocalDeviceServiceClassesImpl(int classOfDevice);

private class MaintainDeviceServiceClassesThread extends Thread {

MaintainDeviceServiceClassesThread() {
super("MaintainDeviceServiceClassesThread");
}

public void run() {
boolean updated = true;
while (true) {
try {
int delay = 1000 * 120;
if (!updated) {
delay = 1000;
}
Thread.sleep(delay);
} catch (InterruptedException e) {
break;
}
if (localDeviceServiceClasses != 0) {
updated = setLocalDeviceServiceClassesImpl(localDeviceServiceClasses);
} else if (!updated) {
updated = true;
}
}
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#setLocalDeviceServiceClasses(int)
*/
public synchronized void setLocalDeviceServiceClasses(int classOfDevice) {
if (localDeviceSupportedSoftwareVersion < BLUETOOTH_SOFTWARE_VERSION_2_0_0) {
return;
}
if (classOfDevice != localDeviceServiceClasses) {
setLocalDeviceServiceClassesImpl(classOfDevice);
}
localDeviceServiceClasses = classOfDevice;
if ((classOfDevice != 0) && (localDeviceServiceClassMaintainer == null)) {
localDeviceServiceClassMaintainer = new MaintainDeviceServiceClassesThread();
UtilsJavaSE.threadSetDaemon(localDeviceServiceClassMaintainer);
localDeviceServiceClassMaintainer.start();
}
}

public native boolean isLocalDevicePowerOn();

private native boolean isLocalDeviceFeatureSwitchRoles();

private native boolean isLocalDeviceFeatureParkMode();

private native boolean isLocalDeviceFeatureRSSI();

private native int getLocalDeviceL2CAPMTUMaximum();

private native int getLocalDeviceSupportedSoftwareVersion();

private native String getLocalDeviceSoftwareVersionInfo();

private native int getLocalDeviceManufacturer();

private native String getLocalDeviceVersion();

public String getLocalDeviceProperty(String property) {
if (BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_DEVICES_MAX.equals(property)) {
return isLocalDeviceFeatureParkMode() ? "255" : "7";
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_SD_TRANS_MAX.equals(property)) {
return "7";
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY_SCAN.equals(property)) {
return BlueCoveImpl.TRUE;
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE_SCAN.equals(property)) {
return BlueCoveImpl.TRUE;
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY.equals(property)) {
return BlueCoveImpl.TRUE;
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE.equals(property)) {
return BlueCoveImpl.TRUE;
}

if (BluetoothConsts.PROPERTY_BLUETOOTH_SD_ATTR_RETRIEVABLE_MAX.equals(property)) {
return String.valueOf(ATTR_RETRIEVABLE_MAX);
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_MASTER_SWITCH.equals(property)) {
// return isLocalDeviceFeatureSwitchRoles() ? TRUE : FALSE;
return BlueCoveImpl.FALSE;
}
if (BluetoothConsts.PROPERTY_BLUETOOTH_L2CAP_RECEIVEMTU_MAX.equals(property)) {
return String.valueOf(receiveMTUMAX());
}

if (BlueCoveLocalDeviceProperties.LOCAL_DEVICE_RADIO_VERSION.equals(property)) {
return getLocalDeviceVersion();
}
if (BlueCoveLocalDeviceProperties.LOCAL_DEVICE_RADIO_MANUFACTURER.equals(property)) {
return String.valueOf(getLocalDeviceManufacturer());
}
if (BlueCoveLocalDeviceProperties.LOCAL_DEVICE_PROPERTY_STACK_VERSION.equals(property)) {
return getLocalDeviceSoftwareVersionInfo();
}

return null;
}

private int receiveMTUMAX() {
if (receive_mtu_max < 0) {
receive_mtu_max = getLocalDeviceL2CAPMTUMaximum();
}
return receive_mtu_max;
}

private native boolean getLocalDeviceDiscoverableImpl();

public int getLocalDeviceDiscoverable() {
if (getLocalDeviceDiscoverableImpl()) {
return DiscoveryAgent.GIAC;
} else {
return DiscoveryAgent.NOT_DISCOVERABLE;
}
}

/**
* There are no functions to set OS X stack Discoverable status.
*/
public boolean setLocalDeviceDiscoverable(int mode) throws BluetoothStateException {
if (getLocalDeviceDiscoverable() == mode) {
return true;
}
return false;
}

private void verifyDeviceReady() throws BluetoothStateException {
if (!isLocalDevicePowerOn()) {
throw new BluetoothStateException("Bluetooth Device is not ready");
}
}

private native boolean retrieveDevicesImpl(int option, RetrieveDevicesCallback retrieveDevicesCallback);

public RemoteDevice[] retrieveDevices(int option) {
final Vector devices = new Vector();
RetrieveDevicesCallback retrieveDevicesCallback = new RetrieveDevicesCallback() {
public void deviceFoundCallback(long deviceAddr, int deviceClass, String deviceName, boolean paired) {
DebugLog.debug("device found", deviceAddr);
RemoteDevice remoteDevice = RemoteDeviceHelper.createRemoteDevice(BluetoothStackOSX.this, deviceAddr, deviceName, paired);
if (!devices.contains(remoteDevice)) {
devices.add(remoteDevice);
}
}
};
if (retrieveDevicesImpl(option, retrieveDevicesCallback)) {
return RemoteDeviceHelper.remoteDeviceListToArray(devices);
} else {
return null;
}
}

private native boolean isRemoteDeviceTrustedImpl(long address);

public Boolean isRemoteDeviceTrusted(long address) {
return new Boolean(isRemoteDeviceTrustedImpl(address));
}

private native boolean isRemoteDeviceAuthenticatedImpl(long address);

public Boolean isRemoteDeviceAuthenticated(long address) {
return new Boolean(isRemoteDeviceAuthenticatedImpl(address));
}

private native int readRemoteDeviceRSSIImpl(long address) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStackExtension#readRemoteDeviceRSSI(long)
*/
public int readRemoteDeviceRSSI(long address) throws IOException {
return readRemoteDeviceRSSIImpl(address);
}

// ---------------------- Remote Device authentication

private native boolean authenticateRemoteDeviceImpl(long address) throws IOException;

public boolean authenticateRemoteDevice(long address) throws IOException {
return authenticateRemoteDeviceImpl(address);
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#authenticateRemoteDevice(long,
* java.lang.String)
*/
public boolean authenticateRemoteDevice(long address, String passkey) throws IOException {
return false;
}

/*
* (non-Javadoc)
*
* @see
* com.intel.bluetooth.BluetoothStack#removeAuthenticationWithRemoteDevice
* (long)
*/
public void removeAuthenticationWithRemoteDevice(long address) throws IOException {
throw new NotSupportedIOException(getStackID());
}

// ---------------------- Device Inquiry

public native String getRemoteDeviceFriendlyName(long address) throws IOException;

private native int runDeviceInquiryImpl(DeviceInquiryRunnable inquiryRunnable, DeviceInquiryThread startedNotify, int accessCode, int duration,
DiscoveryListener listener) throws BluetoothStateException;

public boolean startInquiry(int accessCode, DiscoveryListener listener) throws BluetoothStateException {
// Inquiries are throttled if they are called too quickly in succession.
// e.g. JSR-82 TCK
long sinceDiscoveryLast = System.currentTimeMillis() - lastDeviceDiscoveryTime;
long acceptableInterval = 7 * 1000;
if (sinceDiscoveryLast < acceptableInterval) {
try {
Thread.sleep(acceptableInterval - sinceDiscoveryLast);
} catch (InterruptedException e) {
throw new BluetoothStateException();
}
}

deviceDiscoveryListeners.addElement(listener);
deviceDiscoveryListenerReportedDevices.put(listener, new Vector());
DeviceInquiryRunnable inquiryRunnable = new DeviceInquiryRunnable() {

public int runDeviceInquiry(DeviceInquiryThread startedNotify, int accessCode, DiscoveryListener listener) throws BluetoothStateException {
try {
return runDeviceInquiryImpl(this, startedNotify, accessCode, DeviceInquiryThread.getConfigDeviceInquiryDuration(), listener);
} finally {
lastDeviceDiscoveryTime = System.currentTimeMillis();
deviceDiscoveryListeners.removeElement(listener);
deviceDiscoveryListenerReportedDevices.remove(listener);
}
}

public void deviceDiscoveredCallback(DiscoveryListener listener, long deviceAddr, int deviceClass, String deviceName, boolean paired) {
if (!deviceDiscoveryListeners.contains(listener)) {
return;
}
// Update name if name retrieved
RemoteDevice remoteDevice = RemoteDeviceHelper.createRemoteDevice(BluetoothStackOSX.this, deviceAddr, deviceName, paired);
Vector reported = (Vector) deviceDiscoveryListenerReportedDevices.get(listener);
if (reported == null || (reported.contains(remoteDevice))) {
return;
}
reported.addElement(remoteDevice);
DeviceClass cod = new DeviceClass(deviceClass);
DebugLog.debug("deviceDiscoveredCallback address", remoteDevice.getBluetoothAddress());
DebugLog.debug("deviceDiscoveredCallback deviceClass", cod);
listener.deviceDiscovered(remoteDevice, cod);
}
};
return DeviceInquiryThread.startInquiry(this, inquiryRunnable, accessCode, listener);
}

private native boolean deviceInquiryCancelImpl();

public boolean cancelInquiry(DiscoveryListener listener) {
// no further deviceDiscovered() events will occur for this inquiry
if (!deviceDiscoveryListeners.removeElement(listener)) {
return false;
}
return deviceInquiryCancelImpl();
}

// ---------------------- Service search

private native int runSearchServicesImpl(long address, int transID) throws BluetoothStateException, SearchServicesException;

public int searchServices(int[] attrSet, UUID[] uuidSet, RemoteDevice device, DiscoveryListener listener) throws BluetoothStateException {

SearchServicesRunnable searchRunnable = new SearchServicesRunnable() {

public int runSearchServices(SearchServicesThread sst, int[] attrSet, UUID[] uuidSet, RemoteDevice device, DiscoveryListener listener)
throws BluetoothStateException {
// OS X will retrieve all Records, we filter here in Java
sst.searchServicesStartedCallback();
int recordsSize;
try {
recordsSize = runSearchServicesImpl(RemoteDeviceHelper.getAddress(device), sst.getTransID());
} catch (SearchServicesDeviceNotReachableException e) {
return DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE;
} catch (SearchServicesTerminatedException e) {
return DiscoveryListener.SERVICE_SEARCH_TERMINATED;
} catch (SearchServicesException e) {
return DiscoveryListener.SERVICE_SEARCH_ERROR;
}
if (sst.isTerminated()) {
return DiscoveryListener.SERVICE_SEARCH_TERMINATED;
}
if (recordsSize == 0) {
return DiscoveryListener.SERVICE_SEARCH_NO_RECORDS;
}
Vector records = new Vector();
int[] uuidFilerAttrIDs = new int[] { BluetoothConsts.ServiceClassIDList, BluetoothConsts.ProtocolDescriptorList };
int[] requiredAttrIDs = new int[] { BluetoothConsts.ServiceRecordHandle, BluetoothConsts.ServiceRecordState, BluetoothConsts.ServiceID };
nextRecord: for (int i = 0; i < recordsSize; i++) {
ServiceRecordImpl sr = new ServiceRecordImpl(BluetoothStackOSX.this, device, i);
try {
sr.populateRecord(uuidFilerAttrIDs);
// Apply JSR-82 filter, all UUID should be present
for (int u = 0; u < uuidSet.length; u++) {
if (!((sr.hasServiceClassUUID(uuidSet[u]) || sr.hasProtocolClassUUID(uuidSet[u])))) {
if (debug) {
DebugLog.debug("filtered ServiceRecord (" + i + ")", sr);
}
continue nextRecord;
}
}
if (debug) {
DebugLog.debug("accepted ServiceRecord (" + i + ")", sr);
}
records.addElement(sr);
sr.populateRecord(requiredAttrIDs);
if (attrSet != null) {
sr.populateRecord(attrSet);
}
DebugLog.debug("ServiceRecord (" + i + ")", sr);
} catch (Exception e) {
DebugLog.debug("populateRecord error", e);
}

if (sst.isTerminated()) {
DebugLog.debug("SERVICE_SEARCH_TERMINATED " + sst.getTransID());
return DiscoveryListener.SERVICE_SEARCH_TERMINATED;
}
}
if (records.size() != 0) {
DebugLog.debug("SERVICE_SEARCH_COMPLETED " + sst.getTransID());
ServiceRecord[] fileteredRecords = (ServiceRecord[]) Utils.vector2toArray(records, new ServiceRecord[records.size()]);
listener.servicesDiscovered(sst.getTransID(), fileteredRecords);
return DiscoveryListener.SERVICE_SEARCH_COMPLETED;
} else {
return DiscoveryListener.SERVICE_SEARCH_NO_RECORDS;
}
}

};
return SearchServicesThread.startSearchServices(this, searchRunnable, attrSet, uuidSet, device, listener);
}

private native void cancelServiceSearchImpl(int transID);

public boolean cancelServiceSearch(int transID) {
SearchServicesThread sst = SearchServicesThread.getServiceSearchThread(transID);
if (sst != null) {
synchronized (this) {
if (!sst.isTerminated()) {
sst.setTerminated();
cancelServiceSearchImpl(transID);
return true;
}
}
}
return false;
}

private native byte[] getServiceAttributeImpl(long address, long serviceRecordIndex, int attrID);

public boolean populateServicesRecordAttributeValues(ServiceRecordImpl serviceRecord, int[] attrIDs) throws IOException {
if (attrIDs.length > ATTR_RETRIEVABLE_MAX) {
throw new IllegalArgumentException();
}
boolean anyRetrived = false;
long address = RemoteDeviceHelper.getAddress(serviceRecord.getHostDevice());
for (int i = 0; i < attrIDs.length; i++) {
int id = attrIDs[i];
try {
byte[] blob = getServiceAttributeImpl(address, serviceRecord.getHandle(), id);
if (blob != null) {
DataElement element = (new SDPInputStream(new ByteArrayInputStream(blob))).readElement();
serviceRecord.populateAttributeValue(id, element);
anyRetrived = true;
if (debug) {
DebugLog.debug("data for attribute " + id + " Ox" + Integer.toHexString(id) + " " + element);
}
} else {
if (debug) {
DebugLog.debug("no data for attribute " + id + " Ox" + Integer.toHexString(id));
}
}
} catch (Throwable e) {
if (debug) {
DebugLog.error("error populate attribute " + id + " Ox" + Integer.toHexString(id), e);
}
}
}
return anyRetrived;
}

// ---------------------- Client RFCOMM connections

private native long connectionRfOpenClientConnectionImpl(long address, int channel, boolean authenticate, boolean encrypt, int timeout) throws IOException;

public long connectionRfOpenClientConnection(BluetoothConnectionParams params) throws IOException {
if (params.encrypt) {
throw new BluetoothConnectionException(BluetoothConnectionException.SECURITY_BLOCK, "encrypt mode not supported");
}
Object lock = RemoteDeviceHelper.createRemoteDevice(this, params.address, null, false);
synchronized (lock) {
return connectionRfOpenClientConnectionImpl(params.address, params.channel, params.authenticate, params.encrypt, params.timeout);
}
}

public native void connectionRfCloseClientConnection(long handle) throws IOException;

public native int rfGetSecurityOpt(long handle, int expected) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2Encrypt(long,long,boolean)
*/
public boolean rfEncrypt(long address, long handle, boolean on) throws IOException {
return false;
}

// ---------------------- Server RFCOMM connections

private native long rfServerCreateImpl(byte[] uuidValue, boolean obexSrv, String name, boolean authenticate, boolean encrypt) throws IOException;

private native int rfServerGetChannelID(long handle) throws IOException;

private native void rfServerCloseImpl(long handle) throws IOException;

public long rfServerOpen(BluetoothConnectionNotifierParams params, ServiceRecordImpl serviceRecord) throws IOException {
verifyDeviceReady();
if (params.encrypt) {
throw new BluetoothConnectionException(BluetoothConnectionException.SECURITY_BLOCK, "encrypt mode not supported");
}
byte[] uuidValue = Utils.UUIDToByteArray(params.uuid);
long handle = rfServerCreateImpl(uuidValue, params.obex, params.name, params.authenticate, params.encrypt);
boolean success = false;
try {
int channel = rfServerGetChannelID(handle);
serviceRecord.populateRFCOMMAttributes(handle, channel, params.uuid, params.name, params.obex);
success = true;
} finally {
if (!success) {
rfServerCloseImpl(handle);
}
}
return handle;
}

public void rfServerClose(long handle, ServiceRecordImpl serviceRecord) throws IOException {
rfServerCloseImpl(handle);
}

public void rfServerUpdateServiceRecord(long handle, ServiceRecordImpl serviceRecord, boolean acceptAndOpen) throws ServiceRegistrationException {
sdpServiceUpdateServiceRecord(handle, 'R', serviceRecord);
}

public native long rfServerAcceptAndOpenRfServerConnection(long handle) throws IOException;

public void connectionRfCloseServerConnection(long handle) throws IOException {
connectionRfCloseClientConnection(handle);
}

private native void sdpServiceUpdateServiceRecordPublish(long handle, char handleType) throws ServiceRegistrationException;

private native void sdpServiceAddAttribute(long handle, char handleType, int attrID, int attrType, long numberValue, byte[] arrayValue)
throws ServiceRegistrationException;

private native void sdpServiceSequenceAttributeStart(long handle, char handleType, int attrID, int attrType) throws ServiceRegistrationException;

private native void sdpServiceSequenceAttributeEnd(long handle, char handleType, int attrID) throws ServiceRegistrationException;

private void sdpServiceAddAttribute(long handle, char handleType, int attrID, DataElement element) throws ServiceRegistrationException {
int type = element.getDataType();
switch (type) {
case DataElement.NULL:
sdpServiceAddAttribute(handle, handleType, attrID, type, 0, null);
break;
case DataElement.BOOL:
sdpServiceAddAttribute(handle, handleType, attrID, type, element.getBoolean() ? 1 : 0, null);
break;
case DataElement.U_INT_1:
case DataElement.INT_1:
case DataElement.U_INT_2:
case DataElement.INT_2:
case DataElement.U_INT_4:
case DataElement.INT_4:
case DataElement.INT_8:
sdpServiceAddAttribute(handle, handleType, attrID, type, element.getLong(), null);
break;
case DataElement.U_INT_8:
case DataElement.U_INT_16:
case DataElement.INT_16:
sdpServiceAddAttribute(handle, handleType, attrID, type, 0, (byte[]) element.getValue());
break;
case DataElement.UUID:
sdpServiceAddAttribute(handle, handleType, attrID, type, 0, Utils.UUIDToByteArray((UUID) element.getValue()));
break;
case DataElement.STRING:
byte[] bs = Utils.getUTF8Bytes((String) element.getValue());
sdpServiceAddAttribute(handle, handleType, attrID, type, 0, bs);
break;
case DataElement.URL:
byte[] bu = Utils.getASCIIBytes((String) element.getValue());
sdpServiceAddAttribute(handle, handleType, attrID, type, 0, bu);
break;
case DataElement.DATSEQ:
case DataElement.DATALT:
sdpServiceSequenceAttributeStart(handle, handleType, attrID, type);
for (Enumeration e = (Enumeration) element.getValue(); e.hasMoreElements();) {
DataElement child = (DataElement) e.nextElement();
sdpServiceAddAttribute(handle, handleType, -1, child);
}
sdpServiceSequenceAttributeEnd(handle, handleType, attrID);
break;
default:
throw new IllegalArgumentException();
}
}

private void sdpServiceUpdateServiceRecord(long handle, char handleType, ServiceRecordImpl serviceRecord) throws ServiceRegistrationException {
int[] ids = serviceRecord.getAttributeIDs();
if ((ids == null) || (ids.length == 0)) {
return;
}
for (int i = 0; i < ids.length; i++) {
int attrID = ids[i];
switch (attrID) {
case BluetoothConsts.ServiceRecordHandle:
continue;
case BluetoothConsts.ProtocolDescriptorList:
case BluetoothConsts.AttributeIDServiceName:
continue;
}
sdpServiceAddAttribute(handle, handleType, attrID, serviceRecord.getAttributeValue(attrID));
}
sdpServiceUpdateServiceRecordPublish(handle, handleType);
}

// ---------------------- Shared Client and Server RFCOMM connections

public void connectionRfFlush(long handle) throws IOException {
// TODO Auto-generated method stub
}

public native int connectionRfRead(long handle) throws IOException;

public native int connectionRfRead(long handle, byte[] b, int off, int len) throws IOException;

public native int connectionRfReadAvailable(long handle) throws IOException;

public void connectionRfWrite(long handle, int b) throws IOException {
byte buf[] = new byte[1];
buf[0] = (byte) (b & 0xFF);
connectionRfWrite(handle, buf, 0, 1);
}

public native void connectionRfWrite(long handle, byte[] b, int off, int len) throws IOException;

public native long getConnectionRfRemoteAddress(long handle) throws IOException;

// ---------------------- Client and Server L2CAP connections

private void validateMTU(int receiveMTU, int transmitMTU) {
if (receiveMTU > receiveMTUMAX()) {
throw new IllegalArgumentException("invalid ReceiveMTU value " + receiveMTU);
}
}

private native long l2OpenClientConnectionImpl(long address, int channel, boolean authenticate, boolean encrypt, int receiveMTU, int transmitMTU,
int timeout) throws IOException;

/*
* (non-Javadoc)
*
* @see
* com.intel.bluetooth.BluetoothStack#l2OpenClientConnection(com.intel.bluetooth
* .BluetoothConnectionParams, int, int)
*/
public long l2OpenClientConnection(BluetoothConnectionParams params, int receiveMTU, int transmitMTU) throws IOException {
validateMTU(receiveMTU, transmitMTU);
if (params.encrypt) {
throw new BluetoothConnectionException(BluetoothConnectionException.SECURITY_BLOCK, "encrypt mode not supported");
}
Object lock = RemoteDeviceHelper.createRemoteDevice(this, params.address, null, false);
synchronized (lock) {
return l2OpenClientConnectionImpl(params.address, params.channel, params.authenticate, params.encrypt, receiveMTU, transmitMTU, params.timeout);
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2CloseClientConnection(long)
*/
public native void l2CloseClientConnection(long handle) throws IOException;

private native long l2ServerOpenImpl(byte[] uuidValue, boolean authenticate, boolean encrypt, String name, int receiveMTU, int transmitMTU, int assignPsm)
throws IOException;

public native int l2ServerPSM(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @seecom.intel.bluetooth.BluetoothStack#l2ServerOpen(com.intel.bluetooth.
* BluetoothConnectionNotifierParams, int, int,
* com.intel.bluetooth.ServiceRecordImpl)
*/
public long l2ServerOpen(BluetoothConnectionNotifierParams params, int receiveMTU, int transmitMTU, ServiceRecordImpl serviceRecord) throws IOException {
verifyDeviceReady();
validateMTU(receiveMTU, transmitMTU);
if (params.encrypt) {
throw new BluetoothConnectionException(BluetoothConnectionException.SECURITY_BLOCK, "encrypt mode not supported");
}
byte[] uuidValue = Utils.UUIDToByteArray(params.uuid);
long handle = l2ServerOpenImpl(uuidValue, params.authenticate, params.encrypt, params.name, receiveMTU, transmitMTU, params.bluecove_ext_psm);

int channel = l2ServerPSM(handle);

int serviceRecordHandle = (int) handle;

serviceRecord.populateL2CAPAttributes(serviceRecordHandle, channel, params.uuid, params.name);

return handle;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2ServerUpdateServiceRecord(long,
* com.intel.bluetooth.ServiceRecordImpl, boolean)
*/
public void l2ServerUpdateServiceRecord(long handle, ServiceRecordImpl serviceRecord, boolean acceptAndOpen) throws ServiceRegistrationException {
sdpServiceUpdateServiceRecord(handle, 'L', serviceRecord);
}

/*
* (non-Javadoc)
*
* @see
* com.intel.bluetooth.BluetoothStack#l2ServerAcceptAndOpenServerConnection
* (long)
*/
public native long l2ServerAcceptAndOpenServerConnection(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2CloseServerConnection(long)
*/
public void l2CloseServerConnection(long handle) throws IOException {
l2CloseClientConnection(handle);
}

private native void l2ServerCloseImpl(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2ServerClose(long,
* com.intel.bluetooth.ServiceRecordImpl)
*/
public void l2ServerClose(long handle, ServiceRecordImpl serviceRecord) throws IOException {
l2ServerCloseImpl(handle);
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2GetSecurityOpt(long, int)
*/
public native int l2GetSecurityOpt(long handle, int expected) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2Ready(long)
*/
public native boolean l2Ready(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2receive(long, byte[])
*/
public native int l2Receive(long handle, byte[] inBuf) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2send(long, byte[])
*/
public native void l2Send(long handle, byte[] data, int transmitMTU) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2GetReceiveMTU(long)
*/
public native int l2GetReceiveMTU(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2GetTransmitMTU(long)
*/
public native int l2GetTransmitMTU(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2RemoteAddress(long)
*/
public native long l2RemoteAddress(long handle) throws IOException;

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2Encrypt(long,long,boolean)
*/
public boolean l2Encrypt(long address, long handle, boolean on) throws IOException {
return false;
}
}
Show details Hide details

Change log

r2945 by skarzhevskyy on Mar 21, 2009   Diff
honor requested TransmitMTU
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
r2737 by skarzhevskyy on Feb 12, 2009   Diff
add isLocalDeviceFeatureRSSI
r2736 by skarzhevskyy on Feb 12, 2009   Diff
Adding property bluecove.feature.rssi
All revisions of this file

File info

Size: 35518 bytes, 896 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Hosted by Google Code