01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package com.sun.midp.jsr082;
28:
29: /**
30: * Class contains Bluetooth helper methods.
31: */
32: public class BluetoothUtils {
33: /** Size of byte representation of Bluetooth address. */
34: public static final int BTADDR_SIZE = 6;
35:
36: /**
37: * Converts Bluetooth address from byte array to string representation.
38: *
39: * @param btaddr 6-bytes byte array containing Bluetooth address in
40: * Bluetooth byte order (little-endian)
41: * @return Bluetooth address as string in
42: * user-friendly byte order (big-endian) without comma separator
43: * @throws IllegalArgumentException if input address is invalid
44: */
45: public static String getAddressString(byte[] btaddr)
46: throws IllegalArgumentException {
47: final int len = btaddr.length;
48: if (len != BTADDR_SIZE) {
49: throw new IllegalArgumentException("Incorrect address size");
50: }
51:
52: StringBuffer sb = new StringBuffer(len * 2);
53: String s;
54: for (int i = (len - 1); i >= 0; i--) {
55: // convert decimal to hexadecimal with leading zeroes and uppercase
56: s = Integer.toHexString((btaddr[i] >> 4) & 0xF);
57: sb.append(s.toUpperCase());
58: s = Integer.toHexString(btaddr[i] & 0xF);
59: sb.append(s.toUpperCase());
60: }
61:
62: return sb.toString();
63: }
64:
65: /**
66: * Converts Bluetooth address from string to byte array representation.
67: *
68: * @param btaddr Bluetooth address as string in
69: * user-friendly byte order (big-endian) without comma separator
70: * @return 6-bytes byte array containing Bluetooth address in
71: * Bluetooth byte order (little-endian)
72: * @throws IllegalArgumentException if input address is invalid
73: */
74: public static byte[] getAddressBytes(String btaddr)
75: throws IllegalArgumentException {
76: final int len = btaddr.length() / 2;
77: if (len != BTADDR_SIZE) {
78: throw new IllegalArgumentException("Incorrect address size");
79: }
80:
81: byte[] bytes = new byte[len];
82: try {
83: for (int i = 0; i < len; i++) {
84: String s = btaddr.substring(i * 2, i * 2 + 2);
85: bytes[len - 1 - i] = (byte) Integer.parseInt(s, 16);
86: }
87: } catch (NumberFormatException e) {
88: throw new IllegalArgumentException(
89: "Incorrect address value");
90: }
91:
92: return bytes;
93: }
94: }
|