View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2008 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: ServiceRecords.java 2502 2008-12-05 16:05:14Z skarzhevskyy $
24   */
25  package net.sf.bluecove.awt;
26  
27  import java.awt.Choice;
28  import java.util.Enumeration;
29  import java.util.Vector;
30  
31  import javax.bluetooth.DataElement;
32  import javax.bluetooth.ServiceRecord;
33  import javax.bluetooth.UUID;
34  
35  import net.sf.bluecove.Configuration;
36  import net.sf.bluecove.RemoteDeviceInfo;
37  import net.sf.bluecove.TestResponderCommon;
38  import net.sf.bluecove.util.CollectionUtils;
39  import net.sf.bluecove.util.BluetoothTypesInfo.UUIDConsts;
40  
41  /**
42   * 
43   */
44  public class ServiceRecords {
45  
46  	public static void populateChoice(Choice choice, boolean obex) {
47  		Vector sorted = new Vector();
48  		for (Enumeration en = RemoteDeviceInfo.services.keys(); en.hasMoreElements();) {
49  			String url = (String) en.nextElement();
50  			if (url.startsWith("btgoep")) {
51  				if (!obex) {
52  					continue;
53  				}
54  			} else if (obex) {
55  				continue;
56  			}
57  			int k = url.indexOf(';');
58  			if (k == -1) {
59  				continue;
60  			}
61  			String info = url.substring(0, k);
62  			info += "|";
63  			ServiceRecord serviceRecord = (ServiceRecord) RemoteDeviceInfo.services.get(url);
64  			while (info.length() < 28) {
65  				info += " ";
66  			}
67  			info += " " + TestResponderCommon.niceDeviceName(serviceRecord.getHostDevice().getBluetoothAddress());
68  			info += " " + UUIDName(serviceRecord);
69  			sorted.addElement(info);
70  		}
71  		CollectionUtils.sort(sorted);
72  		for (Enumeration en = sorted.elements(); en.hasMoreElements();) {
73  			choice.add((String) en.nextElement());
74  		}
75  	}
76  
77  	public static String getChoiceURL(Choice choice) {
78  		String info = choice.getSelectedItem();
79  		int k = info.indexOf('|');
80  		if (k != -1) {
81  			String url = info.substring(0, k);
82  			if (Configuration.encrypt.booleanValue()) {
83  				url += ";authenticate=true;encrypt=true";
84  			} else if (Configuration.authenticate.booleanValue()) {
85  				url += ";authenticate=true";
86  			}
87  			return url;
88  		}
89  		return info;
90  	}
91  
92  	public static String UUIDName(ServiceRecord serviceRecord) {
93  		DataElement d = serviceRecord.getAttributeValue(0x001);
94  		if ((d == null) || (d.getDataType() != DataElement.DATSEQ)) {
95  			return "n/a";
96  		}
97  		final UUID SERIAL_PORT_UUID = new UUID(0x1101);
98  		UUID uuid = null;
99  		Enumeration en = (Enumeration) (d.getValue());
100 		while (en.hasMoreElements()) {
101 			DataElement el = (DataElement) en.nextElement();
102 			if (el.getDataType() != DataElement.UUID) {
103 				continue;
104 			}
105 			UUID u = (UUID) el.getValue();
106 			if (u != null) {
107 				if ((uuid != null) && (u.equals(SERIAL_PORT_UUID))) {
108 					continue;
109 				}
110 				uuid = u;
111 			}
112 		}
113 		if (uuid == null) {
114 			return "n/a";
115 		}
116 		return UUIDConsts.getName(uuid);
117 	}
118 }