View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Michael Lifshits
4    *  Copyright (C) 2008 Vlad Skarzhevskyy
5    *
6    *  Licensed to the Apache Software Foundation (ASF) under one
7    *  or more contributor license agreements.  See the NOTICE file
8    *  distributed with this work for additional information
9    *  regarding copyright ownership.  The ASF licenses this file
10   *  to you under the Apache License, Version 2.0 (the
11   *  "License"); you may not use this file except in compliance
12   *  with the License.  You may obtain a copy of the License at
13   *
14   *    http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing,
17   *  software distributed under the License is distributed on an
18   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   *  KIND, either express or implied.  See the License for the
20   *  specific language governing permissions and limitations
21   *  under the License.
22   *
23   *  @author vlads
24   *  @version $Id: EmulatorServiceConnection.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
25   */
26  package com.intel.bluetooth;
27  
28  import java.io.IOException;
29  import java.util.Enumeration;
30  import java.util.Vector;
31  
32  import javax.bluetooth.DataElement;
33  import javax.bluetooth.ServiceRegistrationException;
34  
35  import com.intel.bluetooth.emu.ServicesDescriptor;
36  
37  /**
38   * 
39   */
40  abstract class EmulatorServiceConnection extends EmulatorConnection {
41  
42  	protected BluetoothConnectionNotifierParams params;
43  
44  	EmulatorServiceConnection(EmulatorLocalDevice localDevice, long handle) {
45  		super(localDevice, handle);
46  	}
47  
48  	private void addServiceClassUUID(Vector<String> uuids, ServiceRecordImpl serviceRecord) {
49  		DataElement attrDataElement = serviceRecord.getAttributeValue(BluetoothConsts.ServiceClassIDList);
50  		if ((attrDataElement == null) || (attrDataElement.getDataType() != DataElement.DATSEQ)
51  				|| attrDataElement.getSize() == 0) {
52  			return;
53  		}
54  
55  		Object value = attrDataElement.getValue();
56  		if ((value == null) || (!(value instanceof Enumeration))) {
57  			return;
58  		}
59  		for (Enumeration<?> e = (Enumeration<?>) value; e.hasMoreElements();) {
60  			Object element = e.nextElement();
61  			if (!(element instanceof DataElement)) {
62  				continue;
63  			}
64  			DataElement dataElement = (DataElement) element;
65  			if ((dataElement.getDataType() == DataElement.UUID) && (!uuids.contains(dataElement.getValue().toString()))) {
66  				uuids.add(dataElement.getValue().toString());
67  			}
68  		}
69  	}
70  
71  	private void addProtocolDescriptorUUID(Vector<String> uuids, ServiceRecordImpl serviceRecord) {
72  		DataElement protocolDescriptor = serviceRecord.getAttributeValue(BluetoothConsts.ProtocolDescriptorList);
73  		if ((protocolDescriptor == null) || (protocolDescriptor.getDataType() != DataElement.DATSEQ)) {
74  			return;
75  		}
76  		for (Enumeration<?> protocolsSeqEnum = (Enumeration<?>) protocolDescriptor.getValue(); protocolsSeqEnum
77  				.hasMoreElements();) {
78  			Object element = protocolsSeqEnum.nextElement();
79  			if (!(element instanceof DataElement)) {
80  				throw new IllegalArgumentException("SDP protocol descriptor list");
81  			}
82  			DataElement elementSeq = (DataElement) element;
83  
84  			if (elementSeq.getDataType() == DataElement.DATSEQ) {
85  				Enumeration<?> elementSeqEnum = (Enumeration<?>) elementSeq.getValue();
86  				if (elementSeqEnum.hasMoreElements()) {
87  					DataElement protocolElement = (DataElement) elementSeqEnum.nextElement();
88  					if ((protocolElement.getDataType() == DataElement.UUID)
89  							&& (!uuids.contains(protocolElement.getValue().toString()))) {
90  						uuids.add(protocolElement.getValue().toString());
91  					}
92  				}
93  			}
94  		}
95  	}
96  
97  	void updateServiceRecord(ServiceRecordImpl serviceRecord) throws ServiceRegistrationException {
98  		Vector<String> uuids = new Vector<String>();
99  		addServiceClassUUID(uuids, serviceRecord);
100 		addProtocolDescriptorUUID(uuids, serviceRecord);
101 		byte[] sdpBinary;
102 		try {
103 			sdpBinary = serviceRecord.toByteArray();
104 		} catch (IOException e) {
105 			throw (ServiceRegistrationException) UtilsJavaSE.initCause(
106 					new ServiceRegistrationException(e.getMessage()), e);
107 		}
108 		localDevice.getDeviceManagerService().updateServiceRecord(
109 				localDevice.getAddress(),
110 				serviceRecord.getHandle(),
111 				new ServicesDescriptor((String[]) uuids.toArray(new String[uuids.size()]), sdpBinary,
112 						serviceRecord.deviceServiceClasses));
113 	}
114 }