def find_service(name=None, uuid=None, address=None): if address is not None: addresses = [address] else: addresses = discover_devices(lookup_names=False) results = [] for addr in addresses: uuidstr = uuid or PUBLIC_BROWSE_GROUP if not is_valid_uuid(uuidstr): raise ValueError("invalid UUID") uuidstr = to_full_uuid(uuidstr) dresults = bt.find_service(addr, uuidstr) for dict in dresults: raw = dict["rawrecord"] record = sdp_parse_raw_record(raw) if SERVICE_CLASS_ID_LIST_ATTRID in record: svc_class_id_list = [ t[1] for t in \ record[SERVICE_CLASS_ID_LIST_ATTRID] ] dict["service-classes"] = svc_class_id_list else: dict["services-classes"] = [] if BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRID in record: pdl = [] for profile_desc in \ record[BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRID]: uuidpair, versionpair = profile_desc[1] pdl.append((uuidpair[1], versionpair[1])) dict["profiles"] = pdl else: dict["profiles"] = [] dict["provider"] = record.get(PROVIDER_NAME_ATTRID, None) dict["service-id"] = record.get(SERVICE_ID_ATTRID, None) # XXX the C version is buggy (retrieves an extra byte or two), # so get the service name here even though it may have already # been set dict["name"] = record.get(SERVICE_NAME_ATTRID, None) dict["handle"] = record.get(SERVICE_RECORD_HANDLE_ATTRID, None) # if LANGUAGE_BASE_ATTRID_LIST_ATTRID in record: # for triple in record[LANGUAGE_BASE_ATTRID_LIST_ATTRID]: # code_ISO639, encoding, base_offset = triple # # if SERVICE_DESCRIPTION_ATTRID in record: # service_description = record[SERVICE_DESCRIPTION_ATTRID] if name is None: results.extend(dresults) else: results.extend([d for d in dresults if d["name"] == name]) return results
def find_service (name = None, uuid = None, address = None): if address is not None: addresses = [ address ] else: addresses = discover_devices (lookup_names = False) results = [] for addr in addresses: uuidstr = uuid or PUBLIC_BROWSE_GROUP if not is_valid_uuid (uuidstr): raise ValueError ("invalid UUID") uuidstr = to_full_uuid (uuidstr) dresults = bt.find_service (addr, uuidstr) for dict in dresults: raw = dict["rawrecord"] record = sdp_parse_raw_record (raw) if SERVICE_CLASS_ID_LIST_ATTRID in record: svc_class_id_list = [ t[1] for t in \ record[SERVICE_CLASS_ID_LIST_ATTRID] ] dict["service-classes"] = svc_class_id_list else: dict["services-classes"] = [] if BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRID in record: pdl = [] for profile_desc in \ record[BLUETOOTH_PROFILE_DESCRIPTOR_LIST_ATTRID]: uuidpair, versionpair = profile_desc[1] pdl.append ((uuidpair[1], versionpair[1])) dict["profiles"] = pdl else: dict["profiles"] = [] dict["provider"] = record.get (PROVIDER_NAME_ATTRID, None) dict["service-id"] = record.get (SERVICE_ID_ATTRID, None) # XXX the C version is buggy (retrieves an extra byte or two), # so get the service name here even though it may have already # been set dict["name"] = record.get (SERVICE_NAME_ATTRID, None) dict["handle"] = record.get (SERVICE_RECORD_HANDLE_ATTRID, None) # if LANGUAGE_BASE_ATTRID_LIST_ATTRID in record: # for triple in record[LANGUAGE_BASE_ATTRID_LIST_ATTRID]: # code_ISO639, encoding, base_offset = triple # # if SERVICE_DESCRIPTION_ATTRID in record: # service_description = record[SERVICE_DESCRIPTION_ATTRID] if name is None: results.extend (dresults) else: results.extend ([ d for d in dresults if d["name"] == name ]) return results