BleScanWork.java 10.0 KB

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