jimu-decompiled/sources/com/ubtrobot/jimu/bluetooth/ble/BleLib.java
2025-05-13 19:24:51 +02:00

200 lines
7.4 KiB
Java

package com.ubtrobot.jimu.bluetooth.ble;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.ubtrobot.jimu.bluetooth.Cancellable;
import com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner;
import com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter;
import com.ubtrobot.jimu.bluetooth.proxy.Callback;
import com.ubtrobot.jimu.bluetooth.proxy.Device;
import com.ubtrobot.jimu.bluetooth.proxy.DeviceListener;
import com.ubtrobot.jimu.bluetooth.proxy.Proxy;
import com.ubtrobot.jimu.bluetooth.utils.ByteHexHelper;
import java.util.ArrayList;
import java.util.List;
/* loaded from: classes2.dex */
public class BleLib extends Callback implements Proxy {
private static BleLib e;
private static BleManager f;
private ArrayList<String> b = new ArrayList<>();
private DeviceListener c;
private BleDeviceScanner d;
private BleLib() {
new ArrayList();
new Handler(Looper.getMainLooper());
new BluetoothAdapter.LeScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.2
@Override // android.bluetooth.BluetoothAdapter.LeScanCallback
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
String replace = TextUtils.isEmpty(bluetoothDevice.getName()) ? "" : bluetoothDevice.getName().replace("\n", "").replace("\\n", "");
String str = replace + "\n" + bluetoothDevice.getAddress() + "\n" + i;
Log.i("BleLib", str);
if (BleLib.this.c(str)) {
return;
}
BleLib.this.b.add(str);
BleLib.this.c.b(str);
Proxy.a.add(new Device(bluetoothDevice.getAddress(), replace));
}
};
new ScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.4
@Override // android.bluetooth.le.ScanCallback
public void onBatchScanResults(List<ScanResult> list) {
super.onBatchScanResults(list);
}
@Override // android.bluetooth.le.ScanCallback
public void onScanFailed(int i) {
Log.e("BleLib", "Scan ble fail! errorCode:" + i);
super.onScanFailed(i);
}
@Override // android.bluetooth.le.ScanCallback
@TargetApi(21)
public void onScanResult(int i, ScanResult scanResult) {
Log.d("BleLib", "onScanResult " + scanResult.getDevice().getName());
BluetoothDevice device = scanResult.getDevice();
String replace = TextUtils.isEmpty(device.getName()) ? "" : device.getName().replace("\n", "").replace("\\n", "");
String str = replace + "\n" + device.getAddress() + "\n" + scanResult.getRssi();
Log.i("BleLib", str);
if (BleLib.this.c(str)) {
return;
}
BleLib.this.b.add(str);
BleLib.this.c.b(str);
Proxy.a.add(new Device(device.getAddress(), replace));
}
};
}
/* JADX INFO: Access modifiers changed from: private */
public boolean c(String str) {
for (int i = 0; i < this.b.size(); i++) {
if (this.b.get(i).equals(str)) {
return true;
}
}
return false;
}
public static synchronized BleLib b() {
BleLib bleLib;
synchronized (BleLib.class) {
if (e == null) {
synchronized (BleLib.class) {
if (e == null) {
e = new BleLib();
}
}
}
bleLib = e;
}
return bleLib;
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
public boolean a(Context context, DeviceListener deviceListener) {
this.c = deviceListener;
if (f == null) {
f = new BleManager(context, this);
f.b();
}
if (this.d == null) {
this.d = new BleDeviceScanner(context);
}
return BluetoothAdapter.getDefaultAdapter() != null;
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
public void b(String str, byte b, byte[] bArr, int i) {
DeviceListener deviceListener = this.c;
if (deviceListener != null) {
deviceListener.a(str, b, bArr, i);
}
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
public void b(String str) {
DeviceListener deviceListener = this.c;
if (deviceListener != null) {
deviceListener.disconnect(str);
}
}
public Cancellable a(final ScannedHubEmitter scannedHubEmitter, int i) {
return this.d.a(new ScannedHubEmitter() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.1
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
public void a(com.ubtrobot.jimu.bluetooth.base.discover.ScanResult scanResult) {
if (scanResult.a() == null || scanResult.a().getName() == null || !scanResult.a().getName().toLowerCase().contains("jimu")) {
return;
}
scannedHubEmitter.a(scanResult);
BluetoothDevice a = scanResult.a();
int b = scanResult.b();
String replace = TextUtils.isEmpty(a.getName()) ? "" : a.getName().replace("\n", "").replace("\\n", "");
String str = replace + "\n" + a.getAddress() + "\n" + b;
Log.v("BleLib", str);
if (BleLib.this.c(str)) {
return;
}
BleLib.this.b.add(str);
BleLib.this.c.b(str);
Proxy.a.add(new Device(a.getAddress(), replace));
}
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
public void a(int i2, String str) {
scannedHubEmitter.a(i2, str);
}
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
public void a() {
scannedHubEmitter.a();
}
}, i);
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
public void a(String str) {
if (TextUtils.isEmpty(str)) {
return;
}
Log.i("BleLib", "connect: mac: " + str);
f.d(str);
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
public void a() {
f.a();
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
public void a(String str, boolean z) {
f.a(str, z);
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
public synchronized void a(String str, byte b, byte[] bArr, int i) {
Log.i("BleLib", "sendData:" + ByteHexHelper.a(bArr) + " cmd:" + ((int) b));
f.b(str, b, bArr, i);
Log.i("BleLib", "after-sendData");
}
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
public void a(boolean z, String str) {
DeviceListener deviceListener = this.c;
if (deviceListener != null) {
deviceListener.onConnectState(z, str);
}
}
}