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
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2004 Intel Corporation
*
* 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.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;

import com.intel.bluetooth.BluetoothConsts.DeviceClassConsts;

class ServiceRecordImpl implements ServiceRecord {

private BluetoothStack bluetoothStack;

private RemoteDevice device;

private long handle;

Hashtable attributes;

protected boolean attributeUpdated;

int deviceServiceClasses;

int deviceServiceClassesRegistered;

ServiceRecordImpl(BluetoothStack bluetoothStack, RemoteDevice device, long handle) {

this.bluetoothStack = bluetoothStack;

this.device = device;

this.handle = handle;

this.deviceServiceClassesRegistered = 0;

this.attributes = new Hashtable();
}

byte[] toByteArray() throws IOException {
DataElement rootSeq = new DataElement(DataElement.DATSEQ);
final boolean sort = true;
if (sort) {
int[] sortIDs = new int[attributes.size()];
int k = 0;
for (Enumeration e = attributes.keys(); e.hasMoreElements();) {
Integer key = (Integer) e.nextElement();
sortIDs[k] = key.intValue();
k++;
}
// DebugLog.debug("b4 sort", sortIDs);
// Sort
for (int i = 0; i < sortIDs.length; i++) {
for (int j = 0; j < sortIDs.length - i - 1; j++) {
if (sortIDs[j] > sortIDs[j + 1]) {
int temp = sortIDs[j];
sortIDs[j] = sortIDs[j + 1];
sortIDs[j + 1] = temp;
}
}
}
// DebugLog.debug("sorted", sortIDs);
for (int i = 0; i < sortIDs.length; i++) {
int attrID = sortIDs[i];
rootSeq.addElement(new DataElement(DataElement.U_INT_2, attrID));
rootSeq.addElement(getAttributeValue(attrID));
}
} else {
for (Enumeration e = attributes.keys(); e.hasMoreElements();) {
Integer key = (Integer) e.nextElement();
rootSeq.addElement(new DataElement(DataElement.U_INT_2, key.intValue()));
rootSeq.addElement((DataElement) attributes.get(key));
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
SDPOutputStream sdpOut = new SDPOutputStream(out);
sdpOut.writeElement(rootSeq);
return out.toByteArray();
}

void loadByteArray(byte data[]) throws IOException {
DataElement element = (new SDPInputStream(new ByteArrayInputStream(data))).readElement();
if (element.getDataType() != DataElement.DATSEQ) {
throw new IOException("DATSEQ expected instead of " + element.getDataType());
}
Enumeration en = (Enumeration) element.getValue();
while (en.hasMoreElements()) {
DataElement id = (DataElement) en.nextElement();
if (id.getDataType() != DataElement.U_INT_2) {
throw new IOException("U_INT_2 expected instead of " + id.getDataType());
}
DataElement value = (DataElement) en.nextElement();
this.populateAttributeValue((int) id.getLong(), value);
}
}

/*
* Returns the value of the service attribute ID provided it is present in
* the service record, otherwise this method returns null. Parameters:
* attrID - the attribute whose value is to be returned Returns: the value
* of the attribute ID if present in the service record, otherwise null
* Throws: IllegalArgumentException - if attrID is negative or greater than
* or equal to 2^16
*/

public DataElement getAttributeValue(int attrID) {
if (attrID < 0x0000 || attrID > 0xffff) {
throw new IllegalArgumentException();
}

return (DataElement) attributes.get(new Integer(attrID));
}

/*
* Returns the remote Bluetooth device that populated the service record
* with attribute values. It is important to note that the Bluetooth device
* that provided the value might not be reachable anymore, since it can
* move, turn off, or change its security mode denying all further
* transactions. Returns: the remote Bluetooth device that populated the
* service record, or null if the local device populated this ServiceRecord
*/

public RemoteDevice getHostDevice() {
return device;
}

/*
* Returns the service attribute IDs whose value could be retrieved by a
* call to getAttributeValue(). The list of attributes being returned is not
* sorted and includes default attributes. Returns: an array of service
* attribute IDs that are in this object and have values for them; if there
* are no attribute IDs that have values, this method will return an array
* of length zero. See Also: getAttributeValue(int)
*/

public int[] getAttributeIDs() {
int[] attrIDs = new int[attributes.size()];

int i = 0;

for (Enumeration e = attributes.keys(); e.hasMoreElements();) {
attrIDs[i++] = ((Integer) e.nextElement()).intValue();
}

return attrIDs;
}

/*
* Retrieves the values by contacting the remote Bluetooth device for a set
* of service attribute IDs of a service that is available on a Bluetooth
* device. (This involves going over the air and contacting the remote
* device for the attribute values.) The system might impose a limit on the
* number of service attribute ID values one can request at a time.
* Applications can obtain the value of this limit as a String by calling
* LocalDevice.getProperty("bluetooth.sd.attr.retrievable.max"). The method
* is blocking and will return when the results of the request are
* available. Attribute IDs whose values could be obtained are added to this
* service record. If there exist attribute IDs for which values are
* retrieved this will cause the old values to be overwritten. If the remote
* device cannot be reached, an IOException will be thrown. Parameters:
* attrIDs - the list of service attributes IDs whose value are to be
* retrieved; the number of attributes cannot exceed the property
* bluetooth.sd.attr.retrievable.max; the attributes in the request must be
* legal, i.e. their values are in the range of [0, 2^16-1]. The input
* attribute IDs can include attribute IDs from the default attribute set
* too. Returns: true if the request was successful in retrieving values for
* some or all of the attribute IDs; false if it was unsuccessful in
* retrieving any values Throws: java.io.IOException - if the local device
* is unable to connect to the remote Bluetooth device that was the source
* of this ServiceRecord; if this ServiceRecord was deleted from the SDDB of
* the remote device IllegalArgumentException - if the size of attrIDs
* exceeds the system specified limit as defined by
* bluetooth.sd.attr.retrievable.max; if the attrIDs array length is zero;
* if any of their values are not in the range of [0, 2^16-1]; if attrIDs
* has duplicate values NullPointerException - if attrIDs is null
* RuntimeException - if this ServiceRecord describes a service on the local
* device rather than a service on a remote device
*/

public boolean populateRecord(int[] attrIDs) throws IOException {
/*
* check this is not a local service record
*/
if (device == null) {
throw new RuntimeException("This is local device service record");
}

if (attrIDs == null) {
throw new NullPointerException("attrIDs is null");
}
/*
* check attrIDs is non-null and has length > 0
*/
if (attrIDs.length == 0) {
throw new IllegalArgumentException();
}

/*
* check attrIDs are in range
*/

for (int i = 0; i < attrIDs.length; i++) {
if (attrIDs[i] < 0x0000 || attrIDs[i] > 0xffff) {
throw new IllegalArgumentException();
}
}

/*
* copy and sort attrIDs (required by MS Bluetooth and for check for
* duplicates)
*/

int[] sortIDs = new int[attrIDs.length];
System.arraycopy(attrIDs, 0, sortIDs, 0, attrIDs.length);
for (int i = 0; i < sortIDs.length; i++) {
for (int j = 0; j < sortIDs.length - i - 1; j++) {
if (sortIDs[j] > sortIDs[j + 1]) {
int temp = sortIDs[j];
sortIDs[j] = sortIDs[j + 1];
sortIDs[j + 1] = temp;
}
}
}
/*
* check for duplicates
*/
for (int i = 0; i < sortIDs.length - 1; i++) {
if (sortIDs[i] == sortIDs[i + 1]) {
throw new IllegalArgumentException();
}
DebugLog.debug0x("srvRec query for attr", sortIDs[i]);
}
DebugLog.debug0x("srvRec query for attr", sortIDs[sortIDs.length - 1]);

return this.bluetoothStack.populateServicesRecordAttributeValues(this, sortIDs);
}

/*
* Returns a String including optional parameters that can be used by a
* client to connect to the service described by this ServiceRecord. The
* return value can be used as the first argument to Connector.open(). In
* the case of a Serial Port service record, this string might look like
* "btspp://0050CD00321B:3;authenticate=true;encrypt=false;master=true",
* where "0050CD00321B" is the Bluetooth address of the device that provided
* this ServiceRecord, "3" is the RFCOMM server channel mentioned in this
* ServiceRecord, and there are three optional parameters related to
* security and master/slave roles. If this method is called on a
* ServiceRecord returned from LocalDevice.getRecord(), it will return the
* connection string that a remote device will use to connect to this
* service.
*
* Parameters: requiredSecurity - determines whether authentication or
* encryption are required for a connection mustBeMaster - true indicates
* that this device must play the role of master in connections to this
* service; false indicates that the local device is willing to be either
* the master or the slave Returns: a string that can be used to connect to
* the service or null if the ProtocolDescriptorList in this ServiceRecord
* is not formatted according to the Bluetooth specification Throws:
* IllegalArgumentException - if requiredSecurity is not one of the
* constants NOAUTHENTICATE_NOENCRYPT, AUTHENTICATE_NOENCRYPT, or
* AUTHENTICATE_ENCRYPT See Also: NOAUTHENTICATE_NOENCRYPT,
* AUTHENTICATE_NOENCRYPT, AUTHENTICATE_ENCRYPT
*/

public String getConnectionURL(int requiredSecurity, boolean mustBeMaster) {

int commChannel = -1;

DataElement protocolDescriptor = getAttributeValue(BluetoothConsts.ProtocolDescriptorList);
if ((protocolDescriptor == null) || (protocolDescriptor.getDataType() != DataElement.DATSEQ)) {
return null;
}

/*
* get RFCOMM Channel ProtocolDescriptorList is DATSEQ of DATSEQ of UUID
* and optional parameters
*/

boolean isL2CAP = false;
boolean isRFCOMM = false;
boolean isOBEX = false;

for (Enumeration protocolsSeqEnum = (Enumeration) protocolDescriptor.getValue(); protocolsSeqEnum
.hasMoreElements();) {
DataElement elementSeq = (DataElement) protocolsSeqEnum.nextElement();

if (elementSeq.getDataType() == DataElement.DATSEQ) {
Enumeration elementSeqEnum = (Enumeration) elementSeq.getValue();

if (elementSeqEnum.hasMoreElements()) {
DataElement protocolElement = (DataElement) elementSeqEnum.nextElement();
if (protocolElement.getDataType() != DataElement.UUID) {
continue;
}
Object uuid = protocolElement.getValue();
if (BluetoothConsts.OBEX_PROTOCOL_UUID.equals(uuid)) {
isOBEX = true;
isRFCOMM = false;
isL2CAP = false;
} else if (elementSeqEnum.hasMoreElements() && (BluetoothConsts.RFCOMM_PROTOCOL_UUID.equals(uuid))) {

DataElement protocolPSMElement = (DataElement) elementSeqEnum.nextElement();

switch (protocolPSMElement.getDataType()) {
case DataElement.U_INT_1:
case DataElement.U_INT_2:
case DataElement.U_INT_4:
case DataElement.INT_1:
case DataElement.INT_2:
case DataElement.INT_4:
case DataElement.INT_8:
long val = protocolPSMElement.getLong();
if ((val >= BluetoothConsts.RFCOMM_CHANNEL_MIN)
&& (val <= BluetoothConsts.RFCOMM_CHANNEL_MAX)) {
commChannel = (int) val;
isRFCOMM = true;
isL2CAP = false;
}
break;
}
} else if (elementSeqEnum.hasMoreElements() && (BluetoothConsts.L2CAP_PROTOCOL_UUID.equals(uuid))) {
DataElement protocolPSMElement = (DataElement) elementSeqEnum.nextElement();
switch (protocolPSMElement.getDataType()) {
case DataElement.U_INT_1:
case DataElement.U_INT_2:
case DataElement.U_INT_4:
case DataElement.INT_1:
case DataElement.INT_2:
case DataElement.INT_4:
case DataElement.INT_8:
long pcm = protocolPSMElement.getLong();
if ((pcm >= BluetoothConsts.L2CAP_PSM_MIN) && (pcm <= BluetoothConsts.L2CAP_PSM_MAX)) {
commChannel = (int) pcm;
isL2CAP = true;
}
break;
}
}
}
}
}

if (commChannel == -1) {
return null;
}

/*
* build URL
*/
StringBuffer buf = new StringBuffer();
if (isOBEX) {
buf.append(BluetoothConsts.PROTOCOL_SCHEME_BT_OBEX);
} else if (isRFCOMM) {
buf.append(BluetoothConsts.PROTOCOL_SCHEME_RFCOMM);
} else if (isL2CAP) {
buf.append(BluetoothConsts.PROTOCOL_SCHEME_L2CAP);
} else {
return null;
}
buf.append("://");

if (device == null) {
try {
Object saveID = BlueCoveImpl.getCurrentThreadBluetoothStackID();
try {
BlueCoveImpl.setThreadBluetoothStack(bluetoothStack);
buf.append(LocalDevice.getLocalDevice().getBluetoothAddress());
} finally {
if (saveID != null) {
BlueCoveImpl.setThreadBluetoothStackID(saveID);
}
}
} catch (BluetoothStateException bse) {
DebugLog.error("can't read LocalAddress", bse);
buf.append("localhost");
}
} else {
buf.append(getHostDevice().getBluetoothAddress());
}

buf.append(":");
if (isL2CAP) {
String hex = Integer.toHexString(commChannel);
for (int i = hex.length(); i < 4; i++) {
buf.append('0');
}
buf.append(hex);
} else {
buf.append(commChannel);
}

switch (requiredSecurity) {
case NOAUTHENTICATE_NOENCRYPT:
buf.append(";authenticate=false;encrypt=false");
break;
case AUTHENTICATE_NOENCRYPT:
buf.append(";authenticate=true;encrypt=false");
break;
case AUTHENTICATE_ENCRYPT:
buf.append(";authenticate=true;encrypt=true");
break;
default:
throw new IllegalArgumentException();
}

if (mustBeMaster) {
buf.append(";master=true");
} else {
buf.append(";master=false");
}

return buf.toString();
}

int getChannel(UUID protocolUUID) {

int channel = -1;

DataElement protocolDescriptor = getAttributeValue(BluetoothConsts.ProtocolDescriptorList);
if ((protocolDescriptor == null) || (protocolDescriptor.getDataType() != DataElement.DATSEQ)) {
return -1;
}

/*
* get RFCOMM Channel or L2CAP PSM ProtocolDescriptorList is DATSEQ of
* DATSEQ of UUID and optional parameters
*/

for (Enumeration protocolsSeqEnum = (Enumeration) protocolDescriptor.getValue(); protocolsSeqEnum
.hasMoreElements();) {
DataElement elementSeq = (DataElement) protocolsSeqEnum.nextElement();

if (elementSeq.getDataType() == DataElement.DATSEQ) {
Enumeration elementSeqEnum = (Enumeration) elementSeq.getValue();

if (elementSeqEnum.hasMoreElements()) {
DataElement protocolElement = (DataElement) elementSeqEnum.nextElement();
if (protocolElement.getDataType() != DataElement.UUID) {
continue;
}
Object uuid = protocolElement.getValue();
if (elementSeqEnum.hasMoreElements() && (protocolUUID.equals(uuid))) {

DataElement protocolPSMElement = (DataElement) elementSeqEnum.nextElement();

switch (protocolPSMElement.getDataType()) {
case DataElement.U_INT_1:
case DataElement.U_INT_2:
case DataElement.U_INT_4:
case DataElement.INT_1:
case DataElement.INT_2:
case DataElement.INT_4:
case DataElement.INT_8:
channel = (int) protocolPSMElement.getLong();
break;
}
}
}
}
}
return channel;
}

/*
* Used by a server application to indicate the major service class bits
* that should be activated in the server's DeviceClass when this
* ServiceRecord is added to the SDDB. When client devices do device
* discovery, the server's DeviceClass is provided as one of the arguments
* of the deviceDiscovered method of the DiscoveryListener interface. Client
* devices can consult the DeviceClass of the server device to get a general
* idea of the kind of device this is (e.g., phone, PDA, or PC) and the
* major service classes it offers (e.g., rendering, telephony, or
* information). A server application should use the setDeviceServiceClasses
* method to describe its service in terms of the major service classes.
* This allows clients to obtain a DeviceClass for the server that
* accurately describes all of the services being offered. When
* acceptAndOpen() is invoked for the first time on the notifier associated
* with this ServiceRecord, the classes argument from the
* setDeviceServiceClasses method is OR'ed with the current setting of the
* major service class bits of the local device. The OR operation
* potentially activates additional bits. These bits may be retrieved by
* calling getDeviceClass() on the LocalDevice object. Likewise, a call to
* LocalDevice.updateRecord() will cause the major service class bits to be
* OR'ed with the current settings and updated.
*
* The documentation for DeviceClass gives examples of the integers that
* describe each of the major service classes and provides a URL for the
* complete list. These integers can be used individually or OR'ed together
* to describe the appropriate value for classes.
*
* Later, when this ServiceRecord is removed from the SDDB, the
* implementation will automatically deactivate the device bits that were
* activated as a result of the call to setDeviceServiceClasses. The only
* exception to this occurs if there is another ServiceRecord that is in the
* SDDB and setDeviceServiceClasses has been sent to that other
* ServiceRecord to request that some of the same bits be activated.
*
* Parameters: classes - an integer whose binary representation indicates
* the major service class bits that should be activated Throws:
* IllegalArgumentException - if classes is not an OR of one or more of the
* major service class integers in the Bluetooth Assigned Numbers document.
* While Limited Discoverable Mode is included in this list of major service
* classes, its bit is activated by placing the device in Limited
* Discoverable Mode (see the GAP specification), so if bit 13 is set this
* exception will be thrown. RuntimeExceptin - if the ServiceRecord
* receiving the message was obtained from a remote device
*/

public void setDeviceServiceClasses(int classes) {
if (device != null) {
throw new RuntimeException("Service record obtained from a remote device");
}
if ((classes & (0xff000000 | DeviceClassConsts.LIMITED_DISCOVERY_SERVICE | DeviceClassConsts.FORMAT_VERSION_MASK)) != 0) {
throw new IllegalArgumentException();
}
if ((classes & (DeviceClassConsts.MAJOR_MASK | DeviceClassConsts.MINOR_MASK)) != 0) {
throw new IllegalArgumentException();
}

if ((bluetoothStack.getFeatureSet() & BluetoothStack.FEATURE_SET_DEVICE_SERVICE_CLASSES) == 0) {
throw new NotSupportedRuntimeException(bluetoothStack.getStackID());
}

this.deviceServiceClasses = classes;
}

/*
* Modifies this ServiceRecord to contain the service attribute defined by
* the attribute-value pair (attrID, attrValue). If the attrID does not
* exist in the ServiceRecord, this attribute-value pair is added to this
* ServiceRecord object. If the attrID is already in this ServiceRecord, the
* value of the attribute is changed to attrValue. If attrValue is null, the
* attribute with the attribute ID of attrID is removed from this
* ServiceRecord object. If attrValue is null and attrID does not exist in
* this object, this method will return false. This method makes no
* modifications to a service record in the SDDB. In order for any changes
* made by this method to be reflected in the SDDB, a call must be made to
* the acceptAndOpen() method of the associated notifier to add this
* ServiceRecord to the SDDB for the first time, or a call must be made to
* the updateRecord() method of LocalDevice to modify the version of this
* ServiceRecord that is already in the SDDB.
*
* This method prevents the ServiceRecordHandle from being modified by
* throwing an IllegalArgumentException.
*
* Parameters: attrID - the service attribute ID attrValue - the DataElement
* which is the value of the service attribute Returns: true if the service
* attribute was successfully added, removed, or modified; false if
* attrValue is null and attrID is not in this object Throws:
* IllegalArgumentException - if attrID does not represent a 16-bit unsigned
* integer; if attrID is the value of ServiceRecordHandle (0x0000)
* RuntimeException - if this method is called on a ServiceRecord that was
* created by a call to DiscoveryAgent.searchServices()
*/

public boolean setAttributeValue(int attrID, DataElement attrValue) {
/*
* check this is a local service record
*/

if (device != null) {
throw new IllegalArgumentException();
}

if (attrID < 0x0000 || attrID > 0xffff) {
throw new IllegalArgumentException();
}

if (attrID == BluetoothConsts.ServiceRecordHandle) {
throw new IllegalArgumentException();
}

/*
* remove, add or modify attribute
*/

attributeUpdated = true;
if (attrValue == null) {
return (attributes.remove(new Integer(attrID)) != null);
} else {
attributes.put(new Integer(attrID), attrValue);
return true;
}
}

/**
* Internal implementation function
*/
void populateAttributeValue(int attrID, DataElement attrValue) {
if (attrID < 0x0000 || attrID > 0xffff) {
throw new IllegalArgumentException();
}
if (attrValue == null) {
attributes.remove(new Integer(attrID));
} else {
attributes.put(new Integer(attrID), attrValue);
}
}

public String toString() {

StringBuffer buf = new StringBuffer("{\n");

for (Enumeration e = attributes.keys(); e.hasMoreElements();) {
Integer i = (Integer) e.nextElement();

buf.append("0x");
buf.append(Integer.toHexString(i.intValue()));
buf.append(":\n\t");

DataElement d = (DataElement) attributes.get(i);

buf.append(d);
buf.append("\n");
}

buf.append("}");

return buf.toString();
}

/**
* Internal implementation function
*/
long getHandle() {
return this.handle;
}

/**
* Internal implementation function
*/
void setHandle(long handle) {
this.handle = handle;
}

/**
* Internal implementation function
*/
boolean hasServiceClassUUID(UUID uuid) {
DataElement attrDataElement = getAttributeValue(BluetoothConsts.ServiceClassIDList);
if ((attrDataElement == null) || (attrDataElement.getDataType() != DataElement.DATSEQ)
|| attrDataElement.getSize() == 0) {
// DebugLog.debug("Bogus ServiceClassIDList");
return false;
}

Object value = attrDataElement.getValue();
if ((value == null) || (!(value instanceof Enumeration))) {
DebugLog.debug("Bogus Value in DATSEQ");
if (value != null) {
DebugLog.error("DATSEQ class " + value.getClass().getName());
}
return false;
}
for (Enumeration e = (Enumeration) value; e.hasMoreElements();) {
Object element = e.nextElement();
if (!(element instanceof DataElement)) {
DebugLog.debug("Bogus element in DATSEQ, " + value.getClass().getName());
continue;
}
DataElement dataElement = (DataElement) element;
if ((dataElement.getDataType() == DataElement.UUID) && (uuid.equals(dataElement.getValue()))) {
return true;
}
}

return false;
}

boolean hasProtocolClassUUID(UUID uuid) {
DataElement protocolDescriptor = getAttributeValue(BluetoothConsts.ProtocolDescriptorList);
if ((protocolDescriptor == null) || (protocolDescriptor.getDataType() != DataElement.DATSEQ)) {
// DebugLog.debug("Bogus ProtocolDescriptorList");
return false;
}

for (Enumeration protocolsSeqEnum = (Enumeration) protocolDescriptor.getValue(); protocolsSeqEnum
.hasMoreElements();) {
DataElement elementSeq = (DataElement) protocolsSeqEnum.nextElement();

if (elementSeq.getDataType() == DataElement.DATSEQ) {
Enumeration elementSeqEnum = (Enumeration) elementSeq.getValue();
if (elementSeqEnum.hasMoreElements()) {
DataElement protocolElement = (DataElement) elementSeqEnum.nextElement();
if (protocolElement.getDataType() != DataElement.UUID) {
continue;
}
if (uuid.equals(protocolElement.getValue())) {
return true;
}
}
}
}
return false;
}

DataElement clone(DataElement de) {
DataElement c = null;

switch (de.getDataType()) {
case DataElement.U_INT_1:
case DataElement.U_INT_2:
case DataElement.U_INT_4:
case DataElement.INT_1:
case DataElement.INT_2:
case DataElement.INT_4:
c = new DataElement(de.getDataType(), de.getLong());
break;
case DataElement.URL:
case DataElement.STRING:
case DataElement.UUID:
case DataElement.INT_16:
case DataElement.INT_8:
case DataElement.U_INT_16:
c = new DataElement(de.getDataType(), de.getValue());
break;
case DataElement.NULL:
c = new DataElement(de.getDataType());
break;
case DataElement.BOOL:
c = new DataElement(de.getBoolean());
break;
case DataElement.DATSEQ:
case DataElement.DATALT:
c = new DataElement(de.getDataType());
for (Enumeration en = (Enumeration) de.getValue(); en.hasMoreElements();) {
DataElement dataElement = (DataElement) en.nextElement();
c.addElement(clone(dataElement));
}
}

return c;
}

/**
* Internal implementation function
*/
void populateRFCOMMAttributes(long handle, int channel, UUID uuid, String name, boolean obex) {

this.populateAttributeValue(BluetoothConsts.ServiceRecordHandle, new DataElement(DataElement.U_INT_4, handle));

/*
* service class ID list
*/

DataElement serviceClassIDList = new DataElement(DataElement.DATSEQ);
serviceClassIDList.addElement(new DataElement(DataElement.UUID, uuid));
if (!obex) {
serviceClassIDList.addElement(new DataElement(DataElement.UUID, BluetoothConsts.SERIAL_PORT_UUID));
}

this.populateAttributeValue(BluetoothConsts.ServiceClassIDList, serviceClassIDList);

/*
* protocol descriptor list
*/

DataElement protocolDescriptorList = new DataElement(DataElement.DATSEQ);

DataElement L2CAPDescriptor = new DataElement(DataElement.DATSEQ);
L2CAPDescriptor.addElement(new DataElement(DataElement.UUID, BluetoothConsts.L2CAP_PROTOCOL_UUID));
protocolDescriptorList.addElement(L2CAPDescriptor);

DataElement RFCOMMDescriptor = new DataElement(DataElement.DATSEQ);
RFCOMMDescriptor.addElement(new DataElement(DataElement.UUID, BluetoothConsts.RFCOMM_PROTOCOL_UUID));
RFCOMMDescriptor.addElement(new DataElement(DataElement.U_INT_1, channel));
protocolDescriptorList.addElement(RFCOMMDescriptor);

if (obex) {
DataElement OBEXDescriptor = new DataElement(DataElement.DATSEQ);
OBEXDescriptor.addElement(new DataElement(DataElement.UUID, BluetoothConsts.OBEX_PROTOCOL_UUID));
protocolDescriptorList.addElement(OBEXDescriptor);
}

this.populateAttributeValue(BluetoothConsts.ProtocolDescriptorList, protocolDescriptorList);

if (name != null) {
this.populateAttributeValue(BluetoothConsts.AttributeIDServiceName, new DataElement(DataElement.STRING,
name));
}
}

void populateL2CAPAttributes(int handle, int channel, UUID uuid, String name) {

this.populateAttributeValue(BluetoothConsts.ServiceRecordHandle, new DataElement(DataElement.U_INT_4, handle));

/*
* service class ID list
*/

DataElement serviceClassIDList = new DataElement(DataElement.DATSEQ);
serviceClassIDList.addElement(new DataElement(DataElement.UUID, uuid));

this.populateAttributeValue(BluetoothConsts.ServiceClassIDList, serviceClassIDList);

/*
* protocol descriptor list
*/
DataElement protocolDescriptorList = new DataElement(DataElement.DATSEQ);

DataElement L2CAPDescriptor = new DataElement(DataElement.DATSEQ);
L2CAPDescriptor.addElement(new DataElement(DataElement.UUID, BluetoothConsts.L2CAP_PROTOCOL_UUID));
L2CAPDescriptor.addElement(new DataElement(DataElement.U_INT_2, channel));
protocolDescriptorList.addElement(L2CAPDescriptor);

this.populateAttributeValue(BluetoothConsts.ProtocolDescriptorList, protocolDescriptorList);

if (name != null) {
this.populateAttributeValue(BluetoothConsts.AttributeIDServiceName, new DataElement(DataElement.STRING,
name));
}
}
}

Change log

r2975 by skarzhevskyy on Mar 31, 2009   Diff
config property
bluecove.sdp.string_encoding_ascii
Go to: 
Project members, sign in to write a code review

Older revisions

r2974 by skarzhevskyy on Mar 31, 2009   Diff
config property
bluecove.sdp.string_encoding_ascii
r2624 by skarzhevskyy on Dec 19, 2008   Diff
correction to debug messages
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
All revisions of this file

File info

Size: 29651 bytes, 843 lines

File properties

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