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