package a.b.a.f;

import a.a.a.b;
import a.c.d.h.a;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;

public class c {

    /* renamed from: a  reason: collision with root package name */
    private static final String f12a = "BleAssist";

    public static String a(byte[] bArr) {
        StringBuilder sb = new StringBuilder();
        if (bArr != null) {
            int length = bArr.length;
            for (int i = 0; i < length; i++) {
                sb.append(String.format("%02X ", Byte.valueOf(bArr[i])));
            }
        }
        return sb.toString();
    }

    public static void a(BluetoothGatt bluetoothGatt, String str) {
        BluetoothDevice device;
        if (bluetoothGatt != null && (device = bluetoothGatt.getDevice()) != null) {
            b.c(f12a, str + ": Connected to GATT server. address : " + device.getAddress());
            b.c(f12a, str + ": Connected to GATT server. name : " + device.getName());
        }
    }

    public static void a(BluetoothGattCharacteristic bluetoothGattCharacteristic, String str) {
        b.c(f12a, str + " BluetoothGattCharacteristic begin");
        if (bluetoothGattCharacteristic != null) {
            b.c(f12a, str + ": uuid " + h.a(bluetoothGattCharacteristic.getUuid().toString()));
            b.c(f12a, str + ": properties " + bluetoothGattCharacteristic.getProperties());
            b.c(f12a, str + ": permissions " + bluetoothGattCharacteristic.getPermissions());
            byte[] value = bluetoothGattCharacteristic.getValue();
            if (value != null && value.length > 0) {
                StringBuilder sb = new StringBuilder(value.length);
                int length = value.length;
                for (int i = 0; i < length; i++) {
                    sb.append(String.format("%02X ", Byte.valueOf(value[i])));
                }
                try {
                    b.c(f12a, str + ": value " + new String(value, a.f133a) + "\n" + sb.toString());
                } catch (UnsupportedEncodingException e) {
                    b.b(f12a, "printCharacteristic UnsupportedEncodingException", e);
                }
            }
            b.c(f12a, str + ": descriptors size " + bluetoothGattCharacteristic.getDescriptors().size());
        }
        b.c(f12a, str + " BluetoothGattCharacteristic end");
    }

    public static void a(BluetoothGattService bluetoothGattService, String str) {
        b.c(f12a, str + " BluetoothGattService begin");
        b.c(f12a, str + " Service uuid " + h.b(bluetoothGattService.getUuid().toString()));
        List<BluetoothGattCharacteristic> characteristics = bluetoothGattService.getCharacteristics();
        if (characteristics != null && characteristics.size() > 0) {
            for (BluetoothGattCharacteristic bluetoothGattCharacteristic : characteristics) {
                a(bluetoothGattCharacteristic, str);
            }
        }
        b.c(f12a, str + " BluetoothGattService end");
    }

    public static void a(List<BluetoothGattService> list, String str) {
        b.c(f12a, str + " BluetoothGatt begin");
        if (list != null && list.size() > 0) {
            for (BluetoothGattService bluetoothGattService : list) {
                a(bluetoothGattService, str);
            }
        }
        b.c(f12a, str + " BluetoothGatt end");
    }

    public static byte[] a(String str) {
        return str == null ? new byte[0] : str.getBytes(Charset.forName(a.f133a));
    }

    public static byte[] a(String str, int i) {
        byte[] bytes;
        while (true) {
            bytes = str.getBytes(Charset.forName(a.f133a));
            if (bytes.length <= i) {
                break;
            }
            str = str.substring(0, str.length() - 1);
        }
        byte[] bArr = new byte[i];
        for (int i2 = 0; i2 < i; i2++) {
            if (i2 < bytes.length) {
                bArr[i2] = bytes[i2];
            } else {
                bArr[i2] = 0;
            }
        }
        return bArr;
    }

    public static String b(byte[] bArr) {
        if (bArr != null) {
            return new String(bArr, Charset.forName(a.f133a));
        }
        b.e(f12a, "Can't convert a null byte[] to string.");
        return "";
    }

    public static byte[] c(byte[] bArr) {
        int length = bArr.length - 1;
        while (length >= 0 && bArr[length] == 0) {
            length--;
        }
        int i = length + 1;
        byte[] bArr2 = new byte[i];
        System.arraycopy(bArr, 0, bArr2, 0, i);
        return bArr2;
    }
}
