BleScanWork.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.epson.mobilephone.common.ble;
  2. import android.bluetooth.BluetoothAdapter;
  3. import android.bluetooth.BluetoothDevice;
  4. import android.bluetooth.BluetoothManager;
  5. import android.bluetooth.le.BluetoothLeScanner;
  6. import android.bluetooth.le.ScanCallback;
  7. import android.bluetooth.le.ScanFilter;
  8. import android.bluetooth.le.ScanRecord;
  9. import android.bluetooth.le.ScanResult;
  10. import android.bluetooth.le.ScanSettings;
  11. import android.content.Context;
  12. import android.os.Build;
  13. import android.os.ParcelUuid;
  14. import android.support.annotation.RequiresApi;
  15. import android.util.SparseArray;
  16. import androidx.annotation.NonNull;
  17. import androidx.annotation.Nullable;
  18. import com.epson.mobilephone.common.ble.util.BLEUtility;
  19. import com.epson.mobilephone.common.ble.util.BLEUuid;
  20. import com.epson.mobilephone.common.ble.util.ScannedDevice;
  21. import com.google.common.primitives.UnsignedBytes;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.concurrent.CopyOnWriteArrayList;
  26. import epson.print.Util.EPLog;
  27. public class BleScanWork {
  28. private static final int COMPANY_IDENTIFIER_EPSON = 64;
  29. private BluetoothAdapter mBTAdapter;
  30. private BluetoothLeScanner mBluetoothLeScanner;
  31. @Nullable
  32. private BLEUtility.BleWorkCallback mFailed;
  33. private boolean mIsFiltering;
  34. private boolean mIsScanning;
  35. @NonNull
  36. private BluetoothAdapter.LeScanCallback mLeScanCallback;
  37. @Nullable
  38. private ScanCallback mScanCallback;
  39. /* access modifiers changed from: private */
  40. @NonNull
  41. public List<ScannedDevice> mScannedDeviceList;
  42. /* access modifiers changed from: private */
  43. @Nullable
  44. public BLEUtility.BleWorkCallback mSuccess;
  45. @NonNull
  46. public static BleScanWork getInstace() {
  47. return SingletonHolder.INSTANCE;
  48. }
  49. private static class SingletonHolder {
  50. private static final BleScanWork INSTANCE = new BleScanWork();
  51. private SingletonHolder() {
  52. }
  53. }
  54. private BleScanWork() {
  55. mSuccess = null;
  56. mFailed = null;
  57. mScannedDeviceList = new CopyOnWriteArrayList();
  58. if (Build.VERSION.SDK_INT >= 21) {
  59. mScanCallback = new ScanCallback() {
  60. @RequiresApi(api = 21)
  61. public void onScanResult(int i, @NonNull ScanResult scanResult) {
  62. ScanRecord scanRecord = scanResult.getScanRecord();
  63. if (scanRecord == null) {
  64. EPLog.e("scanRecord is NULL !!");
  65. return;
  66. }
  67. SparseArray<byte[]> manufacturerSpecificData = scanRecord.getManufacturerSpecificData();
  68. StringBuilder sb = new StringBuilder();
  69. for (int i2 = 0; i2 < manufacturerSpecificData.size(); i2++) {
  70. if (manufacturerSpecificData.keyAt(i2) == 64) {
  71. byte[] valueAt = manufacturerSpecificData.valueAt(i2);
  72. for (byte b : valueAt) {
  73. sb.append(BLEUtility.IntToHex2(b & UnsignedBytes.MAX_VALUE));
  74. }
  75. String deviceName = getDeviceName(scanResult);
  76. if (deviceName != null && !deviceName.isEmpty() && scanResult.getRssi() >= -75 && scanResult.getRssi() < 127 && this.update(scanResult.getDevice(), scanResult.getRssi(), deviceName, valueAt) && mScannedDeviceList.size() > 0 && mSuccess != null) {
  77. EPLog.e("mScannedDeviceList --- " + mScannedDeviceList.size());
  78. mSuccess.call(mScannedDeviceList);
  79. return;
  80. }
  81. return;
  82. }
  83. }
  84. }
  85. public void onScanFailed(int i) {
  86. EPLog.e("onScanFailed " + i);
  87. }
  88. @Nullable
  89. @RequiresApi(api = 21)
  90. private String getDeviceName(ScanResult scanResult) {
  91. String deviceName = scanResult.getScanRecord() != null ? scanResult.getScanRecord().getDeviceName() : null;
  92. return (deviceName == null || deviceName.isEmpty()) ? scanResult.getDevice().getName() : deviceName;
  93. }
  94. };
  95. }
  96. mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
  97. public void onLeScan(BluetoothDevice bluetoothDevice, int i, @NonNull byte[] bArr) {
  98. StringBuilder sb = new StringBuilder();
  99. StringBuilder sb2 = new StringBuilder();
  100. StringBuilder sb3 = new StringBuilder();
  101. if (bArr.length >= 43) {
  102. for (int i2 = 27; i2 >= 12; i2--) {
  103. sb.append(BLEUtility.IntToHex2(bArr[i2] & UnsignedBytes.MAX_VALUE));
  104. }
  105. for (int i3 = 6; i3 >= 5; i3--) {
  106. sb2.append(BLEUtility.IntToHex2(bArr[i3] & UnsignedBytes.MAX_VALUE));
  107. }
  108. for (int i4 = 7; i4 <= 9; i4++) {
  109. sb3.append(BLEUtility.IntToHex2(bArr[i4] & UnsignedBytes.MAX_VALUE));
  110. }
  111. }
  112. if (mIsFiltering && BLEUuid.SERVICE_SNMP.replace("-", "").equalsIgnoreCase(sb.toString()) && i >= -75 && i < 127) {
  113. String name = bluetoothDevice.getName();
  114. byte[] bArr2 = new byte[3];
  115. System.arraycopy(bArr, 7, bArr2, 0, bArr2.length);
  116. EPLog.d(name + " Company information = " + sb2 + " getManufacturerSpecificData = " + sb3);
  117. if (this.update(bluetoothDevice, i, name, bArr2) && mSuccess != null && name != null) {
  118. EPLog.e("mScannedDeviceList --- " + mScannedDeviceList.size());
  119. mSuccess.call(mScannedDeviceList);
  120. }
  121. }
  122. }
  123. };
  124. }
  125. @NonNull
  126. public ArrayList<ScannedDevice> getScannedDeviceList() {
  127. return new ArrayList<>(mScannedDeviceList);
  128. }
  129. private void search(BLEUtility.BleWorkCallback bleWorkCallback, BLEUtility.BleWorkCallback bleWorkCallback2) {
  130. mSuccess = bleWorkCallback;
  131. mFailed = bleWorkCallback2;
  132. startScan(true);
  133. }
  134. public boolean update(@Nullable BluetoothDevice bluetoothDevice, int i, String str, byte[] bArr) {
  135. boolean z;
  136. if (bluetoothDevice == null || bluetoothDevice.getAddress() == null) {
  137. return false;
  138. }
  139. Iterator<ScannedDevice> it = mScannedDeviceList.iterator();
  140. while (true) {
  141. if (!it.hasNext()) {
  142. z = false;
  143. break;
  144. }
  145. ScannedDevice next = it.next();
  146. if (bluetoothDevice.getAddress().equals(next.getDevice().getAddress())) {
  147. next.setRssi(i);
  148. z = true;
  149. break;
  150. }
  151. }
  152. if (z) {
  153. return false;
  154. }
  155. mScannedDeviceList.add(new ScannedDevice(bluetoothDevice, i, str, bArr));
  156. EPLog.i("☆☆ = " + str + ":" + i);
  157. return true;
  158. }
  159. public boolean init_scan(@NonNull Context context) {
  160. if (!BLEUtility.isBLESupported(context)) {
  161. return false;
  162. }
  163. BluetoothManager manager = BLEUtility.getManager(context);
  164. if (manager != null) {
  165. mBTAdapter = manager.getAdapter();
  166. }
  167. BluetoothAdapter bluetoothAdapter = mBTAdapter;
  168. if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
  169. return false;
  170. }
  171. if (Build.VERSION.SDK_INT >= 21) {
  172. mBluetoothLeScanner = mBTAdapter.getBluetoothLeScanner();
  173. if (mBluetoothLeScanner == null) {
  174. return false;
  175. }
  176. }
  177. stopScan();
  178. EPLog.d("★ stopScan");
  179. return true;
  180. }
  181. private void startScan(boolean z) {
  182. ScanSettings scanSettings;
  183. EPLog.i("☆startScan☆");
  184. mScannedDeviceList.clear();
  185. mIsFiltering = z;
  186. if (mBTAdapter != null && !mIsScanning) {
  187. if (Build.VERSION.SDK_INT < 21) {
  188. mBTAdapter.startLeScan(mLeScanCallback);
  189. } else if (Build.VERSION.SDK_INT >= 21) {
  190. ArrayList arrayList = new ArrayList();
  191. if (mIsFiltering) {
  192. arrayList.add(new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(BLEUuid.SERVICE_SNMP)).build());
  193. scanSettings = new ScanSettings.Builder().setScanMode(2).setReportDelay(0).build();
  194. } else {
  195. scanSettings = new ScanSettings.Builder().setScanMode(2).build();
  196. }
  197. BluetoothLeScanner bluetoothLeScanner = mBluetoothLeScanner;
  198. if (bluetoothLeScanner != null) {
  199. bluetoothLeScanner.startScan(arrayList, scanSettings, mScanCallback);
  200. mIsScanning = true;
  201. }
  202. }
  203. }
  204. }
  205. public void stopScan() {
  206. EPLog.i("stopScan");
  207. if (mBTAdapter == null) {
  208. return;
  209. }
  210. if (Build.VERSION.SDK_INT < 21) {
  211. mBTAdapter.stopLeScan(mLeScanCallback);
  212. EPLog.i("mLeScanCallback");
  213. mIsScanning = false;
  214. return;
  215. }
  216. BluetoothLeScanner bluetoothLeScanner = mBluetoothLeScanner;
  217. if (bluetoothLeScanner != null) {
  218. bluetoothLeScanner.stopScan(mScanCallback);
  219. EPLog.i("mScanCallback");
  220. mIsScanning = false;
  221. }
  222. }
  223. }