package com.epson.mobilephone.common.ble;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Build;
import android.os.ParcelUuid;


import android.support.annotation.RequiresApi;
import android.util.SparseArray;

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.google.common.primitives.UnsignedBytes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class BleScanWork {
    private static final int COMPANY_IDENTIFIER_EPSON = 64;
    private BluetoothAdapter mBTAdapter;
    private BluetoothLeScanner mBluetoothLeScanner;
    @Nullable
    private BLEUtility.BleWorkCallback mFailed;
    private boolean mIsFiltering;
    private boolean mIsScanning;
    @NonNull
    private BluetoothAdapter.LeScanCallback mLeScanCallback;
    @Nullable
    private ScanCallback mScanCallback;
    /* access modifiers changed from: private */
    @NonNull
    public List<ScannedDevice> mScannedDeviceList;
    /* access modifiers changed from: private */
    @Nullable
    public BLEUtility.BleWorkCallback mSuccess;

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

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

        private SingletonHolder() {
        }
    }

    private BleScanWork() {
        mSuccess = null;
        mFailed = null;
        mScannedDeviceList = new CopyOnWriteArrayList();
        if (Build.VERSION.SDK_INT >= 21) {
            mScanCallback = new ScanCallback() {
                @RequiresApi(api = 21)
                public void onScanResult(int i, @NonNull ScanResult scanResult) {
                    ScanRecord scanRecord = scanResult.getScanRecord();
                    if (scanRecord == null) {
                        EpLog.e("scanRecord is NULL !!");
                        return;
                    }
                    SparseArray<byte[]> manufacturerSpecificData = scanRecord.getManufacturerSpecificData();
                    StringBuilder sb = new StringBuilder();
                    for (int i2 = 0; i2 < manufacturerSpecificData.size(); i2++) {
                        if (manufacturerSpecificData.keyAt(i2) == 64) {
                            byte[] valueAt = manufacturerSpecificData.valueAt(i2);
                            for (byte b : valueAt) {
                                sb.append(BLEUtility.IntToHex2(b & UnsignedBytes.MAX_VALUE));
                            }
                            String deviceName = getDeviceName(scanResult);
                            if (deviceName != null && !deviceName.isEmpty() && scanResult.getRssi() >= -75 && scanResult.getRssi() < 127 && BleScanWork.this.update(scanResult.getDevice(), scanResult.getRssi(), deviceName, valueAt) && BleScanWork.mScannedDeviceList.size() > 0 && BleScanWork.mSuccess != null) {
                                EpLog.e("mScannedDeviceList --- " + BleScanWork.mScannedDeviceList.size());
                                BleScanWork.mSuccess.call(BleScanWork.mScannedDeviceList);
                                return;
                            }
                            return;
                        }
                    }
                }

                public void onScanFailed(int i) {
                    EpLog.e("onScanFailed " + i);
                }

                @Nullable
                @RequiresApi(api = 21)
                private String getDeviceName(ScanResult scanResult) {
                    String deviceName = scanResult.getScanRecord() != null ? scanResult.getScanRecord().getDeviceName() : null;
                    return (deviceName == null || deviceName.isEmpty()) ? scanResult.getDevice().getName() : deviceName;
                }
            };
        }
        mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
            public void onLeScan(BluetoothDevice bluetoothDevice, int i, @NonNull byte[] bArr) {
                StringBuilder sb = new StringBuilder();
                StringBuilder sb2 = new StringBuilder();
                StringBuilder sb3 = new StringBuilder();
                if (bArr.length >= 43) {
                    for (int i2 = 27; i2 >= 12; i2--) {
                        sb.append(BLEUtility.IntToHex2(bArr[i2] & UnsignedBytes.MAX_VALUE));
                    }
                    for (int i3 = 6; i3 >= 5; i3--) {
                        sb2.append(BLEUtility.IntToHex2(bArr[i3] & UnsignedBytes.MAX_VALUE));
                    }
                    for (int i4 = 7; i4 <= 9; i4++) {
                        sb3.append(BLEUtility.IntToHex2(bArr[i4] & UnsignedBytes.MAX_VALUE));
                    }
                }
                if (BleScanWork.mIsFiltering && BLEUuid.SERVICE_SNMP.replace("-", "").equalsIgnoreCase(sb.toString()) && i >= -75 && i < 127) {
                    String name = bluetoothDevice.getName();
                    byte[] bArr2 = new byte[3];
                    System.arraycopy(bArr, 7, bArr2, 0, bArr2.length);
                    EpLog.d(name + " Company information = " + sb2 + " getManufacturerSpecificData = " + sb3);
                    if (BleScanWork.this.update(bluetoothDevice, i, name, bArr2) && BleScanWork.mSuccess != null && name != null) {
                        EpLog.e("mScannedDeviceList --- " + BleScanWork.mScannedDeviceList.size());
                        BleScanWork.mSuccess.call(BleScanWork.mScannedDeviceList);
                    }
                }
            }
        };
    }

    @NonNull
    public ArrayList<ScannedDevice> getScannedDeviceList() {
        return new ArrayList<>(mScannedDeviceList);
    }

    private void search(BLEUtility.BleWorkCallback bleWorkCallback, BLEUtility.BleWorkCallback bleWorkCallback2) {
        mSuccess = bleWorkCallback;
        mFailed = bleWorkCallback2;
        startScan(true);
    }

    public boolean update(@Nullable BluetoothDevice bluetoothDevice, int i, String str, byte[] bArr) {
        boolean z;
        if (bluetoothDevice == null || bluetoothDevice.getAddress() == null) {
            return false;
        }
        Iterator<ScannedDevice> it = mScannedDeviceList.iterator();
        while (true) {
            if (!it.hasNext()) {
                z = false;
                break;
            }
            ScannedDevice next = it.next();
            if (bluetoothDevice.getAddress().equals(next.getDevice().getAddress())) {
                next.setRssi(i);
                z = true;
                break;
            }
        }
        if (z) {
            return false;
        }
        mScannedDeviceList.add(new ScannedDevice(bluetoothDevice, i, str, bArr));
        EpLog.m77i("☆☆ = " + str + ":" + i);
        return true;
    }

    public boolean init_scan(@NonNull Context context) {
        if (!BLEUtility.isBLESupported(context)) {
            return false;
        }
        BluetoothManager manager = BLEUtility.getManager(context);
        if (manager != null) {
            mBTAdapter = manager.getAdapter();
        }
        BluetoothAdapter bluetoothAdapter = mBTAdapter;
        if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
            return false;
        }
        if (Build.VERSION.SDK_INT >= 21) {
            mBluetoothLeScanner = mBTAdapter.getBluetoothLeScanner();
            if (mBluetoothLeScanner == null) {
                return false;
            }
        }
        stopScan();
        EpLog.d("★ stopScan");
        return true;
    }

    private void startScan(boolean z) {
        ScanSettings scanSettings;
        EpLog.m77i("☆startScan☆");
        mScannedDeviceList.clear();
        mIsFiltering = z;
        if (mBTAdapter != null && !mIsScanning) {
            if (Build.VERSION.SDK_INT < 21) {
                mBTAdapter.startLeScan(mLeScanCallback);
            } else if (Build.VERSION.SDK_INT >= 21) {
                ArrayList arrayList = new ArrayList();
                if (mIsFiltering) {
                    arrayList.add(new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(BLEUuid.SERVICE_SNMP)).build());
                    scanSettings = new ScanSettings.Builder().setScanMode(2).setReportDelay(0).build();
                } else {
                    scanSettings = new ScanSettings.Builder().setScanMode(2).build();
                }
                BluetoothLeScanner bluetoothLeScanner = mBluetoothLeScanner;
                if (bluetoothLeScanner != null) {
                    bluetoothLeScanner.startScan(arrayList, scanSettings, mScanCallback);
                    mIsScanning = true;
                }
            }
        }
    }

    public void stopScan() {
        EpLog.m77i("stopScan");
        if (mBTAdapter == null) {
            return;
        }
        if (Build.VERSION.SDK_INT < 21) {
            mBTAdapter.stopLeScan(mLeScanCallback);
            EpLog.m77i("mLeScanCallback");
            mIsScanning = false;
            return;
        }
        BluetoothLeScanner bluetoothLeScanner = mBluetoothLeScanner;
        if (bluetoothLeScanner != null) {
            bluetoothLeScanner.stopScan(mScanCallback);
            EpLog.m77i("mScanCallback");
            mIsScanning = false;
        }
    }
}
