diff options
| author | Jakub Pawlowski <jpawlowski@google.com> | 2016-10-25 21:47:35 +0000 |
|---|---|---|
| committer | android-build-merger <android-build-merger@google.com> | 2016-10-25 21:47:35 +0000 |
| commit | dc4f67ecba79cada8d15d2ba4ce4616313212ea7 (patch) | |
| tree | fadc8653fcc71880df9f259d9b7df7a9be7c4c4b /framework/java | |
| parent | f23eea17db1c2caf3def942ee3b18080db94c471 (diff) | |
| parent | 0028ed262468cc5e55e1073ef91eb99fd9ac1be3 (diff) | |
Merge "Add helper method to convert Bluetooth UUID to bytes"
am: 0028ed2624
Change-Id: I829135e559a9c3cf3c942258a16acee62c37c3ac
Diffstat (limited to 'framework/java')
| -rw-r--r-- | framework/java/android/bluetooth/BluetoothUuid.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/framework/java/android/bluetooth/BluetoothUuid.java b/framework/java/android/bluetooth/BluetoothUuid.java index 2ded4c8fea..243579a31c 100644 --- a/framework/java/android/bluetooth/BluetoothUuid.java +++ b/framework/java/android/bluetooth/BluetoothUuid.java @@ -275,6 +275,48 @@ public final class BluetoothUuid { } /** + * Parse UUID to bytes. The returned value is shortest representation, a 16-bit, 32-bit or 128-bit UUID, + * Note returned value is little endian (Bluetooth). + * + * @param uuid uuid to parse. + * @return shortest representation of {@code uuid} as bytes. + * @throws IllegalArgumentException If the {@code uuid} is null. + */ + public static byte[] uuidToBytes(ParcelUuid uuid) { + if (uuid == null) { + throw new IllegalArgumentException("uuid cannot be null"); + } + + if (is16BitUuid(uuid)) { + byte[] uuidBytes = new byte[UUID_BYTES_16_BIT]; + int uuidVal = getServiceIdentifierFromParcelUuid(uuid); + uuidBytes[0] = (byte)(uuidVal & 0xFF); + uuidBytes[1] = (byte)((uuidVal & 0xFF00) >> 8); + return uuidBytes; + } + + if (is32BitUuid(uuid)) { + byte[] uuidBytes = new byte[UUID_BYTES_32_BIT]; + int uuidVal = getServiceIdentifierFromParcelUuid(uuid); + uuidBytes[0] = (byte)(uuidVal & 0xFF); + uuidBytes[1] = (byte)((uuidVal & 0xFF00) >> 8); + uuidBytes[2] = (byte)((uuidVal & 0xFF0000) >> 16); + uuidBytes[3] = (byte)((uuidVal & 0xFF000000) >> 24); + return uuidBytes; + } + + // Construct a 128 bit UUID. + long msb = uuid.getUuid().getMostSignificantBits(); + long lsb = uuid.getUuid().getLeastSignificantBits(); + + byte[] uuidBytes = new byte[UUID_BYTES_128_BIT]; + ByteBuffer buf = ByteBuffer.wrap(uuidBytes).order(ByteOrder.LITTLE_ENDIAN); + buf.putLong(8, msb); + buf.putLong(0, lsb); + return uuidBytes; + } + + /** * Check whether the given parcelUuid can be converted to 16 bit bluetooth uuid. * * @param parcelUuid |
