001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package com.sun.kvem.jsr082.bluetooth;
027:
028: import javax.bluetooth.*;
029: import com.sun.midp.jsr082.bluetooth.BluetoothStack;
030: import java.util.Vector;
031:
032: /**
033: * Native-based Bluetooth Control Center. For many operations, this
034: * implementation relies on BluetoothStack class.
035: */
036: public class NativeBCC extends BCC {
037:
038: /**
039: * Delimiter character used to separate entries in packed strings returned
040: * by <code>getPreknown()</code>.
041: */
042: private final char ADDR_DELIMETER = ':';
043:
044: /**
045: * Constructs the only instance of this class.
046: */
047: protected NativeBCC() {
048: initialize();
049: }
050:
051: /**
052: * Allocates native resources.
053: */
054: private native void initialize();
055:
056: /**
057: * Releases native resources.
058: */
059: private native void finalize();
060:
061: /**
062: * Enables Bluetooth radio and the Bluetooth protocol stack for use.
063: *
064: * @return true if the operation succeeded, false otherwise
065: */
066: public boolean enableBluetooth() {
067: if (BluetoothStack.getInstance().isEnabled()) {
068: return true;
069: }
070: if (!confirmEnable()) {
071: return false;
072: }
073: return BluetoothStack.getInstance().enable();
074: }
075:
076: /**
077: * Queries the power state of the Bluetooth device.
078: *
079: * @return <code>true</code> is the Bluetooth device is on,
080: * <code>false</code> otherwise.
081: */
082: public boolean isBluetoothEnabled() {
083: return BluetoothStack.getInstance().isEnabled();
084: }
085:
086: /**
087: * Asks user whether Bluetooth radio is allowed to be turned on.
088: *
089: * @return true if user has allowed to enable Bluetooth, false otherwise
090: */
091: public native boolean confirmEnable();
092:
093: /**
094: * Returns local Bluetooth address.
095: *
096: * @return local Bluetooth address.
097: */
098: public String getBluetoothAddress() {
099: return BluetoothStack.getInstance().getLocalAddress();
100: }
101:
102: /**
103: * Determines if the local device is in connectable mode.
104: *
105: * @return true if the device is connectable, false otherwise
106: */
107: public native boolean isConnectable();
108:
109: /**
110: * Returns user-friendly name for the local device.
111: *
112: * @return user-friendly name for the local device, or
113: * null if the name could not be retrieved
114: * @see LocalDevice#getFriendlyName
115: */
116: public String getFriendlyName() {
117: return BluetoothStack.getInstance().getLocalName();
118: }
119:
120: /**
121: * Retrieves the user-friendly name for specified remote device.
122: *
123: * @param address Bluetooth address of a remote device
124: * @return name of the remote device, or
125: * null if the name could not be retrieved
126: * @see RemoteDevice#getFriendlyName
127: */
128: public String getFriendlyName(String address) {
129: return BluetoothStack.getInstance()
130: .askFriendlyNameSync(address);
131: }
132:
133: // JAVADOC COMMENT ELIDED
134: public DeviceClass getDeviceClass() {
135: return new DeviceClass(BluetoothStack.getInstance()
136: .getDeviceClass());
137: }
138:
139: // JAVADOC COMMENT ELIDED
140: public boolean setServiceClasses(int classes) {
141: return BluetoothStack.getInstance().setServiceClasses(classes);
142: }
143:
144: // JAVADOC COMMENT ELIDED
145: public int getAccessCode() {
146: return BluetoothStack.getInstance().getAccessCode();
147: }
148:
149: // JAVADOC COMMENT ELIDED
150: public boolean setAccessCode(int accessCode) {
151: return BluetoothStack.getInstance().setAccessCode(accessCode);
152: }
153:
154: /**
155: * Checks if the local device has a bond with a remote device.
156: *
157: * @param address Bluetooth address of a remote device
158: * @return true if the two devices were paired, false otherwise
159: */
160: public native boolean isPaired(String address);
161:
162: /**
163: * Checks if a remote device was authenticated.
164: *
165: * @param address Bluetooth address of a remote device
166: * @return true if the device was authenticated, false otherwise
167: */
168: public native boolean isAuthenticated(String address);
169:
170: /**
171: * Checks if a remote device is trusted (authorized for all services).
172: *
173: * @param address Bluetooth address of a remote device
174: * @return true if the device is trusted, false otherwise
175: */
176: public native boolean isTrusted(String address);
177:
178: /**
179: * Checks if connections to a remote device are encrypted.
180: *
181: * @param address Bluetooth address of the remote device
182: * @return true if connections to the device are encrypted, false otherwise
183: */
184: public native boolean isEncrypted(String address);
185:
186: /**
187: * Retrieves PIN code to use for pairing with a remote device. If the
188: * PIN code is not known, PIN entry dialog is displayed.
189: *
190: * @param address the Bluetooth address of the remote device
191: * @return string containing the PIN code
192: */
193: public native String getPasskey(String address);
194:
195: /**
196: * Initiates pairing with a remote device.
197: *
198: * @param address the Bluetooth address of the device with which to pair
199: * @param pin an array containing the PIN code
200: * @return true if the device was authenticated, false otherwise
201: */
202: public native boolean bond(String address, String pin);
203:
204: /**
205: * Authenticates remote device.
206: *
207: * @param address Bluetooth address of a remote device
208: * @return true if the device was authenticated, false otherwise
209: */
210: public boolean authenticate(String address) {
211: if (isAuthenticated(address)) {
212: return true;
213: }
214: if (!isPaired(address)) {
215: String pin = getPasskey(address);
216: if (pin == null || !bond(address, pin)) {
217: return false;
218: }
219: }
220:
221: return BluetoothStack.getInstance().authenticateSync(address);
222: }
223:
224: /**
225: * Authorizes a Bluetooth connection.
226: *
227: * @param address Bluetooth address of a remote device
228: * @param handle handle for the service record of the srvice the remote
229: * device is trying to access
230: * @return true if authorization succeeded, false otherwise
231: */
232: public native boolean authorize(String address, int handle);
233:
234: /**
235: * Enables or disables encryption of data exchanges.
236: *
237: * @param address the Bluetooth address of the remote device
238: * @param enable indicated whether the encryption needs to be enabled
239: * @return true if the encryption has been changed, false otherwise
240: */
241: public boolean encrypt(String address, boolean enable) {
242: if (setEncryption(address, enable)) {
243: return BluetoothStack.getInstance().encryptSync(address,
244: enable);
245: }
246: return false;
247: }
248:
249: /**
250: * Returns list of preknown devices in a packed string.
251: *
252: * @return vector containing preknown devices
253: */
254: public Vector getPreknownDevices() {
255: return listDevices(getPreknown());
256: }
257:
258: /**
259: * Returns list of preknown devices in a packed string.
260: *
261: * @return packed string containing preknown devices
262: */
263: private native String getPreknown();
264:
265: /**
266: * Extracts Bluetooth addresses from a packed string.
267: * In the packed string, each device entry is a Bluetooth device address
268: * followed by <code>ADDR_DELIMETER</code> delimiter.
269: *
270: * @param packed string containing Bluetooth addresses
271: * @return Vector containing Bluetooth addresses
272: */
273: private Vector listDevices(String packed) {
274: if (packed == null || packed.trim().length() == 0) {
275: return null;
276: }
277:
278: Vector addrs = new Vector();
279: int index = 0;
280: while (index < packed.length()) {
281: int end = packed.indexOf(ADDR_DELIMETER, index);
282: if (end == -1) {
283: end = packed.length();
284: }
285: addrs.addElement(packed.substring(index, end));
286: index = end + 1;
287: }
288: return addrs;
289: }
290:
291: /**
292: * Checks if there is a connection to the remote device.
293: *
294: * @param address the Bluetooth address of the remote device
295: * @return true if connection is established with the remote device
296: */
297: public native boolean isConnected(String address);
298:
299: /**
300: * Increases or decreases encryption request counter for a remote device.
301: *
302: * @param address the Bluetooth address of the remote device
303: * @param enable indicated whether the encryption needs to be enabled
304: * @return true if the encryption needs to been changed, false otherwise
305: */
306: public native boolean setEncryption(String address, boolean enable);
307:
308: }
|