diff --git a/components/net/sf/briar/plugins/bluetooth/AbstractListener.java b/components/net/sf/briar/plugins/bluetooth/AbstractListener.java
index 171afa4ba721fca1acefa38f91560beabe624328..e367d5187442ca8f5853ee893f84384f207a2193 100644
--- a/components/net/sf/briar/plugins/bluetooth/AbstractListener.java
+++ b/components/net/sf/briar/plugins/bluetooth/AbstractListener.java
@@ -1,10 +1,14 @@
 package net.sf.briar.plugins.bluetooth;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.bluetooth.DataElement;
 import javax.bluetooth.DiscoveryAgent;
 import javax.bluetooth.DiscoveryListener;
+import javax.bluetooth.UUID;
 
 abstract class AbstractListener implements DiscoveryListener {
 
@@ -46,4 +50,15 @@ abstract class AbstractListener implements DiscoveryListener {
 		}
 		return null;
 	}
+
+	protected void findNestedClassIds(Object o, Collection<String> ids) {
+		o = getDataElementValue(o);
+		if(o instanceof Enumeration) {
+			for(Object o1 : Collections.list((Enumeration<?>) o)) {
+				findNestedClassIds(o1, ids);
+			}
+		} else if(o instanceof UUID) {
+			ids.add(o.toString());
+		}
+	}
 }
diff --git a/components/net/sf/briar/plugins/bluetooth/ContactListener.java b/components/net/sf/briar/plugins/bluetooth/ContactListener.java
index e25493506ec91e737315d68f64dfafc4988b5b47..daf546ce3f110229e44c1663751548fcc7234350 100644
--- a/components/net/sf/briar/plugins/bluetooth/ContactListener.java
+++ b/components/net/sf/briar/plugins/bluetooth/ContactListener.java
@@ -1,14 +1,14 @@
 package net.sf.briar.plugins.bluetooth;
 
+import java.util.Collection;
 import java.util.Collections;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.TreeSet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.bluetooth.BluetoothStateException;
-import javax.bluetooth.DataElement;
 import javax.bluetooth.DeviceClass;
 import javax.bluetooth.DiscoveryAgent;
 import javax.bluetooth.RemoteDevice;
@@ -76,21 +76,14 @@ class ContactListener extends AbstractListener {
 					ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
 			if(serviceUrl == null) continue;
 			// Does this service have the UUID we're looking for?
-			DataElement classIds = record.getAttributeValue(0x1);
-			Object o = getDataElementValue(classIds);
-			if(o instanceof Enumeration) {
-				Enumeration<?> e = (Enumeration<?>) o;
-				for(Object o1 : Collections.list(e)) {
-					Object o2 = getDataElementValue(o1);
-					if(o2 instanceof UUID) {
-						UUID serviceUuid = (UUID) o2;
-						if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
-							// The UUID matches - store the URL
-							urls.put(c, serviceUrl);
-						}
-					}
+			Collection<String> uuids = new TreeSet<String>();
+			findNestedClassIds(record.getAttributeValue(0x1), uuids);
+			for(String u : uuids) {
+				if(uuid.equalsIgnoreCase(u.toString())) {
+					// The UUID matches - store the URL
+					urls.put(c, serviceUrl);
 				}
 			}
 		}
 	}
-}
+}
\ No newline at end of file
diff --git a/components/net/sf/briar/plugins/bluetooth/InvitationListener.java b/components/net/sf/briar/plugins/bluetooth/InvitationListener.java
index 9ed9f10c190059bcb3422f18d20af3047f4ec867..01663b99d21bdeb19a9068fbfafe2748f21a5c27 100644
--- a/components/net/sf/briar/plugins/bluetooth/InvitationListener.java
+++ b/components/net/sf/briar/plugins/bluetooth/InvitationListener.java
@@ -1,12 +1,11 @@
 package net.sf.briar.plugins.bluetooth;
 
-import java.util.Collections;
-import java.util.Enumeration;
+import java.util.Collection;
+import java.util.TreeSet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import javax.bluetooth.BluetoothStateException;
-import javax.bluetooth.DataElement;
 import javax.bluetooth.DeviceClass;
 import javax.bluetooth.DiscoveryAgent;
 import javax.bluetooth.RemoteDevice;
@@ -56,23 +55,16 @@ class InvitationListener extends AbstractListener {
 					ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
 			if(serviceUrl == null) continue;
 			// Does this service have the UUID we're looking for?
-			DataElement classIds = record.getAttributeValue(0x1);
-			Object o = getDataElementValue(classIds);
-			if(o instanceof Enumeration) {
-				Enumeration<?> e = (Enumeration<?>) o;
-				for(Object o1 : Collections.list(e)) {
-					Object o2 = getDataElementValue(o1);
-					if(o2 instanceof UUID) {
-						UUID serviceUuid = (UUID) o2;
-						if(uuid.equalsIgnoreCase(serviceUuid.toString())) {
-							// The UUID matches - store the URL
-							synchronized(this) {
-								url = serviceUrl;
-								finished = true;
-								notifyAll();
-								return;
-							}
-						}
+			Collection<String> uuids = new TreeSet<String>();
+			findNestedClassIds(record.getAttributeValue(0x1), uuids);
+			for(String u : uuids) {
+				if(uuid.equalsIgnoreCase(u)) {
+					// The UUID matches - store the URL
+					synchronized(this) {
+						url = serviceUrl;
+						finished = true;
+						notifyAll();
+						return;
 					}
 				}
 			}