BleScanWork.java 9.6 KB

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