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 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 }