198 lines
7.4 KiB
Java
198 lines
7.4 KiB
Java
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();
|
|
}
|
|
}
|
|
}
|
|
}
|