package com.epson.mobilephone.common.ble;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
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.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Handler;

import com.epson.mobilephone.common.ble.util.BLEUtility;
import com.epson.mobilephone.common.ble.util.BLEUuid;
import com.epson.mobilephone.common.ble.util.ScannedDevice;
import com.epson.mobilephone.common.escpr.EscprLib;
import com.epson.mobilephone.common.escpr.MIBCommand;
import com.epson.mobilephone.common.wifidirect.ActivityRequestLocationPermission;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import com.epson.mobilephone.common.wifidirect.WiFiNetworkManager;
import com.evernote.edam.limits.Constants;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import epson.print.IprintApplication;
import epson.print.MyPrinter;

public class BleWork {
    private static final int EPS_COMM_BID = 2;
    private static final int GATT_DELAY = 0;
    private static final int GATT_DELAY2 = 500;
    private static final int RETRY_NUMS = 3;
    private static final int SCAN_ADVERTISING_PACKET_TIMEOUT = 10000;
    Handler handlerRead;
    Handler handlerWrite;
    private BluetoothAdapter mBTAdapter;
    /* access modifiers changed from: private */
    @Nullable
    public BluetoothGatt mConnGatt;
    private Context mContext;
    private BluetoothDevice mDevice;
    private String mDeviceMacAddress;
    private String mDevicePassword;
    private final byte[] mEngineID;
    private EscprLib mEscprlib;
    /* access modifiers changed from: private */
    @Nullable
    public BLEUtility.BleWorkCallback mFailed;
    private BluetoothGattCallback mGattcallback;
    private boolean mInSequence;
    private byte mIndex;
    /* access modifiers changed from: private */
    @NonNull
    public MIBCommand mMibCommand;
    private boolean mReconnect;
    /* access modifiers changed from: private */
    @Nullable
    public BLEUtility.BleWorkCallback mResposne;
    private int mRetry;
    private String mSSID;
    private String mSSIDPassword;
    private short mSecurityType;
    private ScannedDevice mSelectItem;
    public jobSequence mSequence;
    @Nullable
    private BLEUtility.BleWorkCallback mSequenceCallbackFailed;
    /* access modifiers changed from: private */
    @Nullable
    public BLEUtility.BleWorkCallback mSequenceCallbackSuccess;
    /* access modifiers changed from: private */
    @NonNull
    public List<String> mSsidLis;
    public int mStatus;
    /* access modifiers changed from: private */
    @Nullable
    public BLEUtility.BleWorkCallback mSuccess;
    private long mTimeOut;

    public enum jobSequence {
        UNINITIALIZED,
        DISCONNECTED,
        FAILED,
        INITIALIZE,
        GET_VERSION,
        CHECK_SSID,
        SET_PASSWORD,
        MIB_GET_MACADDRESS,
        MIB_GET_SSID_LIST,
        MIB_SET_SSID,
        MIB_SET_SECURITY_TYPE,
        MIB_SEC_LOGIN,
        MIB_SEC_SET_PASS,
        MIB_SEC_LOGOUT,
        MIB_REBOOT_NW,
        COMPLETION,
        MIB_SEC_SKIP,
        AFTER_SETTING_PASSWORD,
        BEFORE_CHECK_SSID
    }

    public static int getScanAdvertisingPacketTimeout() {
        return 10000;
    }

    private void showStatus(String str, int i) {
    }

    static /* synthetic */ int access$508(BleWork bleWork) {
        int i = bleWork.mRetry;
        bleWork.mRetry = i + 1;
        return i;
    }

    @NonNull
    public List<String> getSsidLis() {
        return mSsidLis;
    }

    public void setSSID(String str) {
        mSSID = str;
    }

    public void setRetryFinish() {
        mRetry = 3;
    }

    public String getSSID() {
        return mSSID;
    }

    public synchronized void setSecurityType(short s) {
        mSecurityType = s;
    }

    public synchronized short getSecurityType() {
        return mSecurityType;
    }

    public void setSSIDPassword(String str) {
        mSSIDPassword = str;
    }

    public String getSSIDPassword() {
        String str = mSSIDPassword;
        return str != null ? str : "";
    }

    public void setTimeOut(long j) {
        mTimeOut = j;
    }

    public String getDeviceMacAddress() {
        String str = mDeviceMacAddress;
        if (str == null) {
            return "";
        }
        return str.replace("\"", "");
    }

    public String getDeviceName() {
        return mSelectItem.getDisplayName();
    }

