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.EpLog; 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 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() { this.mSuccess = null; this.mFailed = null; this.mScannedDeviceList = new CopyOnWriteArrayList(); if (Build.VERSION.SDK_INT >= 21) { this.mScanCallback = new ScanCallback() { @RequiresApi(api = 21) public void onScanResult(int i, @NonNull ScanResult scanResult) { ScanRecord scanRecord = scanResult.getScanRecord(); if (scanRecord == null) { EpLog.m71e("scanRecord is NULL !!"); return; } SparseArray 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.this.mScannedDeviceList.size() > 0 && BleScanWork.this.mSuccess != null) { EpLog.m71e("mScannedDeviceList --- " + BleScanWork.this.mScannedDeviceList.size()); BleScanWork.this.mSuccess.call(BleScanWork.this.mScannedDeviceList); return; } return; } } } public void onScanFailed(int i) { EpLog.m71e("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; } }; } this.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.this.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.m69d(name + " Company information = " + sb2 + " getManufacturerSpecificData = " + sb3); if (BleScanWork.this.update(bluetoothDevice, i, name, bArr2) && BleScanWork.this.mSuccess != null && name != null) { EpLog.m71e("mScannedDeviceList --- " + BleScanWork.this.mScannedDeviceList.size()); BleScanWork.this.mSuccess.call(BleScanWork.this.mScannedDeviceList); } } } }; } @NonNull public ArrayList getScannedDeviceList() { return new ArrayList<>(this.mScannedDeviceList); } private void search(BLEUtility.BleWorkCallback bleWorkCallback, BLEUtility.BleWorkCallback bleWorkCallback2) { this.mSuccess = bleWorkCallback; this.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 it = this.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; } this.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) { this.mBTAdapter = manager.getAdapter(); } BluetoothAdapter bluetoothAdapter = this.mBTAdapter; if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { return false; } if (Build.VERSION.SDK_INT >= 21) { this.mBluetoothLeScanner = this.mBTAdapter.getBluetoothLeScanner(); if (this.mBluetoothLeScanner == null) { return false; } } stopScan(); EpLog.m69d("★ stopScan"); return true; } private void startScan(boolean z) { ScanSettings scanSettings; EpLog.m77i("☆startScan☆"); this.mScannedDeviceList.clear(); this.mIsFiltering = z; if (this.mBTAdapter != null && !this.mIsScanning) { if (Build.VERSION.SDK_INT < 21) { this.mBTAdapter.startLeScan(this.mLeScanCallback); } else if (Build.VERSION.SDK_INT >= 21) { ArrayList arrayList = new ArrayList(); if (this.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 = this.mBluetoothLeScanner; if (bluetoothLeScanner != null) { bluetoothLeScanner.startScan(arrayList, scanSettings, this.mScanCallback); this.mIsScanning = true; } } } } public void stopScan() { EpLog.m77i("stopScan"); if (this.mBTAdapter == null) { return; } if (Build.VERSION.SDK_INT < 21) { this.mBTAdapter.stopLeScan(this.mLeScanCallback); EpLog.m77i("mLeScanCallback"); this.mIsScanning = false; return; } BluetoothLeScanner bluetoothLeScanner = this.mBluetoothLeScanner; if (bluetoothLeScanner != null) { bluetoothLeScanner.stopScan(this.mScanCallback); EpLog.m77i("mScanCallback"); this.mIsScanning = false; } } }