Utils.java revision 6654f5c903de510a70f9e72cd5ad7837b615d93f
1/*
2 * Copyright (C) 2012 Google Inc.
3 */
4
5package com.android.bluetooth;
6
7import android.bluetooth.BluetoothAdapter;
8import android.os.ParcelUuid;
9
10import java.nio.ByteBuffer;
11import java.nio.ByteOrder;
12import java.util.UUID;
13
14/**
15 * @hide
16 */
17
18final public class Utils {
19    static final int BD_ADDR_LEN = 6; // bytes
20    static final int BD_UUID_LEN = 16; // bytes
21
22    public static String getAddressStringFromByte(byte[] address) {
23        if (address == null || address.length !=6) {
24            return null;
25        }
26
27        return String.format("%02X:%02X:%02X:%02X:%02X:%02X",
28                address[0], address[1], address[2], address[3], address[4],
29                address[5]);
30    }
31
32    public static byte[] getBytesFromAddress(String address) {
33        int i, j = 0;
34        byte[] output = new byte[BD_ADDR_LEN];
35
36        for (i = 0; i < address.length();i++) {
37            if (address.charAt(i) != ':') {
38                output[j] = (byte) Integer.parseInt(address.substring(i, i+2), 16);
39                j++;
40                i++;
41            }
42        }
43
44        return output;
45    }
46
47    public static int byteArrayToInt(byte[] valueBuf) {
48        return byteArrayToInt(valueBuf, 0);
49    }
50
51    public static short byteArrayToShort(byte[] valueBuf) {
52        ByteBuffer converter = ByteBuffer.wrap(valueBuf);
53        converter.order(ByteOrder.nativeOrder());
54        return converter.getShort();
55    }
56
57    public static int byteArrayToInt(byte[] valueBuf, int offset) {
58        ByteBuffer converter = ByteBuffer.wrap(valueBuf);
59        converter.order(ByteOrder.nativeOrder());
60        return converter.getInt(offset);
61    }
62
63    public static byte[] intToByteArray(int value) {
64        ByteBuffer converter = ByteBuffer.allocate(4);
65        converter.order(ByteOrder.nativeOrder());
66        converter.putInt(value);
67        return converter.array();
68    }
69
70    public static byte[] uuidToByteArray(ParcelUuid pUuid) {
71        int length = BD_UUID_LEN;
72        ByteBuffer converter = ByteBuffer.allocate(length);
73        converter.order(ByteOrder.BIG_ENDIAN);
74        long msb, lsb;
75        UUID uuid = pUuid.getUuid();
76        msb = uuid.getMostSignificantBits();
77        lsb = uuid.getLeastSignificantBits();
78        converter.putLong(msb);
79        converter.putLong(8, lsb);
80        return converter.array();
81    }
82
83    public static byte[] uuidsToByteArray(ParcelUuid[] uuids) {
84        int length = uuids.length * BD_UUID_LEN;
85        ByteBuffer converter = ByteBuffer.allocate(length);
86        converter.order(ByteOrder.BIG_ENDIAN);
87        UUID uuid;
88        long msb, lsb;
89        for (int i = 0; i < uuids.length; i++) {
90            uuid = uuids[i].getUuid();
91            msb = uuid.getMostSignificantBits();
92            lsb = uuid.getLeastSignificantBits();
93            converter.putLong(i * 16, msb);
94            converter.putLong(i * 16 + 8, lsb);
95        }
96        return converter.array();
97    }
98
99    public static ParcelUuid[] byteArrayToUuid(byte[] val) {
100        int numUuids = val.length/BD_UUID_LEN;
101        ParcelUuid[] puuids = new ParcelUuid[numUuids];
102        UUID uuid;
103        int offset = 0;
104
105        ByteBuffer converter = ByteBuffer.wrap(val);
106        converter.order(ByteOrder.BIG_ENDIAN);
107
108        for (int i = 0; i < numUuids; i++) {
109            puuids[i] = new ParcelUuid(new UUID(converter.getLong(offset),
110                    converter.getLong(offset + 8)));
111            offset += 16;
112        }
113        return puuids;
114    }
115
116    public static String debugGetAdapterStateString(int state) {
117        switch (state) {
118            case BluetoothAdapter.STATE_OFF : return "STATE_OFF";
119            case BluetoothAdapter.STATE_ON : return "STATE_ON";
120            case BluetoothAdapter.STATE_TURNING_ON : return "STATE_TURNING_ON";
121            case BluetoothAdapter.STATE_TURNING_OFF : return "STATE_TURNING_OFF";
122            default : return "UNKNOWN";
123        }
124    }
125}
126