blob: 7ace4cd3ca35cbc0db8eea60dae13fe2325c442f [file] [log] [blame]
/*
* Copyright (C) 2012 Google Inc.
*/
package com.android.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.ParcelUuid;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;
/**
* @hide
*/
final public class Utils {
static final int BD_ADDR_LEN = 6; // bytes
static final int BD_UUID_LEN = 16; // bytes
public static String getAddressStringFromByte(byte[] address) {
if (address == null || address.length !=6) {
return null;
}
return String.format("%02X:%02X:%02X:%02X:%02X:%02X",
address[0], address[1], address[2], address[3], address[4],
address[5]);
}
public static byte[] getByteAddress(BluetoothDevice device) {
return getBytesFromAddress(device.getAddress());
}
public static byte[] getBytesFromAddress(String address) {
int i, j = 0;
byte[] output = new byte[BD_ADDR_LEN];
for (i = 0; i < address.length();i++) {
if (address.charAt(i) != ':') {
output[j] = (byte) Integer.parseInt(address.substring(i, i+2), 16);
j++;
i++;
}
}
return output;
}
public static int byteArrayToInt(byte[] valueBuf) {
return byteArrayToInt(valueBuf, 0);
}
public static short byteArrayToShort(byte[] valueBuf) {
ByteBuffer converter = ByteBuffer.wrap(valueBuf);
converter.order(ByteOrder.nativeOrder());
return converter.getShort();
}
public static int byteArrayToInt(byte[] valueBuf, int offset) {
ByteBuffer converter = ByteBuffer.wrap(valueBuf);
converter.order(ByteOrder.nativeOrder());
return converter.getInt(offset);
}
public static byte[] intToByteArray(int value) {
ByteBuffer converter = ByteBuffer.allocate(4);
converter.order(ByteOrder.nativeOrder());
converter.putInt(value);
return converter.array();
}
public static byte[] uuidToByteArray(ParcelUuid pUuid) {
int length = BD_UUID_LEN;
ByteBuffer converter = ByteBuffer.allocate(length);
converter.order(ByteOrder.BIG_ENDIAN);
long msb, lsb;
UUID uuid = pUuid.getUuid();
msb = uuid.getMostSignificantBits();
lsb = uuid.getLeastSignificantBits();
converter.putLong(msb);
converter.putLong(8, lsb);
return converter.array();
}
public static byte[] uuidsToByteArray(ParcelUuid[] uuids) {
int length = uuids.length * BD_UUID_LEN;
ByteBuffer converter = ByteBuffer.allocate(length);
converter.order(ByteOrder.BIG_ENDIAN);
UUID uuid;
long msb, lsb;
for (int i = 0; i < uuids.length; i++) {
uuid = uuids[i].getUuid();
msb = uuid.getMostSignificantBits();
lsb = uuid.getLeastSignificantBits();
converter.putLong(i * 16, msb);
converter.putLong(i * 16 + 8, lsb);
}
return converter.array();
}
public static ParcelUuid[] byteArrayToUuid(byte[] val) {
int numUuids = val.length/BD_UUID_LEN;
ParcelUuid[] puuids = new ParcelUuid[numUuids];
UUID uuid;
int offset = 0;
ByteBuffer converter = ByteBuffer.wrap(val);
converter.order(ByteOrder.BIG_ENDIAN);
for (int i = 0; i < numUuids; i++) {
puuids[i] = new ParcelUuid(new UUID(converter.getLong(offset),
converter.getLong(offset + 8)));
offset += 16;
}
return puuids;
}
public static String debugGetAdapterStateString(int state) {
switch (state) {
case BluetoothAdapter.STATE_OFF : return "STATE_OFF";
case BluetoothAdapter.STATE_ON : return "STATE_ON";
case BluetoothAdapter.STATE_TURNING_ON : return "STATE_TURNING_ON";
case BluetoothAdapter.STATE_TURNING_OFF : return "STATE_TURNING_OFF";
default : return "UNKNOWN";
}
}
}