• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth;
18 
19 import android.app.ActivityManager;
20 import android.bluetooth.BluetoothAdapter;
21 import android.bluetooth.BluetoothDevice;
22 import android.os.Binder;
23 import android.os.ParcelUuid;
24 import android.os.UserHandle;
25 import android.util.Log;
26 
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.nio.ByteBuffer;
31 import java.nio.ByteOrder;
32 import java.util.UUID;
33 
34 /**
35  * @hide
36  */
37 
38 final public class Utils {
39     private static final String TAG = "BluetoothUtils";
40     static final int BD_ADDR_LEN = 6; // bytes
41     static final int BD_UUID_LEN = 16; // bytes
42 
getAddressStringFromByte(byte[] address)43     public static String getAddressStringFromByte(byte[] address) {
44         if (address == null || address.length !=6) {
45             return null;
46         }
47 
48         return String.format("%02X:%02X:%02X:%02X:%02X:%02X",
49                 address[0], address[1], address[2], address[3], address[4],
50                 address[5]);
51     }
52 
getByteAddress(BluetoothDevice device)53     public static byte[] getByteAddress(BluetoothDevice device) {
54         return getBytesFromAddress(device.getAddress());
55     }
56 
getBytesFromAddress(String address)57     public static byte[] getBytesFromAddress(String address) {
58         int i, j = 0;
59         byte[] output = new byte[BD_ADDR_LEN];
60 
61         for (i = 0; i < address.length();i++) {
62             if (address.charAt(i) != ':') {
63                 output[j] = (byte) Integer.parseInt(address.substring(i, i+2), 16);
64                 j++;
65                 i++;
66             }
67         }
68 
69         return output;
70     }
71 
byteArrayToInt(byte[] valueBuf)72     public static int byteArrayToInt(byte[] valueBuf) {
73         return byteArrayToInt(valueBuf, 0);
74     }
75 
byteArrayToShort(byte[] valueBuf)76     public static short byteArrayToShort(byte[] valueBuf) {
77         ByteBuffer converter = ByteBuffer.wrap(valueBuf);
78         converter.order(ByteOrder.nativeOrder());
79         return converter.getShort();
80     }
81 
byteArrayToInt(byte[] valueBuf, int offset)82     public static int byteArrayToInt(byte[] valueBuf, int offset) {
83         ByteBuffer converter = ByteBuffer.wrap(valueBuf);
84         converter.order(ByteOrder.nativeOrder());
85         return converter.getInt(offset);
86     }
87 
intToByteArray(int value)88     public static byte[] intToByteArray(int value) {
89         ByteBuffer converter = ByteBuffer.allocate(4);
90         converter.order(ByteOrder.nativeOrder());
91         converter.putInt(value);
92         return converter.array();
93     }
94 
uuidToByteArray(ParcelUuid pUuid)95     public static byte[] uuidToByteArray(ParcelUuid pUuid) {
96         int length = BD_UUID_LEN;
97         ByteBuffer converter = ByteBuffer.allocate(length);
98         converter.order(ByteOrder.BIG_ENDIAN);
99         long msb, lsb;
100         UUID uuid = pUuid.getUuid();
101         msb = uuid.getMostSignificantBits();
102         lsb = uuid.getLeastSignificantBits();
103         converter.putLong(msb);
104         converter.putLong(8, lsb);
105         return converter.array();
106     }
107 
uuidsToByteArray(ParcelUuid[] uuids)108     public static byte[] uuidsToByteArray(ParcelUuid[] uuids) {
109         int length = uuids.length * BD_UUID_LEN;
110         ByteBuffer converter = ByteBuffer.allocate(length);
111         converter.order(ByteOrder.BIG_ENDIAN);
112         UUID uuid;
113         long msb, lsb;
114         for (int i = 0; i < uuids.length; i++) {
115             uuid = uuids[i].getUuid();
116             msb = uuid.getMostSignificantBits();
117             lsb = uuid.getLeastSignificantBits();
118             converter.putLong(i * 16, msb);
119             converter.putLong(i * 16 + 8, lsb);
120         }
121         return converter.array();
122     }
123 
byteArrayToUuid(byte[] val)124     public static ParcelUuid[] byteArrayToUuid(byte[] val) {
125         int numUuids = val.length/BD_UUID_LEN;
126         ParcelUuid[] puuids = new ParcelUuid[numUuids];
127         UUID uuid;
128         int offset = 0;
129 
130         ByteBuffer converter = ByteBuffer.wrap(val);
131         converter.order(ByteOrder.BIG_ENDIAN);
132 
133         for (int i = 0; i < numUuids; i++) {
134             puuids[i] = new ParcelUuid(new UUID(converter.getLong(offset),
135                     converter.getLong(offset + 8)));
136             offset += 16;
137         }
138         return puuids;
139     }
140 
debugGetAdapterStateString(int state)141     public static String debugGetAdapterStateString(int state) {
142         switch (state) {
143             case BluetoothAdapter.STATE_OFF : return "STATE_OFF";
144             case BluetoothAdapter.STATE_ON : return "STATE_ON";
145             case BluetoothAdapter.STATE_TURNING_ON : return "STATE_TURNING_ON";
146             case BluetoothAdapter.STATE_TURNING_OFF : return "STATE_TURNING_OFF";
147             default : return "UNKNOWN";
148         }
149     }
150 
copyStream(InputStream is, OutputStream os, int bufferSize)151     public static void copyStream(InputStream is, OutputStream os, int bufferSize) throws IOException {
152         if (is != null && os!=null) {
153             byte[] buffer = new byte[bufferSize];
154             int bytesRead=0;
155             while ( (bytesRead = is.read(buffer))>=0) {
156                 os.write(buffer,0,bytesRead);
157             }
158         }
159     }
160 
safeCloseStream(InputStream is)161     public static void safeCloseStream(InputStream is) {
162         if (is != null) {
163             try {
164                 is.close();
165             } catch (Throwable t) {
166                 Log.d(TAG,"Error closing stream",t);
167             }
168         }
169     }
170 
safeCloseStream(OutputStream os)171     public static void safeCloseStream(OutputStream os) {
172         if (os != null) {
173             try {
174                 os.close();
175             } catch (Throwable t) {
176                 Log.d(TAG,"Error closing stream",t);
177             }
178         }
179     }
180 
checkCaller()181     public static boolean checkCaller() {
182         boolean ok;
183         // Get the caller's user id then clear the calling identity
184         // which will be restored in the finally clause.
185         int callingUser = UserHandle.getCallingUserId();
186         long ident = Binder.clearCallingIdentity();
187 
188         try {
189             // With calling identity cleared the current user is the foreground user.
190             int foregroundUser = ActivityManager.getCurrentUser();
191             ok = (foregroundUser == callingUser);
192         } catch (Exception ex) {
193             Log.e(TAG,"checkIfCallerIsSelfOrForegroundUser: Exception ex=" + ex);
194             ok = false;
195         } finally {
196             Binder.restoreCallingIdentity(ident);
197         }
198         return ok;
199     }
200 }
201