FileDocCategorySizeDatePackage
BluetoothServiceFinder.javaAPI DocExample4372Thu Feb 16 12:43:16 GMT 2006None

BluetoothServiceFinder

public class BluetoothServiceFinder extends Object implements DiscoveryListener

(Omit source code)

Fields Summary
private DiscoveryAgent
agent
private int
serviceSearchCount
private ServiceRecord
record
private Vector
devices
private String
uuid
private int[]
transactions
Constructors Summary
private BluetoothServiceFinder(String serviceUUID)


     
      
    this.uuid = serviceUUID;
    agent = LocalDevice.getLocalDevice().getDiscoveryAgent();
    int maxSimultaneousSearches = Integer.parseInt(
     LocalDevice.getProperty("bluetooth.sd.trans.max"));
    transactions = new int[maxSimultaneousSearches];
    // We need to initialize the transactions list with illegal
    // values. According to spec, the transaction ID is supposed to be
    // positive, and thus non-zero. However, several implementations
    // get this wrong and use zero as a transaction ID.
    for (int i = 0; i < maxSimultaneousSearches; i++) {
      transactions[i] = -1;
    }
  
Methods Summary
private voidaddTransaction(int transactionID)

    for (int i = 0; i < transactions.length; i++) {
      if (transactions[i] == -1) {
        transactions[i] = transactionID;
        return;
      }
    }
  
public voiddeviceDiscovered(RemoteDevice device, DeviceClass type)

    devices.addElement(device);
  
public static java.lang.StringgetConnectionURL(java.lang.String uuid)

    BluetoothServiceFinder finder 
     = new BluetoothServiceFinder(BluetoothReceiver.UUID);
    return finder.getFirstURL();
  
private java.lang.StringgetFirstURL()

    try {
      agent.startInquiry(DiscoveryAgent.GIAC, this);
      synchronized (this) {
        try {
          this.wait();
        } 
        catch (InterruptedException ex) {
        }
      }
    } 
    catch (BluetoothStateException ex) {
      System.out.println("No devices in range");
    }

    if (devices.size() > 0) {
      RemoteDevice[] remotes = new RemoteDevice[devices.size()];
      devices.copyInto(remotes);
      if (searchServices(remotes)) {
        return record.getConnectionURL(
          ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
      }
    }
    return null;
  
public voidinquiryCompleted(int discType)

    synchronized (this) {
      this.notifyAll();
    }
  
private voidremoveTransaction(int transactionID)

    for (int i = 0; i < transactions.length; i++) {
      if (transactions[i] == transactionID) {
        transactions[i] = -1;
        return;
      }
    }
  
private booleansearchServices(RemoteDevice[] devices)

    UUID[] searchList = { new UUID(uuid, false) };
    for (int i = 0; i < devices.length; i++) {
      if (record != null) {
        return true;
      }
      try {
                                         // don't care about attributes
        int transactionID = agent.searchServices(null, searchList, devices[i], this);
        addTransaction(transactionID);
      } 
      catch (BluetoothStateException ex) {
      }

      synchronized (this) {
        serviceSearchCount++;
        if (serviceSearchCount == transactions.length) {
          try {
            this.wait();
          } 
          catch (InterruptedException ex) {
            // continue
          }
        }
      }
    }

    while (serviceSearchCount > 0) { // unfinished searches
      synchronized (this) {
        try {
          this.wait();
        } 
        catch (InterruptedException ex) {
          // continue
        }
      }
    }
    if (record != null) return true;
    else return false;
  
public voidserviceSearchCompleted(int transactionID, int responseCode)

    removeTransaction(transactionID);
    serviceSearchCount--;
    synchronized (this) {
      this.notifyAll();
    }
  
public voidservicesDiscovered(int transactionID, ServiceRecord[] records)

    if (record == null) {
      record = records[0];
      for (int i = 0; i < transactions.length; i++) {
        if (transactions[i] != -1) {
          agent.cancelServiceSearch(transactions[i]);
        }
      }
    }