165 lines
5.9 KiB
Java
165 lines
5.9 KiB
Java
package com.ubtrobot.jimu.bluetooth.base.discover;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.bluetooth.BluetoothAdapter;
|
|
import android.bluetooth.BluetoothDevice;
|
|
import android.bluetooth.BluetoothManager;
|
|
import android.bluetooth.le.BluetoothLeScanner;
|
|
import android.bluetooth.le.ScanCallback;
|
|
import android.content.Context;
|
|
import android.os.Build;
|
|
import android.os.Handler;
|
|
import android.util.Log;
|
|
import com.ubtrobot.jimu.bluetooth.Cancellable;
|
|
import com.ubtrobot.log.ALog;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class BleDeviceScanner {
|
|
private BluetoothAdapter b;
|
|
private Handler d;
|
|
private ScannedHubEmitter e;
|
|
private int a = 5000;
|
|
private List<BluetoothDevice> c = new ArrayList();
|
|
private BluetoothAdapter.LeScanCallback f = new BluetoothAdapter.LeScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner.1
|
|
@Override // android.bluetooth.BluetoothAdapter.LeScanCallback
|
|
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
|
|
if (BleDeviceScanner.this.c.contains(bluetoothDevice)) {
|
|
return;
|
|
}
|
|
BleDeviceScanner.this.c.add(bluetoothDevice);
|
|
BleDeviceScanner.this.e.a(new ScanResult(bluetoothDevice, i));
|
|
}
|
|
};
|
|
private ScanCallback g = new ScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner.2
|
|
@Override // android.bluetooth.le.ScanCallback
|
|
public void onBatchScanResults(List<android.bluetooth.le.ScanResult> list) {
|
|
super.onBatchScanResults(list);
|
|
}
|
|
|
|
@Override // android.bluetooth.le.ScanCallback
|
|
public void onScanFailed(int i) {
|
|
Log.e("ContentValues", "Scan ble fail! errorCode:" + i);
|
|
super.onScanFailed(i);
|
|
BleDeviceScanner.this.e.a(i, "Scan ble device fail! Detail define refer to ScanCallback.SCAN_FAILED_*");
|
|
}
|
|
|
|
@Override // android.bluetooth.le.ScanCallback
|
|
@TargetApi(21)
|
|
public void onScanResult(int i, android.bluetooth.le.ScanResult scanResult) {
|
|
BluetoothDevice device = scanResult.getDevice();
|
|
if (BleDeviceScanner.this.c.contains(device)) {
|
|
return;
|
|
}
|
|
BleDeviceScanner.this.c.add(device);
|
|
BleDeviceScanner.this.e.a(new ScanResult(device, scanResult.getRssi()));
|
|
}
|
|
};
|
|
|
|
private class DiscoverCancelable implements Cancellable, Runnable {
|
|
private final Handler a;
|
|
private final ScannedHubEmitter b;
|
|
private boolean c;
|
|
private boolean d;
|
|
|
|
public DiscoverCancelable(Handler handler, ScannedHubEmitter scannedHubEmitter, int i) {
|
|
this.a = handler;
|
|
this.a.postDelayed(this, i);
|
|
this.b = scannedHubEmitter;
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
|
public synchronized boolean cancel() {
|
|
if (!this.d && !this.c) {
|
|
this.a.removeCallbacks(this);
|
|
BleDeviceScanner.this.b();
|
|
this.d = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
|
public synchronized boolean isCancelled() {
|
|
return this.d;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public synchronized void run() {
|
|
if (!this.d) {
|
|
this.c = true;
|
|
BleDeviceScanner.this.b();
|
|
this.b.a();
|
|
}
|
|
}
|
|
}
|
|
|
|
public BleDeviceScanner(Context context) {
|
|
this.d = new Handler(context.getMainLooper());
|
|
this.b = ((BluetoothManager) context.getSystemService("bluetooth")).getAdapter();
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void b() {
|
|
BluetoothAdapter bluetoothAdapter = this.b;
|
|
if (bluetoothAdapter == null) {
|
|
return;
|
|
}
|
|
if (Build.VERSION.SDK_INT < 21) {
|
|
bluetoothAdapter.stopLeScan(this.f);
|
|
return;
|
|
}
|
|
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
|
|
if (bluetoothLeScanner == null) {
|
|
return;
|
|
}
|
|
bluetoothLeScanner.stopScan(this.g);
|
|
ALog.a("ContentValues").d("call BluetoothLeScanner.stopScan by new api after Android LOLLIPOP");
|
|
}
|
|
|
|
public Cancellable a(ScannedHubEmitter scannedHubEmitter, int i) {
|
|
BluetoothAdapter bluetoothAdapter = this.b;
|
|
if (bluetoothAdapter == null) {
|
|
Log.e("ContentValues", "Bluetooth is not support!");
|
|
this.e.a(4, "Scan fail as bluetooth is not support");
|
|
return null;
|
|
}
|
|
if (bluetoothAdapter.getState() != 12) {
|
|
scannedHubEmitter.a(-3, "Bluetooth is not turn on!");
|
|
return null;
|
|
}
|
|
if (i < 5000) {
|
|
Log.e("ContentValues", "Duration must greater than 5000");
|
|
this.a = 5000;
|
|
} else {
|
|
this.a = i;
|
|
}
|
|
DiscoverCancelable discoverCancelable = new DiscoverCancelable(this.d, scannedHubEmitter, this.a);
|
|
this.e = scannedHubEmitter;
|
|
try {
|
|
a();
|
|
} catch (Exception e) {
|
|
Log.e("ContentValues", "Start scan fail", e);
|
|
}
|
|
return discoverCancelable;
|
|
}
|
|
|
|
private void a() {
|
|
if (this.b == null) {
|
|
return;
|
|
}
|
|
this.c.clear();
|
|
if (Build.VERSION.SDK_INT >= 21) {
|
|
BluetoothLeScanner bluetoothLeScanner = this.b.getBluetoothLeScanner();
|
|
if (bluetoothLeScanner == null) {
|
|
return;
|
|
}
|
|
bluetoothLeScanner.startScan(this.g);
|
|
ALog.a("ContentValues").d("call BluetoothLeScanner.startScan by new api after Android LOLLIPOP");
|
|
return;
|
|
}
|
|
this.b.startLeScan(this.f);
|
|
}
|
|
}
|