001: /*
002: *
003: *
004: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
005: * Reserved. Use is subject to license terms.
006: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License version
010: * 2 only, as published by the Free Software Foundation.
011: *
012: * This program is distributed in the hope that it will be useful, but
013: * WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License version 2 for more details (a copy is
016: * included at /legal/license.txt).
017: *
018: * You should have received a copy of the GNU General Public License
019: * version 2 along with this work; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
021: * 02110-1301 USA
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
024: * Clara, CA 95054 or visit www.sun.com if you need additional
025: * information or have any questions.
026: */
027:
028: /*
029: * (c) Copyright 2001, 2002 Motorola, Inc. ALL RIGHTS RESERVED.
030: */
031: package javax.bluetooth;
032:
033: import java.io.IOException;
034: import javax.microedition.io.Connection;
035: import com.sun.kvem.jsr082.bluetooth.BluetoothConnection;
036: import com.sun.kvem.jsr082.bluetooth.BCC;
037:
038: /**
039: * This class is defined by the JSR-82 specification
040: * <em>Java™ APIs for Bluetooth™ Wireless Technology,
041: * Version 1.1.</em>
042: */
043: // JAVADOC COMMENT ELIDED
044: public class RemoteDevice {
045: // JAVADOC COMMENT ELIDED
046: private long l_address;
047:
048: // JAVADOC COMMENT ELIDED
049: private String s_address;
050:
051: // JAVADOC COMMENT ELIDED
052: private String friendlyName;
053:
054: // JAVADOC COMMENT ELIDED
055: protected RemoteDevice(String address) {
056: if (address == null) {
057: throw new NullPointerException("null address");
058: }
059: final String errorMsg = "Malformed address: " + address;
060:
061: if (address.length() != 12) {
062: throw new IllegalArgumentException(errorMsg);
063: }
064:
065: if (address.startsWith("-")) {
066: throw new IllegalArgumentException(errorMsg);
067: }
068:
069: try {
070: l_address = Long.parseLong(address, 16);
071: } catch (NumberFormatException e) {
072: throw new IllegalArgumentException(errorMsg);
073: }
074:
075: // should be upper case only
076: address = address.toUpperCase();
077:
078: try {
079: String lAddr = LocalDevice.getLocalDevice()
080: .getBluetoothAddress();
081:
082: if (address.equals(lAddr)) {
083: throw new IllegalArgumentException(
084: "Can't use the local address.");
085: }
086: } catch (BluetoothStateException e) {
087: throw new RuntimeException(
088: "Can't initialize bluetooth support");
089: }
090: s_address = address;
091: }
092:
093: // JAVADOC COMMENT ELIDED
094: public boolean isTrustedDevice() {
095: return BCC.getInstance().isTrusted(getBluetoothAddress());
096: }
097:
098: // JAVADOC COMMENT ELIDED
099: public String getFriendlyName(boolean alwaysAsk) throws IOException {
100: // contact the remote device if name is not known or alwaysAsk is true
101: if (friendlyName == null || alwaysAsk) {
102: friendlyName = BCC.getInstance().getFriendlyName(
103: getBluetoothAddress());
104: }
105: return friendlyName;
106: }
107:
108: // JAVADOC COMMENT ELIDED
109: public final String getBluetoothAddress() {
110: return s_address;
111: }
112:
113: // JAVADOC COMMENT ELIDED
114: public boolean equals(Object obj) {
115: return obj instanceof RemoteDevice
116: && l_address == ((RemoteDevice) obj).l_address;
117: }
118:
119: // JAVADOC COMMENT ELIDED
120: public int hashCode() {
121: return (int) ((l_address >>> 24) ^ (l_address & 0xffffffL));
122: }
123:
124: // JAVADOC COMMENT ELIDED
125: public static RemoteDevice getRemoteDevice(Connection conn)
126: throws IOException {
127: return BluetoothConnection.getConnection(conn)
128: .getRemoteDevice();
129: }
130:
131: // JAVADOC COMMENT ELIDED
132: public boolean authenticate() throws IOException {
133: if (!BCC.getInstance().isConnected(getBluetoothAddress())) {
134: throw new IOException(
135: "There are no open connections between the "
136: + "local device and this RemoteDevice.");
137: }
138:
139: return BCC.getInstance().authenticate(getBluetoothAddress());
140: }
141:
142: // JAVADOC COMMENT ELIDED
143: public boolean authorize(Connection conn) throws IOException {
144: BluetoothConnection btconn = BluetoothConnection
145: .getConnection(conn);
146:
147: if (!equals(btconn.getRemoteDevice())) {
148: throw new IllegalArgumentException(
149: "The specified connection "
150: + "is not a connection to this RemoteDevice.");
151: }
152: if (!btconn.isServerSide()) {
153: throw new IllegalArgumentException(
154: "The local device is client "
155: + "rather than the server for the specified connection.");
156: }
157:
158: return authenticate()
159: && (isTrustedDevice() || btconn.isAuthorized() || btconn
160: .authorize());
161: }
162:
163: // JAVADOC COMMENT ELIDED
164: public boolean encrypt(Connection conn, boolean on)
165: throws IOException {
166: BluetoothConnection btconn = BluetoothConnection
167: .getConnection(conn);
168: if (!equals(btconn.getRemoteDevice())) {
169: throw new IllegalArgumentException(
170: "The specified connection "
171: + "is not a connection to this RemoteDevice.");
172: }
173: if (on && !authenticate()) {
174: return false;
175: }
176: return btconn.encrypt(on);
177: }
178:
179: // JAVADOC COMMENT ELIDED
180: public boolean isAuthenticated() {
181: return BCC.getInstance().isAuthenticated(getBluetoothAddress());
182: }
183:
184: // JAVADOC COMMENT ELIDED
185: public boolean isAuthorized(Connection conn) throws IOException {
186: BluetoothConnection btconn = BluetoothConnection
187: .getConnection(conn);
188: RemoteDevice device;
189:
190: try {
191: device = btconn.getRemoteDevice();
192: } catch (IllegalArgumentException e) {
193: return false;
194: }
195: if (!equals(device)) {
196: throw new IllegalArgumentException(
197: "The specified connection "
198: + "is not a connection to this RemoteDevice.");
199: }
200:
201: return btconn.isServerSide() && btconn.isAuthorized();
202: }
203:
204: // JAVADOC COMMENT ELIDED
205: public boolean isEncrypted() {
206: return BCC.getInstance().isEncrypted(getBluetoothAddress());
207: }
208: } // end of class 'RemoteDevice' definition
|