View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 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: RemoteDeviceManager.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove.se;
26  
27  import java.io.IOException;
28  
29  import javax.bluetooth.BluetoothStateException;
30  import javax.bluetooth.DiscoveryAgent;
31  import javax.bluetooth.LocalDevice;
32  import javax.bluetooth.RemoteDevice;
33  
34  import org.bluecove.tester.log.Logger;
35  
36  
37  /**
38   * 
39   */
40  public class RemoteDeviceManager {
41  
42  	public static void retrieveDevices(int option) {
43  		try {
44  			DiscoveryAgent discoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
45  			RemoteDevice[] devices = discoveryAgent.retrieveDevices(option);
46  			String optStr = "";
47  			switch (option) {
48  			case DiscoveryAgent.CACHED:
49  				optStr = "CACHED";
50  				break;
51  			case DiscoveryAgent.PREKNOWN:
52  				optStr = "PREKNOWN";
53  				break;
54  			}
55  			if (devices == null) {
56  				Logger.debug("no devices meet the " + optStr + " criteria");
57  				return;
58  			}
59  			Logger.debug("-- List of  " + optStr + " remote devices");
60  			for (int i = 0; i < devices.length; i++) {
61  				StringBuffer b = new StringBuffer();
62  				b.append(devices[i].getBluetoothAddress()).append(' ');
63  				boolean trusted = devices[i].isTrustedDevice();
64  				b.append("Trusted:").append(trusted).append(' ');
65  				// Padding with spaces for English
66  				if (trusted) {
67  					b.append(' ');
68  				}
69  				boolean authenticated = devices[i].isAuthenticated();
70  				b.append("Authenticated:").append(authenticated).append(' ');
71  				// Padding with spaces for English
72  				if (authenticated) {
73  					b.append(' ');
74  				}
75  				try {
76  					b.append(devices[i].getFriendlyName(false));
77  				} catch (IOException e) {
78  				}
79  				Logger.debug(b.toString());
80  			}
81  
82  		} catch (BluetoothStateException e) {
83  			Logger.error("retrieve", e);
84  		}
85  
86  	}
87  }