175 lines
5.6 KiB
Java
175 lines
5.6 KiB
Java
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();
|
|
}
|
|
}
|