package com.dss.ups.sp51a.ble.manager;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import android.util.Log;
import com.dss.ups.sp51a.ble.data.ConnectState;
import com.dss.ups.sp51a.ble.utils.CharacteristicExtKt;
import g.a.a.a.a;
import io.netty.handler.codec.rtsp.RtspHeaders;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import kotlin.Metadata;
import kotlin.collections.j;
import kotlin.jvm.internal.o;

/* JADX INFO: loaded from: classes.dex */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000_\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\b\n\u0000\n\u0002\u0010\u000b\n\u0002\b\u0005\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0005\n\u0002\u0010\t\n\u0000\n\u0002\u0010\u0012\n\u0002\b\u000f\n\u0002\b\u0003\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0006*\u0001)\u0018\u0000 92\u00020\u0001:\u00019B\u000f\u0012\u0006\u00105\u001a\u000204¢\u0006\u0004\b7\u00108J'\u0010\b\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0005\u001a\u00020\u00042\u0006\u0010\u0007\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\b\u0010\tJ\u0017\u0010\n\u001a\u00020\u00062\u0006\u0010\u0007\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\n\u0010\u000bJ\u0019\u0010\u000f\u001a\u0004\u0018\u00010\u000e2\u0006\u0010\r\u001a\u00020\fH\u0002¢\u0006\u0004\b\u000f\u0010\u0010J!\u0010\u0013\u001a\u0004\u0018\u00010\u000e2\u0006\u0010\u0012\u001a\u00020\u00112\u0006\u0010\r\u001a\u00020\fH\u0002¢\u0006\u0004\b\u0013\u0010\u0014J\u0019\u0010\u000f\u001a\u0004\u0018\u00010\u000e2\u0006\u0010\r\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\u000f\u0010\u0015J!\u0010\u001a\u001a\u0004\u0018\u00010\u00192\u0006\u0010\u0016\u001a\u00020\u000e2\u0006\u0010\u0018\u001a\u00020\u0017H\u0002¢\u0006\u0004\b\u001a\u0010\u001bJ\u001f\u0010\u001c\u001a\u00020\u00062\u0006\u0010\u0016\u001a\u00020\u000e2\u0006\u0010\u0018\u001a\u00020\u0017H\u0002¢\u0006\u0004\b\u001c\u0010\u001dJ\u000f\u0010\u001e\u001a\u00020\u0006H\u0002¢\u0006\u0004\b\u001e\u0010\u001fJ\u000f\u0010 \u001a\u00020\u0006H\u0002¢\u0006\u0004\b \u0010\u001fJ\u000f\u0010!\u001a\u00020\u0006H\u0002¢\u0006\u0004\b!\u0010\u001fJ\u0017\u0010\"\u001a\u00020\f2\u0006\u0010\r\u001a\u00020\u0004H\u0002¢\u0006\u0004\b\"\u0010#J\u001d\u0010$\u001a\u00020\u00062\u0006\u0010\u0003\u001a\u00020\u00022\u0006\u0010\u0007\u001a\u00020\u0006¢\u0006\u0004\b$\u0010%J\r\u0010&\u001a\u00020\u0006¢\u0006\u0004\b&\u0010\u001fJ\u0017\u0010\u001a\u001a\u0004\u0018\u00010\u00192\u0006\u0010\r\u001a\u00020\f¢\u0006\u0004\b\u001a\u0010'R\u0016\u0010\u0005\u001a\u00020\u00048\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u0005\u0010(R\u0016\u0010*\u001a\u00020)8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b*\u0010+R\u0016\u0010-\u001a\u00020,8\u0002@\u0002X\u0082\u0004¢\u0006\u0006\n\u0004\b-\u0010.R\u0018\u0010/\u001a\u0004\u0018\u00010\u00118\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b/\u00100R\u0016\u00102\u001a\u0002018\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b2\u00103R\u0016\u00105\u001a\u0002048\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b5\u00106¨\u0006:"}, d2 = {"Lcom/dss/ups/sp51a/ble/manager/BleDeviceWrapper;", "", "Landroid/content/Context;", "context", "", "mtu", "", "sync", "connectGatt", "(Landroid/content/Context;IZ)Z", "waitForConnectResult", "(Z)Z", "", "uuid", "Landroid/bluetooth/BluetoothGattCharacteristic;", "getCharacteristic", "(Ljava/lang/String;)Landroid/bluetooth/BluetoothGattCharacteristic;", "Landroid/bluetooth/BluetoothGatt;", "gatt", "findCharacteristic", "(Landroid/bluetooth/BluetoothGatt;Ljava/lang/String;)Landroid/bluetooth/BluetoothGattCharacteristic;", "(I)Landroid/bluetooth/BluetoothGattCharacteristic;", "characteristic", "", RtspHeaders.Values.TIMEOUT, "", "readCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;J)[B", "writeCharacteristic", "(Landroid/bluetooth/BluetoothGattCharacteristic;J)Z", "isConnected", "()Z", "isConnecting", "pingGatt", "getFullUuid", "(I)Ljava/lang/String;", "connect", "(Landroid/content/Context;Z)Z", "disconnect", "(Ljava/lang/String;)[B", "I", "com/dss/ups/sp51a/ble/manager/BleDeviceWrapper$gattCallback$1", "gattCallback", "Lcom/dss/ups/sp51a/ble/manager/BleDeviceWrapper$gattCallback$1;", "Lcom/dss/ups/sp51a/ble/manager/GattLock;", "lock", "Lcom/dss/ups/sp51a/ble/manager/GattLock;", "bluetoothGatt", "Landroid/bluetooth/BluetoothGatt;", "Lcom/dss/ups/sp51a/ble/data/ConnectState;", "connectState", "Lcom/dss/ups/sp51a/ble/data/ConnectState;", "Landroid/bluetooth/BluetoothDevice;", "device", "Landroid/bluetooth/BluetoothDevice;", "<init>", "(Landroid/bluetooth/BluetoothDevice;)V", "Companion", "lib_release"}, k = 1, mv = {1, 5, 1})
public final class BleDeviceWrapper {
    private static final String TAG = "BleDeviceWrapper";
    private static final String UUID_DESCRIPTOR_CLIENT_CHARACTERISTIC_CONFIGURATION = "00002902-0000-1000-8000-00805f9b34fb";
    private BluetoothGatt bluetoothGatt;
    private ConnectState connectState;
    private BluetoothDevice device;
    private final BleDeviceWrapper$gattCallback$1 gattCallback;
    private final GattLock lock;
    private int mtu;

    /* JADX WARN: Type inference failed for: r2v4, types: [com.dss.ups.sp51a.ble.manager.BleDeviceWrapper$gattCallback$1] */
    public BleDeviceWrapper(BluetoothDevice bluetoothDevice) {
        o.f(bluetoothDevice, "device");
        this.device = bluetoothDevice;
        this.mtu = 256;
        this.connectState = ConnectState.DISCONNECTED;
        this.lock = new GattLock();
        this.gattCallback = new BluetoothGattCallback() { // from class: com.dss.ups.sp51a.ble.manager.BleDeviceWrapper$gattCallback$1
            @Override // android.bluetooth.BluetoothGattCallback
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicRead(gatt, characteristic, status);
                if (characteristic == null) {
                    return;
                }
                Log.d("BleDeviceWrapper", o.l("onCharacteristicRead : ", characteristic.getUuid()));
                this.this$0.lock.notifyReadLock(characteristic.getValue());
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
                super.onCharacteristicWrite(gatt, characteristic, status);
                if (characteristic == null) {
                    return;
                }
                Log.d("BleDeviceWrapper", o.l("onCharacteristicWrite : ", characteristic.getUuid()));
                this.this$0.lock.notifyWriteLock(status == 0);
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                if (gatt == null) {
                    return;
                }
                StringBuilder sbF = a.F("onConnectionStateChange ");
                BluetoothDevice device = gatt.getDevice();
                sbF.append((Object) (device == null ? null : device.getName()));
                sbF.append(", ");
                sbF.append(gatt.getDevice());
                sbF.append(" : ");
                a.p0(sbF, newState, "BleDeviceWrapper");
                ConnectState connectState = this.this$0.connectState;
                ConnectState connectState2 = ConnectState.CONNECTING;
                boolean z2 = connectState == connectState2;
                BleDeviceWrapper bleDeviceWrapper = this.this$0;
                if (newState == 0) {
                    connectState2 = ConnectState.DISCONNECTED;
                } else if (newState != 1) {
                    if (newState == 2) {
                        connectState2 = ConnectState.DISCOVERING;
                    } else if (newState != 3) {
                        return;
                    } else {
                        connectState2 = ConnectState.DISCONNECTING;
                    }
                }
                bleDeviceWrapper.connectState = connectState2;
                if (this.this$0.connectState == ConnectState.DISCOVERING) {
                    Log.i("BleDeviceWrapper", "设备连接上 开始扫描服务");
                    gatt.discoverServices();
                }
                if (this.this$0.connectState == ConnectState.DISCONNECTED) {
                    Log.e("BleDeviceWrapper", "连接断开 连接断开后的相应处理");
                    gatt.close();
                    this.this$0.bluetoothGatt = null;
                }
                if (status == 0 || !z2) {
                    return;
                }
                this.this$0.lock.notifyConnect(null);
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorRead(gatt, descriptor, status);
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
                super.onDescriptorWrite(gatt, descriptor, status);
                if (descriptor == null) {
                    return;
                }
                StringBuilder sbF = a.F("onDescriptorWrite : ");
                sbF.append(descriptor.getCharacteristic().getUuid());
                sbF.append(' ');
                sbF.append(descriptor.getUuid());
                sbF.append(' ');
                sbF.append(status);
                Log.d("BleDeviceWrapper", sbF.toString());
                this.this$0.lock.notifyWriteLock(status == 0);
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
                super.onMtuChanged(gatt, mtu, status);
                Log.e("BleDeviceWrapper", "onMtuChanged...");
            }

            @Override // android.bluetooth.BluetoothGattCallback
            public void onServicesDiscovered(BluetoothGatt gatt, int status) {
                super.onServicesDiscovered(gatt, status);
                if (gatt == null) {
                    return;
                }
                BluetoothDevice device = gatt.getDevice();
                Log.d("BleDeviceWrapper", o.l("onServicesDiscovered ", device == null ? null : device.getName()));
                this.this$0.bluetoothGatt = gatt;
                BluetoothGatt bluetoothGatt = this.this$0.bluetoothGatt;
                o.c(bluetoothGatt);
                o.e(bluetoothGatt.getServices(), "bluetoothGatt!!.services");
                this.this$0.connectState = ConnectState.CONNECTED;
                this.this$0.lock.notifyConnect(gatt);
            }
        };
    }

    private final boolean connectGatt(Context context, int mtu, boolean sync) {
        this.mtu = mtu;
        this.bluetoothGatt = null;
        this.connectState = ConnectState.CONNECTING;
        if (!sync) {
            this.bluetoothGatt = this.device.connectGatt(context, false, this.gattCallback);
            return true;
        }
        BluetoothGatt bluetoothGattWaitForConnect$default = GattLock.waitForConnect$default(this.lock, context, this.device, this.gattCallback, 0L, 8, null);
        this.bluetoothGatt = bluetoothGattWaitForConnect$default;
        return bluetoothGattWaitForConnect$default != null;
    }

    private final BluetoothGattCharacteristic findCharacteristic(BluetoothGatt gatt, String uuid) {
        List<BluetoothGattService> services = gatt.getServices();
        o.e(services, "gatt.services");
        Iterator<T> it = services.iterator();
        while (it.hasNext()) {
            List<BluetoothGattCharacteristic> characteristics = ((BluetoothGattService) it.next()).getCharacteristics();
            o.e(characteristics, "s.characteristics");
            for (BluetoothGattCharacteristic bluetoothGattCharacteristic : characteristics) {
                if (o.a(bluetoothGattCharacteristic.getUuid().toString(), uuid)) {
                    return bluetoothGattCharacteristic;
                }
            }
        }
        return null;
    }

    private final BluetoothGattCharacteristic getCharacteristic(int uuid) {
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return null;
        }
        return findCharacteristic(bluetoothGatt, getFullUuid(uuid));
    }

    private final BluetoothGattCharacteristic getCharacteristic(String uuid) {
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return null;
        }
        return findCharacteristic(bluetoothGatt, uuid);
    }

    private final String getFullUuid(int uuid) {
        String str = String.format("%08x", Arrays.copyOf(new Object[]{Integer.valueOf(uuid)}, 1));
        o.e(str, "java.lang.String.format(format, *args)");
        return o.l(str, "-0000-1000-8000-00805f9b34fb");
    }

    private final boolean isConnected() {
        return this.connectState == ConnectState.CONNECTED;
    }

    private final boolean isConnecting() {
        return !j.G(ConnectState.DISCONNECTED, ConnectState.CONNECTED).contains(this.connectState);
    }

    private final boolean pingGatt() {
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return false;
        }
        return bluetoothGatt.readRemoteRssi();
    }

    private final byte[] readCharacteristic(BluetoothGattCharacteristic characteristic, long timeout) {
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return null;
        }
        if (isConnected()) {
            return this.lock.waitForReadCharacteristic(bluetoothGatt, characteristic, timeout);
        }
        StringBuilder sbF = a.F("device not connect:");
        sbF.append(this.connectState.ordinal());
        sbF.append(' ');
        Log.e(TAG, sbF.toString());
        return null;
    }

    private final boolean waitForConnectResult(boolean sync) {
        if (!sync) {
            return true;
        }
        BluetoothGatt bluetoothGattWaitForConnectResult$default = GattLock.waitForConnectResult$default(this.lock, 0L, 1, null);
        this.bluetoothGatt = bluetoothGattWaitForConnectResult$default;
        return bluetoothGattWaitForConnectResult$default != null;
    }

    private final boolean writeCharacteristic(BluetoothGattCharacteristic characteristic, long timeout) {
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return false;
        }
        if (isConnected()) {
            return this.lock.waitForWriteCharacteristic(bluetoothGatt, characteristic, timeout);
        }
        StringBuilder sbF = a.F("device not connect:");
        sbF.append(this.connectState.ordinal());
        sbF.append(' ');
        Log.d(TAG, sbF.toString());
        return false;
    }

    public final synchronized boolean connect(Context context, boolean sync) {
        boolean zPingGatt;
        o.f(context, "context");
        zPingGatt = pingGatt();
        Log.d(TAG, "connect " + ((Object) this.device.getName()) + ", " + this.device + ", connectState: " + this.connectState + ", pingGatt: " + zPingGatt);
        return (zPingGatt && isConnected()) ? true : isConnecting() ? waitForConnectResult(sync) : connectGatt(context, this.mtu, sync);
    }

    public final synchronized boolean disconnect() {
        Log.d(TAG, "disconnect " + ((Object) this.device.getName()) + ", connectState: " + this.connectState + ", gatt: " + this.bluetoothGatt);
        BluetoothGatt bluetoothGatt = this.bluetoothGatt;
        if (bluetoothGatt == null) {
            return true;
        }
        o.c(bluetoothGatt);
        bluetoothGatt.disconnect();
        BluetoothGatt bluetoothGatt2 = this.bluetoothGatt;
        o.c(bluetoothGatt2);
        bluetoothGatt2.close();
        this.bluetoothGatt = null;
        this.connectState = ConnectState.DISCONNECTED;
        return true;
    }

    public final synchronized byte[] readCharacteristic(String uuid) {
        o.f(uuid, "uuid");
        if (!isConnected()) {
            Log.e(TAG, "readCharacteristic: device not connect");
            return null;
        }
        BluetoothGattCharacteristic characteristic = getCharacteristic(uuid);
        o.c(characteristic);
        if (!CharacteristicExtKt.isReadable(characteristic)) {
            Log.e(TAG, "characteristic is no readable");
            return null;
        }
        byte[] characteristic2 = readCharacteristic(characteristic, 3000L);
        if (characteristic2 != null) {
            return characteristic2;
        }
        Log.e(TAG, "readCharacteristic fail");
        return null;
    }
}
