FileDocCategorySizeDatePackage
BluetoothUtils.javaAPI DocphoneME MR2 API (J2ME)3552Wed May 02 18:00:30 BST 2007com.sun.midp.jsr082

BluetoothUtils

public class BluetoothUtils extends Object
Class contains Bluetooth helper methods.

Fields Summary
public static final int
BTADDR_SIZE
Size of byte representation of Bluetooth address.
Constructors Summary
Methods Summary
public static byte[]getAddressBytes(java.lang.String btaddr)
Converts Bluetooth address from string to byte array representation.

param
btaddr Bluetooth address as string in user-friendly byte order (big-endian) without comma separator
return
6-bytes byte array containing Bluetooth address in Bluetooth byte order (little-endian)
throws
IllegalArgumentException if input address is invalid

        final int len = btaddr.length() / 2;
            if (len != BTADDR_SIZE) {
                throw new IllegalArgumentException("Incorrect address size");
            }
        
        byte[] bytes = new byte[len];
        try {
            for (int i = 0; i < len; i++) {
                String s = btaddr.substring(i * 2, i * 2 + 2);
                bytes[len - 1 - i] = (byte)Integer.parseInt(s, 16);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Incorrect address value");
        }
        
        return bytes;
    
public static java.lang.StringgetAddressString(byte[] btaddr)
Converts Bluetooth address from byte array to string representation.

param
btaddr 6-bytes byte array containing Bluetooth address in Bluetooth byte order (little-endian)
return
Bluetooth address as string in user-friendly byte order (big-endian) without comma separator
throws
IllegalArgumentException if input address is invalid

    
                                                                         
        
              
        final int len = btaddr.length;
            if (len != BTADDR_SIZE) {
                throw new IllegalArgumentException("Incorrect address size");
            }
        
        StringBuffer sb = new StringBuffer(len * 2);        
        String s;        
        for (int i = (len - 1); i >= 0; i--) {
            // convert decimal to hexadecimal with leading zeroes and uppercase
            s = Integer.toHexString((btaddr[i] >> 4) & 0xF);
            sb.append(s.toUpperCase());
            s = Integer.toHexString(btaddr[i] & 0xF);
            sb.append(s.toUpperCase());
        }
        
        return sb.toString();