    private BleWork() {
        mSuccess = null;
        mFailed = null;
        mSequenceCallbackSuccess = null;
        mSequenceCallbackFailed = null;
        mResposne = null;
        mSsidLis = new ArrayList();
        mTimeOut = -1;
        mEscprlib = EscprLib.getInstance();
        mMibCommand = new MIBCommand();
        mIndex = 0;
        mInSequence = false;
        mEngineID = mEscprlib.getEngineId(IprintApplication.getInstance());
        mReconnect = false;
        mSequence = jobSequence.UNINITIALIZED;
        mGattcallback = null;
        if (mGattcallback != null) {
            EPLog.e("mGattcallback != null");
        }
        mGattcallback = new BluetoothGattCallback() {
            public void onMtuChanged(BluetoothGatt bluetoothGatt, int i, int i2) {
                super.onMtuChanged(bluetoothGatt, i, i2);
                EPLog.i("onMtuChanged " + i);
            }

            public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
                super.onDescriptorWrite(bluetoothGatt, bluetoothGattDescriptor, i);
                showStatus("DescriptorWrite", i);
                if (i != 0) {
                    sequenceError();
                } else if (BleWork.mResposne != null) {
                    BleWork.mResposne.call(bluetoothGattDescriptor);
                }
            }

            public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
                EPLog.i("status = " + i + "  newState = " + i2);
                if (i == 0 && i2 == 2) {
                    EPLog.i("STATE_CONNECTED  -- " + BleWork.mSequence);
                    BleWork bleWork = BleWork.this;
                    bleWork.mStatus = i2;
                    if (bleWork.mConnGatt == null) {
                        EPLog.e("mConnGatt  NULL !");
                    }
                    if (bluetoothGatt == null) {
                        EPLog.e("gatt  NULL !");
                    }
                    BluetoothGatt unused = BleWork.mConnGatt = bluetoothGatt;
                    handlerWrite.removeCallbacks((Runnable) null);
                    handlerWrite.postDelayed(new Runnable() {
                        public void run() {
                            EPLog.i("discoverServices");
                            try {
                                if (!((BluetoothGatt) Objects.requireNonNull(BleWork.mConnGatt)).discoverServices()) {
                                    EPLog.e("discoverServices error");
                                }
                            } catch (NullPointerException unused) {
                                EPLog.e("mConnGatt NULL !");
                            }
                        }
                    }, 500);
                } else if (i != 0 || i2 == 0) {
                    EPLog.i("STATE_DISCONNECTED　" + BleWork.mSequence + " mReconnect = " + BleWork.mReconnect + " mRetry  " + BleWork.mRetry);
                    if (!BleWork.mReconnect && BleWork.mSequence == jobSequence.UNINITIALIZED) {
                        failedProcessing();
                    } else if (BleWork.mReconnect || BleWork.mSequence == jobSequence.UNINITIALIZED || BleWork.mSequence == jobSequence.MIB_REBOOT_NW || BleWork.mSequence == jobSequence.DISCONNECTED || BleWork.mSequence == jobSequence.MIB_SET_SSID || BleWork.mSequence == jobSequence.AFTER_SETTING_PASSWORD) {
                        EPLog.d("★ stopScan");
                        BleScanWork.getInstace().stopScan();
                        BleWork bleWork2 = BleWork.this;
                        bleWork2.mStatus = i2;
                        if ((bleWork2.mSequence == jobSequence.AFTER_SETTING_PASSWORD || BleWork.mSequence == jobSequence.BEFORE_CHECK_SSID || BleWork.mSequence == jobSequence.MIB_SET_SSID || BleWork.mSequence == jobSequence.MIB_REBOOT_NW) && !BleWork.mReconnect) {
                            if (BleWork.mSequence == jobSequence.MIB_REBOOT_NW) {
                                gattClose();
                            }
                            EPLog.i(" BREAK ★ mSequence " + BleWork.mSequence);
                            return;
                        }
                        gattClose();
                        if (BleWork.mSequence != jobSequence.UNINITIALIZED && !BleWork.mReconnect) {
                            return;
                        }
                        if (BleWork.mRetry < 3) {
                            try {
                                TimeUnit.SECONDS.sleep(5);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            EPLog.m82w("retry " + BleWork.mRetry);
                            BleWork.access$508(BleWork.this);
                            EPLog.e("init");
                            boolean unused2 = init();
                            return;
                        }
                        failedProcessing();
                    } else {
                        failedProcessing();
                    }
                } else if (i2 == 1) {
                    EPLog.i("STATE_CONNECTING");
                } else if (i2 == 3) {
                    EPLog.i("STATE_DISCONNECTING");
                }
            }

            public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
                EPLog.i();
                try {
                    for (BluetoothGattService next : bluetoothGatt.getServices()) {
                        if (next != null) {
                            if (next.getUuid() != null) {
                                if (BLEUuid.SERVICE_SNMP.equalsIgnoreCase(next.getUuid().toString())) {
                                    EPLog.i("発見!! EPSON SNMP サービス");
                                    for (Map.Entry next2 : BLEUuid.GattCharsMap.entrySet()) {
                                        next2.setValue(next.getCharacteristic(UUID.fromString((String) next2.getKey())));
                                    }
                                    if (!BLEUuid.GattCharsMap.containsValue((Object) null)) {
                                        boolean unused = BleWork.mReconnect = false;
                                        BleWork.mSequenceCallbackSuccess.call(BleWork.mSequence);
                                        new NotifySetThread().execute(new Void[0]);
                                    } else {
                                        throw new BleException(1, "getCharacteristic Error !");
                                    }
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                } catch (BleException e) {
                    EPLog.e(e.getMessage());
                    e.printStackTrace();
                    failedProcessing();
                }
            }

            public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, @NonNull BluetoothGattCharacteristic bluetoothGattCharacteristic) {
                super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
                EPLog.d("onCharacteristicChanged");
                if (BLEUuid.CHAR_SNMP_STATUS_STRING.equals(bluetoothGattCharacteristic.getUuid().toString())) {
                    Short valueOf = Short.valueOf(bluetoothGattCharacteristic.getIntValue(17, 0).shortValue());
                    String sh = valueOf.toString();
                    if (valueOf.shortValue() == 0) {
                        EPLog.i("[Notify]SNMP Status" + sh);
                        characteristicRead();
                        return;
                    }
                    EPLog.e("[Notify]SNMP Status" + sh);
                    failedProcessing();
                } else if (BLEUuid.CHAR_SECURITY_TYPE_STRING.equals(bluetoothGattCharacteristic.getUuid().toString())) {
                    Short valueOf2 = Short.valueOf(bluetoothGattCharacteristic.getIntValue(17, 0).shortValue());
                    setSecurityType(valueOf2.shortValue());
                    String sh2 = valueOf2.toString();
                    EPLog.i("[Notify]Security Type　" + sh2);
                    if (valueOf2.shortValue() <= 0) {
                        BleWork.mSsidLis.clear();
                        getSSIDList();
                    } else if (BleWork.mSequenceCallbackSuccess != null) {
                        BleWork.mSequenceCallbackSuccess.call(BleWork.mSequence);
                    }
                } else {
                    String sh3 = Short.valueOf(bluetoothGattCharacteristic.getIntValue(17, 0).shortValue()).toString();
                    EPLog.e("[Notify]unKnown type" + sh3);
                }
            }

            public void onCharacteristicRead(BluetoothGatt bluetoothGatt, @NonNull BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
                showStatus("Read", i);
                if (i == 0) {
                    readCharacteristicData(bluetoothGattCharacteristic);
                } else {
                    byte unused = BleWork.mIndex = (byte) 0;
                }
            }

            /* JADX WARNING: Can't fix incorrect switch cases order */
            /* Code decompiled incorrectly, please refer to instructions dump. */
            private void readCharacteristicData(@android.support.annotation.NonNull android.bluetooth.BluetoothGattCharacteristic r7) {
                /*
                    r6 = this;
                    java.util.UUID r0 = r7.getUuid()
                    java.lang.String r0 = r0.toString()
                    if (r0 != 0) goto L_0x0011
                    java.lang.String r7 = "characteristic NULL !!"
                    com.epson.mobilephone.common.EPLog.e((java.lang.String) r7)
                    goto L_0x03a1
                L_0x0011:
                    java.lang.String r0 = r0.toLowerCase()
                    int r1 = r0.hashCode()
                    r2 = -1
                    r3 = 1
                    r4 = 0
                    switch(r1) {
                        case -929193779: goto L_0x0048;
                        case -881344628: goto L_0x003e;
                        case 593366729: goto L_0x0034;
                        case 641215880: goto L_0x002a;
                        case 1286468426: goto L_0x0020;
                        default: goto L_0x001f;
                    }
                L_0x001f:
                    goto L_0x0052
                L_0x0020:
                    java.lang.String r1 = "802a0002-4ef4-4e59-b573-2bed4a4ac158"
                    boolean r0 = r0.equals(r1)
                    if (r0 == 0) goto L_0x0052
                    r0 = 3
                    goto L_0x0053
                L_0x002a:
                    java.lang.String r1 = "00002a25-0000-1000-8000-00805f9b34fb"
                    boolean r0 = r0.equals(r1)
                    if (r0 == 0) goto L_0x0052
                    r0 = 1
                    goto L_0x0053
                L_0x0034:
                    java.lang.String r1 = "802a0001-4ef4-4e59-b573-2bed4a4ac158"
                    boolean r0 = r0.equals(r1)
                    if (r0 == 0) goto L_0x0052
                    r0 = 2
                    goto L_0x0053
                L_0x003e:
                    java.lang.String r1 = "00002a29-0000-1000-8000-00805f9b34fb"
                    boolean r0 = r0.equals(r1)
                    if (r0 == 0) goto L_0x0052
                    r0 = 0
                    goto L_0x0053
                L_0x0048:
                    java.lang.String r1 = "802a0005-4ef4-4e59-b573-2bed4a4ac158"
                    boolean r0 = r0.equals(r1)
                    if (r0 == 0) goto L_0x0052
                    r0 = 4
                    goto L_0x0053
                L_0x0052:
                    r0 = -1
                L_0x0053:
                    r1 = 18
                    switch(r0) {
                        case 0: goto L_0x0389;
                        case 1: goto L_0x0370;
                        case 2: goto L_0x032e;
                        case 3: goto L_0x02bd;
                        case 4: goto L_0x005a;
                        default: goto L_0x0058;
                    }
                L_0x0058:
                    goto L_0x03a1
                L_0x005a:
                    byte[] r7 = r7.getValue()
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    com.epson.mobilephone.common.escpr.MIBDataBlock r0 = r0.commandBlock
                    int r1 = r7.length
                    r0.bufSize = r1
                    r0 = 0
                L_0x006a:
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r1 = r1.mMibCommand
                    com.epson.mobilephone.common.escpr.MIBDataBlock r1 = r1.commandBlock
                    int r1 = r1.bufSize
                    if (r0 >= r1) goto L_0x0087
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r1 = r1.mMibCommand
                    com.epson.mobilephone.common.escpr.MIBDataBlock r1 = r1.commandBlock
                    byte[] r1 = r1.buf
                    byte r5 = r7[r0]
                    r1[r0] = r5
                    int r0 = r0 + 1
                    goto L_0x006a
                L_0x0087:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    boolean r7 = r7.mInSequence
                    if (r7 == 0) goto L_0x00e7
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.EscprLib r7 = r7.mEscprlib
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    java.lang.String r7 = r7.mibParseResponseAsStr(r0)
                    boolean r0 = r7.isEmpty()
                    if (r0 == 0) goto L_0x00c3
                    java.lang.String r7 = "[Read]SNMP Response :end!"
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    boolean unused = r7.mInSequence = r4
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    byte unused = r7.mIndex = r4
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.util.BLEUtility$BleWorkCallback r7 = r7.mSequenceCallbackSuccess
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = r0.mSequence
                    r7.call(r0)
                    goto L_0x03a1
                L_0x00c3:
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]SNMP Response :"
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r0 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r0)
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    java.util.List r0 = r0.mSsidLis
                    r0.add(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.getSSIDList()
                    goto L_0x03a1
                L_0x00e7:
                    java.lang.StringBuilder r7 = new java.lang.StringBuilder
                    r7.<init>()
                    java.lang.String r0 = "mSequence   "
                    r7.append(r0)
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = r0.mSequence
                    r7.append(r0)
                    java.lang.String r7 = r7.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    int[] r7 = com.epson.mobilephone.common.ble.BleWork.C10195.$SwitchMap$com$epson$mobilephone$common$ble$BleWork$jobSequence
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = r0.mSequence
                    int r0 = r0.ordinal()
                    r7 = r7[r0]
                    switch(r7) {
                        case 1: goto L_0x026e;
                        case 2: goto L_0x0226;
                        case 3: goto L_0x019f;
                        case 4: goto L_0x019f;
                        case 5: goto L_0x019f;
                        case 6: goto L_0x0110;
                        case 7: goto L_0x0110;
                        case 8: goto L_0x0110;
                        default: goto L_0x010e;
                    }
                L_0x010e:
                    goto L_0x03a1
                L_0x0110:
                    int[] r7 = new int[r3]
                    r7[r4] = r2
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.EscprLib r0 = r0.mEscprlib
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r1 = r1.mMibCommand
                    int r0 = r0.mibParseResponseAsInt(r1, r7)
                    if (r0 != 0) goto L_0x0198
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]SNMP Response :sucess mSequence   "
                    r0.append(r1)
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r1 = r1.mSequence
                    r0.append(r1)
                    java.lang.String r1 = " val:"
                    r0.append(r1)
                    r7 = r7[r4]
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = com.epson.mobilephone.common.ble.BleWork.jobSequence.MIB_SEC_SKIP
                    if (r7 != r0) goto L_0x0157
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = com.epson.mobilephone.common.ble.BleWork.jobSequence.MIB_SEC_LOGOUT
                    r7.mSequence = r0
                    goto L_0x0166
                L_0x0157:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = com.epson.mobilephone.common.ble.BleWork.jobSequence.MIB_REBOOT_NW
                    if (r7 != r0) goto L_0x0166
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    boolean unused = r7.mReconnect = r4
                    goto L_0x03a1
                L_0x0166:
                    r6.IncrementSequence()
                    java.lang.StringBuilder r7 = new java.lang.StringBuilder
                    r7.<init>()
                    java.lang.String r0 = "mSequence   "
                    r7.append(r0)
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = r0.mSequence
                    r7.append(r0)
                    java.lang.String r7 = r7.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    int r7 = r7.ordinal()
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = com.epson.mobilephone.common.ble.BleWork.jobSequence.MIB_REBOOT_NW
                    int r0 = r0.ordinal()
                    if (r7 > r0) goto L_0x03a1
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.setSSIDWork()
                    goto L_0x03a1
                L_0x0198:
                    java.lang.String r7 = "[Read]SNMP Response :error "
                    com.epson.mobilephone.common.EPLog.e((java.lang.String) r7)
                    goto L_0x03a1
                L_0x019f:
                    java.lang.StringBuilder r7 = new java.lang.StringBuilder
                    r7.<init>()
                    java.lang.String r0 = "mibIdentifier   requestId =  "
                    r7.append(r0)
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    com.epson.mobilephone.common.escpr.MIBIdentifier r0 = r0.mibIdentifier
                    int r0 = r0.requestId
                    r7.append(r0)
                    java.lang.String r7 = r7.toString()
                    com.epson.mobilephone.common.EPLog.d(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.EscprLib r7 = r7.mEscprlib
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    byte[] r1 = r1.mEngineID
                    int r7 = r7.mibParseSecResponse(r0, r1)
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "mSequence   "
                    r0.append(r1)
                    com.epson.mobilephone.common.ble.BleWork r1 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r1 = r1.mSequence
                    r0.append(r1)
                    java.lang.String r1 = " ret = "
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r0 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r0)
                    if (r7 == 0) goto L_0x01fc
                    java.lang.String r7 = "[Read]SNMP Response :end!"
                    com.epson.mobilephone.common.EPLog.i(r7)
                    goto L_0x03a1
                L_0x01fc:
                    r6.IncrementSequence()
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]SNMP Response :"
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = " mSequence   "
                    r0.append(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.setSSIDWork()
                    goto L_0x03a1
                L_0x0226:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.EscprLib r7 = r7.mEscprlib
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    java.lang.String r7 = r7.mibParseResponseAsStr(r0)
                    boolean r0 = r7.isEmpty()
                    r0 = r0 ^ r3
                    if (r0 != 0) goto L_0x0244
                    java.lang.String r7 = "[Read]SNMP Response :end!"
                    com.epson.mobilephone.common.EPLog.i(r7)
                    goto L_0x03a1
                L_0x0244:
                    r6.IncrementSequence()
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]SNMP Response :"
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = " mSequence   "
                    r0.append(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.setSSIDWork()
                    goto L_0x03a1
                L_0x026e:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.EscprLib r7 = r7.mEscprlib
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.escpr.MIBCommand r0 = r0.mMibCommand
                    java.lang.String r7 = r7.mibParseResponseAsMacaddress(r0)
                    boolean r0 = r7.isEmpty()
                    if (r0 != 0) goto L_0x02b6
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    java.lang.String unused = r0.mDeviceMacAddress = r7
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r1 = com.epson.mobilephone.common.ble.BleWork.jobSequence.MIB_SET_SSID
                    r0.mSequence = r1
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]SNMP Response :sucess:MacAddress ★"
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = "★ mSequence "
                    r0.append(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r7 = r7.mSequence
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.setSSIDWork()
                    goto L_0x03a1
                L_0x02b6:
                    java.lang.String r7 = "[Read]SNMP Response :failed"
                    com.epson.mobilephone.common.EPLog.e((java.lang.String) r7)
                    goto L_0x03a1
                L_0x02bd:
                    java.lang.Integer r7 = r7.getIntValue(r1, r4)
                    java.lang.String r7 = r7.toString()
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "[Read]Version "
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    java.lang.String r7 = r7.mSSID
                    if (r7 == 0) goto L_0x03a1
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    java.lang.String r7 = r7.mSSID
                    boolean r7 = r7.isEmpty()
                    if (r7 != 0) goto L_0x03a1
                    r6.IncrementSequence()
                    java.lang.String r7 = " @@  checkSSID  @@ "
                    com.epson.mobilephone.common.EPLog.d(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    android.bluetooth.BluetoothGatt r7 = r7.mConnGatt
                    if (r7 != 0) goto L_0x0302
                    java.lang.String r7 = "mConnGatt  NULL !"
                    com.epson.mobilephone.common.EPLog.e((java.lang.String) r7)
                L_0x0302:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    short r7 = r7.getSecurityType()
                    r0 = 255(0xff, float:3.57E-43)
                    if (r7 != r0) goto L_0x0328
                    java.lang.String r7 = " skip  "
                    com.epson.mobilephone.common.EPLog.d(r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.util.BLEUtility$BleWorkCallback r7 = r7.mSequenceCallbackSuccess
                    if (r7 == 0) goto L_0x03a1
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.util.BLEUtility$BleWorkCallback r7 = r7.mSequenceCallbackSuccess
                    com.epson.mobilephone.common.ble.BleWork r0 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = r0.mSequence
                    r7.call(r0)
                    goto L_0x03a1
                L_0x0328:
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.checkSSID()
                    goto L_0x03a1
                L_0x032e:
                    java.lang.Integer r7 = r7.getIntValue(r1, r4)
                    short r7 = r7.shortValue()
                    java.lang.Short r7 = java.lang.Short.valueOf(r7)
                    java.lang.String r0 = r7.toString()
                    java.lang.StringBuilder r1 = new java.lang.StringBuilder
                    r1.<init>()
                    java.lang.String r2 = "802a0001-4ef4-4e59-b573-2bed4a4ac158\n[Read]Password "
                    r1.append(r2)
                    r1.append(r0)
                    java.lang.String r0 = r1.toString()
                    com.epson.mobilephone.common.EPLog.i(r0)
                    short r7 = r7.shortValue()
                    r0 = 65535(0xffff, float:9.1834E-41)
                    if (r7 != r0) goto L_0x0367
                    java.lang.String r7 = "password error !"
                    com.epson.mobilephone.common.EPLog.e((java.lang.String) r7)
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    com.epson.mobilephone.common.ble.BleWork$jobSequence r0 = com.epson.mobilephone.common.ble.BleWork.jobSequence.FAILED
                    r7.mSequence = r0
                    goto L_0x03a1
                L_0x0367:
                    r6.IncrementSequence()
                    com.epson.mobilephone.common.ble.BleWork r7 = com.epson.mobilephone.common.ble.BleWork.this
                    r7.checkVersion()
                    goto L_0x03a1
                L_0x0370:
                    java.lang.String r7 = r7.getStringValue(r4)
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "00002a29-0000-1000-8000-00805f9b34fb\n[Read]Serial Number "
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                    goto L_0x03a1
                L_0x0389:
                    java.lang.String r7 = r7.getStringValue(r4)
                    java.lang.StringBuilder r0 = new java.lang.StringBuilder
                    r0.<init>()
                    java.lang.String r1 = "00002a29-0000-1000-8000-00805f9b34fb\n[Read]Manufacturer Name "
                    r0.append(r1)
                    r0.append(r7)
                    java.lang.String r7 = r0.toString()
                    com.epson.mobilephone.common.EPLog.i(r7)
                L_0x03a1:
                    return
                */
                throw new UnsupportedOperationException("Method not decompiled: com.epson.mobilephone.common.ble.BleWork.C10141.readCharacteristicData(android.bluetooth.BluetoothGattCharacteristic):void");
            }

            private void IncrementSequence() {
                int ordinal = BleWork.mSequence.ordinal();
                jobSequence[] values = jobSequence.values();
                BleWork.mSequence = values[ordinal + 1];
                EPLog.i("ΔΔ　mSequence = " + BleWork.mSequence);
            }

            public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
                showStatus("Write", i);
                if (i != 0) {
                    byte unused = BleWork.mIndex = (byte) 0;
                    if (BleWork.mSequence == jobSequence.INITIALIZE) {
                        failedProcessing();
                    } else if (BleWork.mSequence == jobSequence.MIB_REBOOT_NW) {
                        if (BleWork.mSuccess != null) {
                            BleWork.mSuccess.call(BleWork.mSequence);
                        }
                    } else if (BleWork.mFailed != null) {
                        BleWork.mFailed.call(BleWork.mSequence);
                    }
                } else if (BleWork.mSequence == jobSequence.INITIALIZE) {
                    characteristicRead();
                } else if (BleWork.mSequence == jobSequence.MIB_REBOOT_NW && BleWork.mSuccess != null) {
                    BleWork.mSuccess.call(BleWork.mSequence);
                }
            }

            private void notifySet(@NonNull BluetoothGattCharacteristic bluetoothGattCharacteristic) {
                if (!BleWork.mConnGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true)) {
                    EPLog.e("Notification set ERROR");
                }
                BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID.fromString(BLEUuid.CLIENT_CHARACTERISTIC_CONFIG));
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                EPLog.i("notify set");
                if (!BleWork.mConnGatt.writeDescriptor(descriptor)) {
                    EPLog.e("Notification write ERROR");
                }
            }
        };
        handlerRead = new Handler();
        handlerWrite = new Handler();
    }

    @NonNull
    public static BleWork getInstace() {
        return SingletonHolder.INSTANCE;
    }

    private static final class SingletonHolder {
        private static final BleWork INSTANCE = new BleWork();

        private SingletonHolder() {
        }
    }

    public void resetSequence() {
        mSequence = jobSequence.UNINITIALIZED;
        mDevice = null;
        mSSIDPassword = null;
        mSsidLis.clear();
        mReconnect = false;
        mConnGatt = null;
    }

    @NonNull
    public static String getSSID(@NonNull Context context) {
        String curSSID = WiFiDirectManager.getCurSSID(context);
        EPLog.i("" + curSSID);
        return (curSSID == null || curSSID.equals("<unknown ssid>")) ? "" : curSSID;
    }

    public static boolean chechSSIDforBle(@NonNull Context context) {
        if (Build.VERSION.SDK_INT <= 28 || ActivityRequestLocationPermission.canAccessWiFiInfo(context, 4)) {
            return !getSSID(context).isEmpty() && !WiFiDirectManager.isSimpleAP(context);
        }
        return WiFiNetworkManager.getInsetance(context).isWiFiValidated();
    }

    public static boolean isStartBleProcess(@NonNull Context context, Boolean bool) {
        BluetoothAdapter adapter;
        BluetoothManager manager = BLEUtility.getManager(context);
        if ((manager != null && ((adapter = manager.getAdapter()) == null || !adapter.isEnabled())) || !BLEUtility.isBLESupported(context)) {
            return false;
        }
        if ((!bool.booleanValue() || MyPrinter.getCurPrinter(context).getName() == null) && chechSSIDforBle(context)) {
            return true;
        }
        return false;
    }

    private void setSequenceCallback(BLEUtility.BleWorkCallback bleWorkCallback, BLEUtility.BleWorkCallback bleWorkCallback2) {
        mSequenceCallbackSuccess = bleWorkCallback;
        mSequenceCallbackFailed = bleWorkCallback2;
    }

    private void setNotifyCallback(BLEUtility.BleWorkCallback bleWorkCallback) {
        mResposne = bleWorkCallback;
    }

    private void setSSID(BLEUtility.BleWorkCallback bleWorkCallback, BLEUtility.BleWorkCallback bleWorkCallback2) {
        mSuccess = bleWorkCallback;
        mFailed = bleWorkCallback2;
        mSequence = jobSequence.MIB_GET_MACADDRESS;
        setSSIDWork();
    }

    public void disconnect(boolean z) {
        EPLog.i("mSequence == " + mSequence + ":: " + mStatus);
        mSequence = z ? jobSequence.DISCONNECTED : mSequence;
        if (mConnGatt != null) {
            int i = mStatus;
            if (!(i == 3 || i == 0)) {
                EPLog.d(" !! mConnGatt disconnect");
                mConnGatt.disconnect();
            }
            EPLog.d(" !! mConnGatt close " + mSequence + " " + mStatus);
            mReconnect = false;
            if (z) {
                gattClose();
            }
        }
    }

    private void gattClose() {
        EPLog.d(" !! mConnGatt close");
        BluetoothGatt bluetoothGatt = mConnGatt;
        if (bluetoothGatt != null) {
            bluetoothGatt.close();
            mConnGatt = null;
        }
    }

    public void disconnect() {
        disconnect(true);
    }

    public void connect() {
        EPLog.i("再接続");
        mReconnect = true;
        mRetry = 0;
        EPLog.e("init");
        init();
    }

    private void setSSIDWork() {
        ((Activity) mContext).runOnUiThread(new Runnable() {
            public void run() {
                startSetSSID();
            }
        });
    }

    private void notifySet(@NonNull BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        if (!mConnGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true)) {
            EPLog.e("Notification set ERROR");
        }
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID.fromString(BLEUuid.CLIENT_CHARACTERISTIC_CONFIG));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        EPLog.i("notify set");
        if (!mConnGatt.writeDescriptor(descriptor)) {
            EPLog.e(" writeDescriptor ERROR");
        }
    }

    public class NotifySetThread extends AsyncTask<Void, Void, Boolean> {
        boolean stop;
        boolean success;

        private NotifySetThread() {
            stop = false;
            success = false;
        }

        protected void onPreExecute() {
            super.onPreExecute();
            EPLog.i();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            setNotifyCallback(new BLEUtility.BleWorkCallback() {
                public void call(@NonNull Object obj) {
                    EPLog.i(" -- NotifySetThread call");
                    BluetoothGattCharacteristic bluetoothGattCharacteristic = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SECURITY_TYPE_STRING);
                    BluetoothGattCharacteristic bluetoothGattCharacteristic2 = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SNMP_STATUS_STRING);
                    BluetoothGattDescriptor bluetoothGattDescriptor = (BluetoothGattDescriptor) obj;
                    if (bluetoothGattCharacteristic == bluetoothGattDescriptor.getCharacteristic()) {
                        notifySet(bluetoothGattCharacteristic2);
                    } else if (bluetoothGattCharacteristic2 == bluetoothGattDescriptor.getCharacteristic()) {
                        NotifySetThread notifySetThread = NotifySetThread.this;
                        notifySetThread.stop = true;
                        notifySetThread.success = true;
                    } else {
                        NotifySetThread.this.stop = true;
                    }
                }
            });
        }

        /* access modifiers changed from: protected */
        @Nullable
        public Boolean doInBackground(Void... voidArr) {
            notifySet(BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SECURITY_TYPE_STRING));
            long currentTimeMillis = System.currentTimeMillis();
            while (System.currentTimeMillis() - currentTimeMillis <= 30000) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    if (!this.stop) {
                    }
                }
                if (stop) {
                    EPLog.i(" set ended ");
                    EPLog.i();
                    return Boolean.valueOf(success);
                }
            }
            EPLog.e(" set failed! ");
            return false;
        }

        protected void onPostExecute(Boolean bool) {
            super.onPostExecute(bool);
            if (bool.booleanValue()) {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                BleWork.mSequence = jobSequence.INITIALIZE;
                checkPassword();
                return;
            }
            EPLog.e("setNotify");
            failedProcessing();
        }
    }

    private void characteristicRead() {
        EPLog.i("read " + mSequence);
        if (mSequence != jobSequence.MIB_REBOOT_NW) {
            final int[] iArr = {0};
            handlerRead.postDelayed(new Runnable() {
                public void run() {
                    switch (C10195.$SwitchMap$com$epson$mobilephone$common$ble$BleWork$jobSequence[BleWork.mSequence.ordinal()]) {
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                        case 5:
                        case 6:
                        case 7:
                        case 8:
                        case 11:
                            try {
                                if (!((BluetoothGatt) Objects.requireNonNull(BleWork.mConnGatt)).readCharacteristic(BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SNMP_RESPONSE_STRING))) {
                                    int[] iArr = iArr;
                                    int i = iArr[0] + 1;
                                    iArr[0] = i;
                                    if (i < 3) {
                                        EPLog.e("FALSE !! retry " + iArr[0]);
                                        handlerRead.removeCallbacks((Runnable) null);
                                        handlerRead.postDelayed(this, (long) (iArr[0] * 500));
                                        return;
                                    }
                                    EPLog.e("FALSE !!!");
                                    sequenceError();
                                    return;
                                }
                                return;
                            } catch (NullPointerException unused) {
                                EPLog.e("mConnGatt NULL !");
                                return;
                            }
                        case 9:
                            if (!BleWork.mConnGatt.readCharacteristic(BLEUuid.GattCharsMap.get(BLEUuid.CHAR_PASSWORD_STRING))) {
                                EPLog.e("FALSE !! ");
                                return;
                            }
                            return;
                        case 10:
                            if (!BleWork.mConnGatt.readCharacteristic(BLEUuid.GattCharsMap.get(BLEUuid.CHAR_VERSION_STRING))) {
                                EPLog.e("FALSE !! ");
                                return;
                            }
                            return;
                        default:
                            return;
                    }
                }
            }, 0);
        }
    }

    private boolean init() {
        int i;
        mEscprlib.epsWrapperInitDriver(mContext.getApplicationContext(), 2);
        if (!BLEUtility.isBLESupported(mContext)) {
            return false;
        }
        BluetoothManager manager = BLEUtility.getManager(mContext);
        if (manager != null) {
            mBTAdapter = manager.getAdapter();
        }
        if (mBTAdapter == null) {
            return false;
        }
        if (mDevice == null) {
            mDevice = mSelectItem.getDevice();
            if (mDevice == null) {
                EPLog.e("mDevice is null !!!");
                return false;
            }
        }
        byte[] hardwareType = mSelectItem.getHardwareType();
        if (hardwareType.length >= 3) {
            i = 4096 - ByteBuffer.wrap(new byte[]{0, 0, hardwareType[2], hardwareType[1]}).getInt();
        } else {
            i = Constants.EDAM_APPLICATIONDATA_ENTRY_LEN_MAX;
        }
        EPLog.d("password = " + i);
        mDevicePassword = String.valueOf(i);
        if (mConnGatt == null && mStatus == 0) {
            EPLog.i("connectGatt");
            mConnGatt = mDevice.connectGatt(mContext, false, mGattcallback);
            mStatus = 1;
            if (!mReconnect) {
                mSequence = jobSequence.UNINITIALIZED;
            }
        } else if (mConnGatt != null) {
            EPLog.i("re-connect and re-discover Services");
            mConnGatt.close();
            mConnGatt = mDevice.connectGatt(mContext, false, mGattcallback);
            mConnGatt.connect();
            mConnGatt.discoverServices();
            if (!mReconnect) {
                mSequence = jobSequence.INITIALIZE;
            }
        } else {
            EPLog.i("  STATE_CONNECTING");
            mConnGatt = mDevice.connectGatt(mContext, false, mGattcallback);
            mStatus = 1;
            if (!mReconnect) {
                mSequence = jobSequence.DISCONNECTED;
            }
        }
        return true;
    }

    public boolean init(@NonNull Context context, @NonNull ScannedDevice scannedDevice) {
        mContext = context;
        mSelectItem = scannedDevice;
        mRetry = 0;
        mSequence = jobSequence.UNINITIALIZED;
        setSecurityType(0);
        mReconnect = true;
        mSSID = getSSID(mContext);
        EPLog.i(mSSID);
        EPLog.e("init");
        return init();
    }

    public void checkPassword() {
        EPLog.i();
        String str = mDevicePassword;
        if (!str.isEmpty()) {
            BluetoothGattCharacteristic bluetoothGattCharacteristic = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_PASSWORD_STRING);
            bluetoothGattCharacteristic.setValue(Short.parseShort(str), 18, 0);
            EPLog.i("Password write :" + str);
            gattWriteCharacter(bluetoothGattCharacteristic);
        }
    }

    private void gattWriteCharacter(final BluetoothGattCharacteristic bluetoothGattCharacteristic) {
        final int[] iArr = {0};
        handlerWrite.removeCallbacks((Runnable) null);
        handlerWrite.postDelayed(new Runnable() {
            public void run() {
                if (BleWork.mConnGatt == null) {
                    EPLog.e("mConnGatt  NULL !");
                }
                if (bluetoothGattCharacteristic == null) {
                    EPLog.e("characteristic  NULL !");
                }
                try {
                    if (!((BluetoothGatt) Objects.requireNonNull(BleWork.mConnGatt)).writeCharacteristic(bluetoothGattCharacteristic)) {
                        int[] iArr = iArr;
                        int i = iArr[0] + 1;
                        iArr[0] = i;
                        if (i <= 3) {
                            EPLog.e("writeCharacteristic error ! retry " + iArr[0]);
                            handlerWrite.removeCallbacks((Runnable) null);
                            handlerWrite.postDelayed(this, (long) (iArr[0] * 500));
                            return;
                        }
                        EPLog.e("writeCharacteristic error !! retry failed !!");
                        failedProcessing();
                    }
                } catch (NullPointerException unused) {
                    EPLog.e("mConnGatt NULL !");
                }
            }
        }, 0);
    }

    public void checkVersion() {
        characteristicRead();
    }

    public void getSSIDList() {
        mInSequence = true;
        mSequence = jobSequence.MIB_GET_SSID_LIST;
        EscprLib escprLib = mEscprlib;
        MIBCommand mIBCommand = mMibCommand;
        byte b = (byte) (mIndex + 1);
        mIndex = b;
        int mibGetESSIDList = escprLib.mibGetESSIDList(mIBCommand, b);
        if (mibGetESSIDList != 0) {
            EPLog.e("mibGetESSIDList ret = " + mibGetESSIDList);
            return;
        }
        byte[] bArr = new byte[mMibCommand.commandBlock.bufSize];
        System.arraycopy(mMibCommand.commandBlock.buf, 0, bArr, 0, mMibCommand.commandBlock.bufSize);
        BluetoothGattCharacteristic bluetoothGattCharacteristic = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SNMP_REQUEST_STRING);
        bluetoothGattCharacteristic.setValue(bArr);
        gattWriteCharacter(bluetoothGattCharacteristic);
    }

    private void checkSSID() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        byte[] bytes = mSSID.getBytes(Charset.forName("UTF8"));
        BluetoothGattCharacteristic bluetoothGattCharacteristic = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SSID_STRING);
        bluetoothGattCharacteristic.setValue(bytes);
        EPLog.i("SSID write :" + mSSID);
        gattWriteCharacter(bluetoothGattCharacteristic);
    }

    public void setSSIDType() {
        mSequence = jobSequence.CHECK_SSID;
        if (mConnGatt == null) {
            EPLog.e("mConnGatt  NULL !");
        }
        checkSSID();
    }

    private void startSetSSID() {
        int i;
        EPLog.i("mSequence = " + mSequence + " mSSID =  " + mSSID);
        mInSequence = false;
        String str = "";
        String str2 = "";
        switch (mSequence) {
            case MIB_GET_MACADDRESS:
                str = "epsGetMacAddress";
                str2 = "mibGetMacadress";
                i = mEscprlib.mibGetMacadress(mMibCommand);
                break;
            case MIB_SET_SSID:
                str = "epsWlanConfSSID";
                str2 = "mibSetSSID";
                EPLog.i("mSSID  " + mSSID);
                if (!mSSID.isEmpty()) {
                    i = mEscprlib.mibSetSSID(mMibCommand, mSSID);
                    break;
                } else {
                    EPLog.m82w("set SSID :  empty");
                    return;
                }
            case MIB_SEC_LOGIN:
                str = "epsAdminMibAccessLoginOperation";
                str2 = "mibSecLogin ";
                if (mEngineID == null) {
                    EPLog.e("mEngineID");
                    failedProcessing();
                }
                i = mEscprlib.mibSecLogin(mMibCommand, mEngineID);
                break;
            case MIB_SEC_SET_PASS:
                str = "epsWlanCommonPassPhrase";
                str2 = "mibSecSetPass ";
                String str3 = mSSIDPassword;
                EPLog.i("mSSIDPassword  " + mSSIDPassword);
                if (str3.isEmpty()) {
                    EPLog.m82w("SSIDPassword is empty");
                }
                if (mEngineID == null) {
                    EPLog.e("mEngineID");
                    failedProcessing();
                }
                i = mEscprlib.mibSecSetPassword(mMibCommand, mEngineID, str3);
                break;
            case MIB_SEC_LOGOUT:
                str = "epsAdminMibAccessLogOutOperation";
                str2 = "mibSecLogout ";
                if (mEngineID == null) {
                    EPLog.e("mEngineID");
                    failedProcessing();
                }
                i = mEscprlib.mibSecLogout(mMibCommand, mEngineID);
                break;
            case MIB_SET_SECURITY_TYPE:
                str = "epsWlanSecurityType";
                str2 = "mibSetSecurityType ";
                if (mSSIDPassword == null) {
                    mSSIDPassword = "";
                }
                int i2 = mSSIDPassword.isEmpty() ? 0 : 255;
                EPLog.i("MIB_SET_SECURITY_TYPE SecurityType = " + i2);
                int mibSetSecurityType = mEscprlib.mibSetSecurityType(mMibCommand, i2);
                if (i2 == 0) {
                    mSequence = jobSequence.MIB_SEC_SKIP;
                }
                i = mibSetSecurityType;
                break;
            case MIB_REBOOT_NW:
                str = "epsBluetoothOperationSetupEnd";
                str2 = "mibRebootNW  ";
                i = mEscprlib.mibRebootNWwithBLE(mMibCommand);
                break;
            default:
                i = 0;
                break;
        }
        if (i != 0) {
            EPLog.e(str2 + " ret = " + i);
            sequenceError();
            return;
        }
        byte[] bArr = new byte[mMibCommand.commandBlock.bufSize];
        System.arraycopy(mMibCommand.commandBlock.buf, 0, bArr, 0, mMibCommand.commandBlock.bufSize);
        EPLog.i("SNMP Request write :" + str + " mSequence   " + mSequence);
        BluetoothGattCharacteristic bluetoothGattCharacteristic = BLEUuid.GattCharsMap.get(BLEUuid.CHAR_SNMP_REQUEST_STRING);
        bluetoothGattCharacteristic.setValue(bArr);
        gattWriteCharacter(bluetoothGattCharacteristic);
    }

    private void sequenceError() {
        failedProcessing();
    }

    private void failedProcessing() {
        disconnect();
        if (mSequenceCallbackFailed != null) {
            EPLog.e(" " + mSequence);
            mSequenceCallbackFailed.call(mSequence);
        }
    }
}
