package com.ubtrobot.jimu.bluetooth.base; import android.bluetooth.BluetoothSocket; import android.os.SystemClock; import android.util.Log; import com.ijm.dataencryption.de.DataDecryptTool; import com.ubtrobot.jimu.bluetooth.base.DeviceProcessThread; import com.ubtrobot.jimu.bluetooth.utils.ByteHexHelper; import com.ubtrobot.log.ALog; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /* loaded from: classes2.dex */ public class BlueToothClientHandler extends Thread implements DeviceProcessThread.DeviceProcessThreadCallBack { private final String a; private final BluetoothSocket b; private final InputStream c; private final OutputStream d; private final ClentCallBack e; private ProtocolPacket g; private DeviceProcessThread k; private int l; private int m; private boolean f = true; private boolean j = true; private long h = SystemClock.uptimeMillis(); private long i = this.h; public interface ClentCallBack { void a(String str); void a(String str, byte b, byte[] bArr, int i); } public BlueToothClientHandler(String str, BluetoothSocket bluetoothSocket, ClentCallBack clentCallBack) { InputStream inputStream; this.l = 0; this.m = 0; this.a = str; this.b = bluetoothSocket; this.e = clentCallBack; OutputStream outputStream = null; try { inputStream = bluetoothSocket.getInputStream(); try { outputStream = bluetoothSocket.getOutputStream(); } catch (IOException e) { e = e; Log.e("BlueToothClientHandler", "temp sockets not created", e); this.c = inputStream; this.d = outputStream; this.l = 0; this.m = 0; this.g = new ProtocolPacket(); this.k = new DeviceProcessThread(this); this.k.start(); } } catch (IOException e2) { e = e2; inputStream = null; } this.c = inputStream; this.d = outputStream; this.l = 0; this.m = 0; this.g = new ProtocolPacket(); this.k = new DeviceProcessThread(this); this.k.start(); } public String a() { return this.a; } public void b(byte[] bArr, int i) { this.k.a(bArr, i); } public void c(byte[] bArr, int i) { if (SystemClock.uptimeMillis() - this.i < 3000) { return; } if (!this.j) { this.h = SystemClock.uptimeMillis(); } else { a(bArr, i); this.l++; } } @Override // java.lang.Thread, java.lang.Runnable public void run() { byte[] bArr = new byte[DataDecryptTool.DECRYPT_SP_FILE]; while (this.f) { try { int read = this.c.read(bArr); if (read > 0) { byte[] bArr2 = new byte[read]; System.arraycopy(bArr, 0, bArr2, 0, read); ALog.a("DV_READSTATUS").d("Receive: param=" + ByteHexHelper.a(bArr2)); for (int i = 0; i < read; i++) { if (this.g.b(bArr[i])) { if (this.e != null) { this.g.a(this.g.g().length); this.e.a(this.a, this.g.f(), this.g.g(), this.g.a()); } this.l = 0; this.m = 0; this.h = SystemClock.uptimeMillis(); this.k.a(this.g.f()); } } } } catch (IOException e) { Log.e("BlueToothClientHandler", "read form bt socket fail", e); return; } catch (Exception unused) { this.f = false; return; } } } public void a(boolean z) { this.j = z; } public void b() { synchronized (this) { try { this.l = 0; this.m = 0; this.f = false; if (this.k != null) { this.k.a(); } if (this.c != null) { this.c.close(); } if (this.d != null) { this.d.close(); } if (this.b != null) { this.b.close(); } if (this.e != null) { this.e.a(this.a); } } catch (IOException e) { e.printStackTrace(); } } } @Override // com.ubtrobot.jimu.bluetooth.base.DeviceProcessThread.DeviceProcessThreadCallBack public synchronized void a(byte[] bArr, int i) { try { try { ALog.a("DV_READSTATUS").d("SendData=" + ByteHexHelper.a(bArr)); this.d.write(bArr, 0, i); this.i = SystemClock.uptimeMillis(); this.m = this.m + 1; } catch (Exception e) { ALog.a("BlueToothClientHandler").d("Send bluetooth data to socket fail! Close socket!", e); b(); } } catch (IOException e2) { ALog.a("BlueToothClientHandler").d("Send bluetooth data to socket fail! Close socket!", e2); b(); } } public boolean c() { return (this.f && isAlive() && (!(this.l > 3 || this.m > 6) || !this.j)) ? false : true; } }