393 lines
15 KiB
Java
393 lines
15 KiB
Java
package com.ubtrobot.jimu.connection;
|
|
|
|
import android.bluetooth.BluetoothManager;
|
|
import android.content.Context;
|
|
import android.os.Handler;
|
|
import android.os.HandlerThread;
|
|
import android.os.Looper;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
import com.ubtrobot.jimu.bluetooth.ConnectCallback;
|
|
import com.ubtrobot.jimu.bluetooth.ConnectionState;
|
|
import com.ubtrobot.jimu.bluetooth.ConnectionStateListener;
|
|
import com.ubtrobot.jimu.bluetooth.DataReceiveListener;
|
|
import com.ubtrobot.jimu.bluetooth.JimuBluetoothManager;
|
|
import com.ubtrobot.jimu.bluetooth.base.IPacket;
|
|
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
|
|
import com.ubtrobot.log.ALog;
|
|
import java.util.ArrayDeque;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public class RobotConnection {
|
|
private static final String p = "RobotConnection";
|
|
private Context a;
|
|
private JimuBluetoothManager b;
|
|
private Handler d;
|
|
private String g;
|
|
private IPacket h;
|
|
protected Handler l;
|
|
private final ArrayDeque<IPacket> e = new ArrayDeque<>();
|
|
private ExecutorService f = Executors.newSingleThreadExecutor();
|
|
private Object i = new Object();
|
|
private DataReceiveListener j = new DataReceiveListener() { // from class: com.ubtrobot.jimu.connection.RobotConnection.1
|
|
@Override // com.ubtrobot.jimu.bluetooth.DataReceiveListener
|
|
public void onDataAvailable(String str, byte b, byte[] bArr) {
|
|
ProtocolPacket protocolPacket = new ProtocolPacket();
|
|
protocolPacket.a(b);
|
|
protocolPacket.a(bArr);
|
|
protocolPacket.a(str);
|
|
if (protocolPacket.isValid()) {
|
|
RobotConnection.this.a(str, protocolPacket);
|
|
} else {
|
|
Log.e(RobotConnection.p, "Receive an invalid length byte array.");
|
|
}
|
|
}
|
|
};
|
|
protected final HashMap<Integer, WrapperResponseCallback> k = new HashMap<>();
|
|
private volatile boolean m = false;
|
|
private Runnable n = new Runnable() { // from class: com.ubtrobot.jimu.connection.RobotConnection.4
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
RobotConnection.this.e();
|
|
}
|
|
};
|
|
private List<PacketsReceiveListener> o = new ArrayList();
|
|
private HandlerThread c = new HandlerThread("BtSendDataThread");
|
|
|
|
public class WrapperResponseCallback implements ResponseCallback {
|
|
private final IPacket a;
|
|
private final ResponseCallback b;
|
|
private final Runnable c;
|
|
private int d = 1;
|
|
private volatile boolean e;
|
|
|
|
WrapperResponseCallback(ResponseCallback responseCallback, IPacket iPacket, Handler handler) {
|
|
this.b = responseCallback;
|
|
this.a = iPacket;
|
|
RobotConnection.this.d = handler;
|
|
this.c = new Runnable(RobotConnection.this) { // from class: com.ubtrobot.jimu.connection.RobotConnection.WrapperResponseCallback.1
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
ALog.a(RobotConnection.p).d("response timeout! packet:" + WrapperResponseCallback.this.a.getId());
|
|
WrapperResponseCallback.this.e = true;
|
|
RobotConnection.this.m = false;
|
|
RobotConnection.this.f.execute(RobotConnection.this.n);
|
|
WrapperResponseCallback wrapperResponseCallback = WrapperResponseCallback.this;
|
|
wrapperResponseCallback.a(wrapperResponseCallback.a, new RequestException(-3, "Timeout."));
|
|
}
|
|
};
|
|
}
|
|
|
|
static /* synthetic */ int e(WrapperResponseCallback wrapperResponseCallback) {
|
|
int i = wrapperResponseCallback.d;
|
|
wrapperResponseCallback.d = i - 1;
|
|
return i;
|
|
}
|
|
|
|
public void a(int i) {
|
|
if (i < 1) {
|
|
i = 1;
|
|
}
|
|
this.d = i;
|
|
}
|
|
|
|
void a() {
|
|
byte f = this.a.f();
|
|
RobotConnection.this.d.postDelayed(this.c, f != 8 ? (f == 26 || f == 35 || f == 128) ? 3000L : 2000L : 6000L);
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket, final IPacket iPacket2) {
|
|
RobotConnection.this.d.post(new Runnable() { // from class: com.ubtrobot.jimu.connection.RobotConnection.WrapperResponseCallback.2
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
synchronized (RobotConnection.this.k) {
|
|
WrapperResponseCallback.e(WrapperResponseCallback.this);
|
|
if (WrapperResponseCallback.this.d <= 0) {
|
|
RobotConnection.this.k.remove(Integer.valueOf(WrapperResponseCallback.this.a.getId()));
|
|
}
|
|
}
|
|
WrapperResponseCallback.this.b.a(WrapperResponseCallback.this.a, iPacket2);
|
|
RobotConnection.this.d.removeCallbacks(WrapperResponseCallback.this.c);
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket, final RequestException requestException) {
|
|
RobotConnection.this.d.post(new Runnable() { // from class: com.ubtrobot.jimu.connection.RobotConnection.WrapperResponseCallback.3
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
synchronized (RobotConnection.this.k) {
|
|
WrapperResponseCallback.this.d = 0;
|
|
RobotConnection.this.k.remove(Integer.valueOf(WrapperResponseCallback.this.a.getId()));
|
|
}
|
|
WrapperResponseCallback.this.b.a(WrapperResponseCallback.this.a, requestException);
|
|
RobotConnection.this.d.removeCallbacks(WrapperResponseCallback.this.c);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public RobotConnection(Context context) {
|
|
this.c.start();
|
|
this.d = new Handler(this.c.getLooper());
|
|
this.a = context.getApplicationContext();
|
|
this.l = new Handler(context.getMainLooper());
|
|
this.b = JimuBluetoothManager.d();
|
|
if (!this.b.supportsBluetooth(this.a)) {
|
|
Log.e(p, "Initialize Bluetooth fail!");
|
|
} else if (((BluetoothManager) this.a.getSystemService("bluetooth")).getAdapter() == null) {
|
|
Toast.makeText(this.a, "请检测设备是否支持蓝牙", 0).show();
|
|
} else {
|
|
this.b.a(this.j);
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void e() {
|
|
IPacket poll;
|
|
if (ConnectionState.STATE_CONNECTED != a(this.g)) {
|
|
Log.w(p, "Robot not connect");
|
|
return;
|
|
}
|
|
if (this.m) {
|
|
ALog.a(p).d("is Sending, send nothing and return");
|
|
return;
|
|
}
|
|
synchronized (this.e) {
|
|
poll = this.e.poll();
|
|
}
|
|
if (poll == null) {
|
|
return;
|
|
}
|
|
synchronized (this.i) {
|
|
this.h = poll;
|
|
}
|
|
synchronized (this.k) {
|
|
WrapperResponseCallback wrapperResponseCallback = this.k.get(Integer.valueOf(poll.getId()));
|
|
if (wrapperResponseCallback == null) {
|
|
Log.e(p, "No responseCallback");
|
|
return;
|
|
}
|
|
wrapperResponseCallback.a();
|
|
this.m = true;
|
|
this.b.a(b(), poll.f(), poll.g());
|
|
}
|
|
}
|
|
|
|
private void d() {
|
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
|
throw new SyncCallOnMainThreadException();
|
|
}
|
|
}
|
|
|
|
public String b() {
|
|
return this.g;
|
|
}
|
|
|
|
public void b(ConnectionStateListener connectionStateListener) {
|
|
this.b.b(connectionStateListener);
|
|
}
|
|
|
|
private void b(IPacket iPacket, ResponseCallback responseCallback, int i) {
|
|
if (iPacket != null && iPacket.isValid()) {
|
|
if (ConnectionState.STATE_CONNECTED != a(this.g)) {
|
|
responseCallback.a(iPacket, new RequestException(-1, "ERR_CODE_BT_NOT_CONNECTED"));
|
|
return;
|
|
}
|
|
WrapperResponseCallback wrapperResponseCallback = new WrapperResponseCallback(responseCallback, iPacket, this.l);
|
|
wrapperResponseCallback.a(i);
|
|
synchronized (this.k) {
|
|
this.k.put(Integer.valueOf(iPacket.getId()), wrapperResponseCallback);
|
|
}
|
|
b(iPacket);
|
|
return;
|
|
}
|
|
responseCallback.a(iPacket, new RequestException(-4, "ERR_CODE_CMD_INVALID"));
|
|
}
|
|
|
|
public void a(String str, ConnectCallback connectCallback) {
|
|
this.g = str;
|
|
this.b.connectWithCallback(str, connectCallback);
|
|
}
|
|
|
|
public void a() {
|
|
this.b.disconnect();
|
|
}
|
|
|
|
public synchronized ConnectionState a(String str) {
|
|
return this.b.getConnectionState(str);
|
|
}
|
|
|
|
public void a(ConnectionStateListener connectionStateListener) {
|
|
this.b.a(connectionStateListener);
|
|
}
|
|
|
|
public void a(IPacket iPacket, final ResponseCallback responseCallback, int i) {
|
|
d();
|
|
final boolean[] zArr = new boolean[1];
|
|
final boolean[] zArr2 = new boolean[1];
|
|
final ProtocolPacket[] protocolPacketArr = new ProtocolPacket[1];
|
|
final RequestException[] requestExceptionArr = new RequestException[1];
|
|
b(iPacket, new ResponseCallback(this) { // from class: com.ubtrobot.jimu.connection.RobotConnection.2
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket2, IPacket iPacket3) {
|
|
synchronized (zArr) {
|
|
zArr2[0] = true;
|
|
protocolPacketArr[0] = iPacket3;
|
|
responseCallback.a(iPacket2, iPacket3);
|
|
zArr[0] = true;
|
|
ALog.a(RobotConnection.p).d("responded notify");
|
|
zArr.notifyAll();
|
|
}
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket2, RequestException requestException) {
|
|
synchronized (zArr) {
|
|
requestExceptionArr[0] = requestException;
|
|
zArr[0] = true;
|
|
ALog.a(RobotConnection.p).d("responded notify");
|
|
zArr.notifyAll();
|
|
}
|
|
}
|
|
}, i);
|
|
synchronized (zArr) {
|
|
while (!zArr[0]) {
|
|
try {
|
|
ALog.a(p).d("responded wait");
|
|
zArr.wait();
|
|
break;
|
|
} catch (InterruptedException e) {
|
|
ALog.a(p).c("Interrupted by someone when sync doRequest. req=" + iPacket + "Ignore it.", e);
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
}
|
|
if (requestExceptionArr[0] != null) {
|
|
responseCallback.a(iPacket, requestExceptionArr[0]);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void b(IPacket iPacket) {
|
|
synchronized (this.e) {
|
|
this.e.add(iPacket);
|
|
}
|
|
this.f.execute(this.n);
|
|
}
|
|
|
|
public void b(PacketsReceiveListener packetsReceiveListener) {
|
|
this.o.remove(packetsReceiveListener);
|
|
}
|
|
|
|
public IPacket a(IPacket iPacket) throws RequestException {
|
|
ProtocolPacket protocolPacket;
|
|
d();
|
|
final boolean[] zArr = new boolean[1];
|
|
final boolean[] zArr2 = new boolean[1];
|
|
final ProtocolPacket[] protocolPacketArr = new ProtocolPacket[1];
|
|
final RequestException[] requestExceptionArr = new RequestException[1];
|
|
a(iPacket, new ResponseCallback(this) { // from class: com.ubtrobot.jimu.connection.RobotConnection.3
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket2, IPacket iPacket3) {
|
|
synchronized (zArr) {
|
|
zArr2[0] = true;
|
|
protocolPacketArr[0] = iPacket3;
|
|
zArr[0] = true;
|
|
ALog.a(RobotConnection.p).d("responded notify");
|
|
zArr.notifyAll();
|
|
}
|
|
}
|
|
|
|
@Override // com.ubtrobot.jimu.connection.ResponseCallback
|
|
public void a(IPacket iPacket2, RequestException requestException) {
|
|
synchronized (zArr) {
|
|
requestExceptionArr[0] = requestException;
|
|
zArr[0] = true;
|
|
ALog.a(RobotConnection.p).d("responded notify");
|
|
zArr.notifyAll();
|
|
}
|
|
}
|
|
});
|
|
synchronized (zArr) {
|
|
while (!zArr[0]) {
|
|
try {
|
|
ALog.a(p).d("responded wait");
|
|
zArr.wait();
|
|
break;
|
|
} catch (InterruptedException e) {
|
|
ALog.a(p).c("Interrupted by someone when sync doRequest. req=" + iPacket + "Ignore it.", e);
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
}
|
|
if (requestExceptionArr[0] == null) {
|
|
protocolPacket = protocolPacketArr[0];
|
|
} else {
|
|
throw requestExceptionArr[0];
|
|
}
|
|
}
|
|
return protocolPacket;
|
|
}
|
|
|
|
private void a(IPacket iPacket, ResponseCallback responseCallback) {
|
|
b(iPacket, responseCallback, 1);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void a(String str, ProtocolPacket protocolPacket) {
|
|
if (protocolPacket.isValid()) {
|
|
if (protocolPacket.f() == 3) {
|
|
return;
|
|
}
|
|
if (protocolPacket.getId() == this.h.getId()) {
|
|
this.m = false;
|
|
this.f.execute(this.n);
|
|
synchronized (this.i) {
|
|
if (protocolPacket.getId() != this.h.getId()) {
|
|
Log.w(p, "Error response! cmd:" + protocolPacket + " res:" + protocolPacket);
|
|
} else {
|
|
ProtocolPacket protocolPacket2 = (ProtocolPacket) protocolPacket.clone();
|
|
synchronized (this.k) {
|
|
WrapperResponseCallback wrapperResponseCallback = this.k.get(Integer.valueOf(protocolPacket.getId()));
|
|
if (wrapperResponseCallback == null) {
|
|
Log.e(p, "No responseCallback");
|
|
} else {
|
|
wrapperResponseCallback.a(this.h, protocolPacket2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Iterator<PacketsReceiveListener> it = this.o.iterator();
|
|
while (it.hasNext()) {
|
|
it.next().a(str, protocolPacket);
|
|
}
|
|
return;
|
|
}
|
|
Log.i(p, "Receive an packet of not response. packet:" + protocolPacket);
|
|
}
|
|
|
|
public void a(PacketsReceiveListener packetsReceiveListener) {
|
|
if (this.o.contains(packetsReceiveListener)) {
|
|
return;
|
|
}
|
|
this.o.add(packetsReceiveListener);
|
|
}
|
|
|
|
public void a(boolean z) {
|
|
String str = this.g;
|
|
if (str == null) {
|
|
Log.e(p, "enableHeartbeat fail, connected device is null");
|
|
} else {
|
|
this.b.a(str, z);
|
|
}
|
|
}
|
|
}
|