Initial commit

This commit is contained in:
2025-05-13 19:24:51 +02:00
commit a950f49678
10604 changed files with 932663 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.ubtrobot.jimu.bluetooth.ble;
/* compiled from: BleDeviceProcessThread.java */
/* loaded from: classes2.dex */
class BUFFER_DATA {
public byte[] a;
public int b;
public long c;
public int d;
BUFFER_DATA() {
}
}

View File

@@ -0,0 +1,197 @@
package com.ubtrobot.jimu.bluetooth.ble;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.SystemClock;
import android.util.Log;
import com.afunx.ble.blelitelib.log.BleLiteLog;
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
import com.afunx.ble.blelitelib.utils.BleUuidUtils;
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
import com.ubtrobot.jimu.bluetooth.ble.BleDeviceProcessThread;
import com.ubtrobot.jimu.bluetooth.utils.ByteHexHelper;
/* loaded from: classes2.dex */
public class BleClientHandler extends Thread implements BleDeviceProcessThread.Callback {
private static final String p = BleClientHandler.class.getSimpleName();
private String a;
private BleGattClientProxy b;
private BluetoothGattService c;
private BluetoothGattCharacteristic d;
private BluetoothGattCharacteristic e;
private int l;
private int m;
private ClientCallback n;
private boolean h = true;
private boolean k = true;
private BleGattClientProxy.OnCharacteristicNotificationListener o = new BleGattClientProxy.OnCharacteristicNotificationListener() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.1
@Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy.OnCharacteristicNotificationListener
public synchronized void onCharacteristicNotification(byte[] bArr) {
if (bArr != null) {
if (bArr.length != 0) {
try {
Log.i(BleClientHandler.p, "onReceive:msg :" + ByteHexHelper.a(bArr));
for (byte b : bArr) {
if (BleClientHandler.this.f.b(b)) {
if (BleClientHandler.this.n != null) {
BleClientHandler.this.f.a(BleClientHandler.this.f.g().length);
BleClientHandler.this.n.a(BleClientHandler.this.a, BleClientHandler.this.f.f(), BleClientHandler.this.f.g(), BleClientHandler.this.f.a());
}
BleClientHandler.this.l = 0;
BleClientHandler.this.m = 0;
BleClientHandler.this.i = SystemClock.uptimeMillis();
BleClientHandler.this.g.a(BleClientHandler.this.f.f());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
};
private ProtocolPacket f = new ProtocolPacket();
private long i = SystemClock.uptimeMillis();
private long j = this.i;
private BleDeviceProcessThread g = new BleDeviceProcessThread(this);
public interface ClientCallback {
void a(String str);
void a(String str, byte b, byte[] bArr, int i);
}
public BleClientHandler(String str, BleGattClientProxy bleGattClientProxy, ClientCallback clientCallback) {
this.l = 0;
this.m = 0;
this.a = str;
this.b = bleGattClientProxy;
this.n = clientCallback;
this.l = 0;
this.m = 0;
this.g.start();
b();
}
@Override // java.lang.Thread, java.lang.Runnable
public void run() {
while (this.h) {
try {
if (this.c == null) {
this.c = this.b.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
}
if (this.d == null) {
this.d = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-1e4d-4bd9-ba61-23c647249616"));
}
if (this.e == null) {
this.e = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-8841-43f4-a8d4-ecbe34729bb3"));
}
} catch (Exception unused) {
this.h = false;
return;
}
}
}
public void c(byte[] bArr, int i) {
if (SystemClock.uptimeMillis() - this.j < 3000) {
return;
}
if (!this.k) {
this.i = SystemClock.uptimeMillis();
} else {
a(bArr, i);
this.l++;
}
}
public boolean d() {
return (this.h && isAlive() && (!(this.l > 3 || this.m > 6) || !this.k)) ? false : true;
}
public void b(byte[] bArr, int i) {
this.g.a(bArr, i);
}
public String a() {
return this.a;
}
public void b() {
if (this.c == null) {
BleGattClientProxy bleGattClientProxy = this.b;
if (bleGattClientProxy == null) {
return;
} else {
this.c = bleGattClientProxy.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
}
}
BluetoothGattService bluetoothGattService = this.c;
if (bluetoothGattService == null) {
Log.e(p, "gattWriteService is null");
return;
}
if (this.d == null) {
BleGattClientProxy bleGattClientProxy2 = this.b;
if (bleGattClientProxy2 == null || bluetoothGattService == null) {
return;
} else {
this.d = bleGattClientProxy2.discoverCharacteristic(bluetoothGattService, BleUuidUtils.str2uuid("49535343-1e4d-4bd9-ba61-23c647249616"));
}
}
BleGattClientProxy bleGattClientProxy3 = this.b;
if (bleGattClientProxy3 == null) {
return;
}
BluetoothGattCharacteristic bluetoothGattCharacteristic = this.d;
if (bluetoothGattCharacteristic == null) {
BleLiteLog.e(p, "registerCharacteristicNotification fail! Because gattNotifyCharacteristic is null! ");
} else {
bleGattClientProxy3.registerCharacteristicNotification(bluetoothGattCharacteristic, this.o);
}
}
public void a(boolean z) {
this.k = z;
}
@Override // com.ubtrobot.jimu.bluetooth.ble.BleDeviceProcessThread.Callback
public synchronized void a(byte[] bArr, int i) {
try {
Log.i("Handler-", "onSendData");
if (this.c == null) {
this.c = this.b.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
}
if (this.e == null) {
this.e = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-8841-43f4-a8d4-ecbe34729bb3"));
}
Log.i(p, "onSend:" + ByteHexHelper.a(bArr));
boolean writeCharacterisitcNoResponse2 = this.b.writeCharacterisitcNoResponse2(this.e, bArr);
this.j = SystemClock.uptimeMillis();
this.m = this.m + 1;
Log.i(p, "sendNormalCount: " + this.m + " writeSucc: " + writeCharacterisitcNoResponse2);
} catch (Exception e) {
e.printStackTrace();
c();
}
}
public void c() {
synchronized (this) {
try {
this.l = 0;
this.m = 0;
this.h = false;
if (this.g != null) {
this.g.a();
}
if (this.n != null) {
this.n.a(this.a);
}
this.b.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@@ -0,0 +1,115 @@
package com.ubtrobot.jimu.bluetooth.ble;
import android.os.SystemClock;
import android.util.Log;
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/* loaded from: classes2.dex */
public class BleDeviceProcessThread extends Thread {
private static final String d = BleDeviceProcessThread.class.getSimpleName();
private List<BUFFER_DATA> a;
private boolean b = true;
private Callback c;
public interface Callback {
void a(byte[] bArr, int i);
}
public BleDeviceProcessThread(Callback callback) {
this.c = callback;
new ProtocolPacket();
this.a = new ArrayList();
}
public void a(byte[] bArr, int i) {
synchronized (this) {
Log.i(d, "sendData");
BUFFER_DATA buffer_data = new BUFFER_DATA();
buffer_data.a = bArr;
buffer_data.b = i;
buffer_data.c = SystemClock.uptimeMillis();
buffer_data.d = 0;
this.a.add(buffer_data);
if (this.c != null) {
this.c.a(bArr, i);
}
}
}
public void b(byte[] bArr, int i) {
synchronized (this) {
if (this.c != null) {
this.c.a(bArr, i);
}
}
}
@Override // java.lang.Thread, java.lang.Runnable
public void run() {
while (this.b) {
synchronized (this) {
BUFFER_DATA buffer_data = null;
Iterator<BUFFER_DATA> it = this.a.iterator();
while (true) {
if (!it.hasNext()) {
break;
}
BUFFER_DATA next = it.next();
if (SystemClock.uptimeMillis() - next.c >= 10000) {
if (next.d >= 2) {
buffer_data = next;
break;
} else {
b(next.a, next.b);
next.c = SystemClock.uptimeMillis();
next.d++;
}
}
}
if (buffer_data != null) {
this.a.remove(buffer_data);
} else {
try {
Thread.sleep(100L);
} catch (InterruptedException unused) {
this.b = false;
Thread.currentThread().interrupt();
return;
}
}
}
}
}
public void a() {
synchronized (this) {
this.b = false;
this.a.clear();
}
}
public void a(byte b) {
synchronized (this) {
ProtocolPacket protocolPacket = new ProtocolPacket();
BUFFER_DATA buffer_data = null;
Iterator<BUFFER_DATA> it = this.a.iterator();
while (true) {
if (!it.hasNext()) {
break;
}
BUFFER_DATA next = it.next();
protocolPacket.b(next.a);
if (protocolPacket.f() == b) {
buffer_data = next;
break;
}
}
if (buffer_data != null) {
this.a.remove(buffer_data);
}
}
}
}

View File

@@ -0,0 +1,199 @@
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);
}
}
}

View File

@@ -0,0 +1,174 @@
package com.ubtrobot.jimu.bluetooth.ble;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
import com.afunx.ble.blelitelib.proxy.BleGattClientProxyImpl;
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
import com.ubtrobot.jimu.bluetooth.ble.BleClientHandler;
import com.ubtrobot.jimu.bluetooth.proxy.Callback;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/* loaded from: classes2.dex */
public class BleManager extends Thread implements BleClientHandler.ClientCallback {
private static final String f = BleManager.class.getSimpleName();
private boolean a = true;
private BleGattClientProxy b;
private Callback c;
private List<BleClientHandler> d;
private Handler e;
public BleManager(Context context, Callback callback) {
new ArrayList();
new ArrayList();
this.d = new ArrayList();
this.e = new Handler(Looper.getMainLooper());
this.b = new BleGattClientProxyImpl(context.getApplicationContext());
this.c = callback;
}
public synchronized void d(final String str) {
if (TextUtils.isEmpty(str)) {
return;
}
Log.i(f, "connect: mac: " + str);
new Thread(new Runnable() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleManager.1
@Override // java.lang.Runnable
public void run() {
final boolean connect = BleManager.this.b.connect(str, 30000L);
BleManager.this.b(str);
BleManager.this.e.post(new Runnable() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleManager.1.1
@Override // java.lang.Runnable
public void run() {
BleManager.this.c.a(connect, str);
}
});
}
}).start();
}
@Override // java.lang.Thread, java.lang.Runnable
public void run() {
BleClientHandler bleClientHandler;
while (this.a) {
synchronized (this) {
Iterator<BleClientHandler> it = this.d.iterator();
while (true) {
if (!it.hasNext()) {
bleClientHandler = null;
break;
}
bleClientHandler = it.next();
if (bleClientHandler.d()) {
Log.i(f, "tryToReleaseConnection");
break;
}
ProtocolPacket protocolPacket = new ProtocolPacket();
protocolPacket.a((byte) 3);
protocolPacket.a((byte[]) null);
protocolPacket.a(0);
byte[] b = protocolPacket.b();
bleClientHandler.c(b, b.length);
}
if (bleClientHandler != null) {
String a = bleClientHandler.a();
bleClientHandler.c();
this.d.remove(bleClientHandler);
if (this.c != null) {
this.c.b(a);
}
} else {
try {
Thread.sleep(300L);
} catch (InterruptedException unused) {
this.a = false;
Thread.currentThread().interrupt();
return;
}
}
}
}
}
public synchronized void a() {
if (this.d != null) {
Iterator<BleClientHandler> it = this.d.iterator();
while (it.hasNext()) {
it.next().c();
}
this.d.clear();
}
}
public void b() {
this.a = true;
start();
}
public synchronized BleClientHandler c(String str) {
BleClientHandler bleClientHandler;
bleClientHandler = null;
Iterator<BleClientHandler> it = this.d.iterator();
while (true) {
if (!it.hasNext()) {
break;
}
BleClientHandler next = it.next();
if (next.a().equals(str)) {
bleClientHandler = next;
break;
}
}
return bleClientHandler;
}
public synchronized void b(String str, byte b, byte[] bArr, int i) {
ProtocolPacket protocolPacket = new ProtocolPacket();
protocolPacket.a(b);
protocolPacket.a(bArr);
protocolPacket.a(i);
byte[] b2 = protocolPacket.b();
BleClientHandler c = c(str);
if (c != null) {
c.b(b2, b2.length);
}
}
public void a(String str, boolean z) {
BleClientHandler c = c(str);
if (c != null) {
c.a(z);
}
}
@Override // com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.ClientCallback
public synchronized void a(String str, byte b, byte[] bArr, int i) {
if (this.c != null) {
this.c.b(str, b, bArr, i);
}
}
@Override // com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.ClientCallback
public synchronized void a(String str) {
if (this.c != null) {
this.c.b(str);
}
}
public synchronized void b(String str) {
if (c(str) != null) {
return;
}
if (this.b == null) {
return;
}
BleClientHandler bleClientHandler = new BleClientHandler(str, this.b, this);
this.d.add(bleClientHandler);
bleClientHandler.start();
}
}