BleScanWork.java 9.6 KB

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