package com.ubtrobot.jimu.bluetooth.base; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.os.SystemClock; import com.ubtrobot.jimu.bluetooth.base.BlueToothClientHandler; import com.ubtrobot.jimu.bluetooth.base.BluetoothUtil; 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 BlueToothManager extends Thread implements BlueToothClientHandler.ClentCallBack, BluetoothUtil.BluetoothUtilCallBack { private Callback d; private BluetoothUtil e; private boolean a = false; private boolean c = true; private List b = new ArrayList(); public BlueToothManager(Context context, Callback callback) { this.d = callback; this.e = new BluetoothUtil(context, this); SystemClock.uptimeMillis(); } public void a(BluetoothDevice bluetoothDevice) { b(); this.e.a(bluetoothDevice); } @Override // com.ubtrobot.jimu.bluetooth.base.BluetoothUtil.BluetoothUtilCallBack public void a(String str, int i) { } public synchronized BlueToothClientHandler b(String str) { BlueToothClientHandler blueToothClientHandler; blueToothClientHandler = null; Iterator it = this.b.iterator(); while (true) { if (!it.hasNext()) { break; } BlueToothClientHandler next = it.next(); if (next.a().equals(str)) { blueToothClientHandler = next; break; } } return blueToothClientHandler; } public void c() { this.c = true; this.e.a(this.a); start(); } @Override // java.lang.Thread, java.lang.Runnable public void run() { BlueToothClientHandler blueToothClientHandler; while (this.c) { synchronized (this) { Iterator it = this.b.iterator(); while (true) { if (!it.hasNext()) { blueToothClientHandler = null; break; } blueToothClientHandler = it.next(); if (blueToothClientHandler.c()) { break; } ProtocolPacket protocolPacket = new ProtocolPacket(); protocolPacket.a((byte) 3); protocolPacket.a((byte[]) null); protocolPacket.a(0); byte[] b = protocolPacket.b(); blueToothClientHandler.c(b, b.length); } if (blueToothClientHandler != null) { String a = blueToothClientHandler.a(); blueToothClientHandler.b(); this.b.remove(blueToothClientHandler); if (this.d != null) { this.d.b(a); } } else { try { Thread.sleep(300L); } catch (InterruptedException unused) { this.c = false; Thread.currentThread().interrupt(); return; } } } } } public void a(String str, boolean z) { BlueToothClientHandler b = b(str); if (b != null) { b.a(z); } } public synchronized void b(String str, BluetoothSocket bluetoothSocket) { if (b(str) != null) { return; } BlueToothClientHandler blueToothClientHandler = new BlueToothClientHandler(str, bluetoothSocket, this); this.b.add(blueToothClientHandler); blueToothClientHandler.start(); } @Override // com.ubtrobot.jimu.bluetooth.base.BlueToothClientHandler.ClentCallBack public synchronized void a(String str, byte b, byte[] bArr, int i) { if (this.d != null) { this.d.b(str, b, bArr, i); } } @Override // com.ubtrobot.jimu.bluetooth.base.BlueToothClientHandler.ClentCallBack public synchronized void a(String str) { if (this.d != null) { this.d.b(str); } } public synchronized void b() { if (this.b != null) { Iterator it = this.b.iterator(); while (it.hasNext()) { it.next().b(); } this.b.clear(); } } @Override // com.ubtrobot.jimu.bluetooth.base.BluetoothUtil.BluetoothUtilCallBack public synchronized void a(String str, BluetoothSocket bluetoothSocket) { b(str, bluetoothSocket); if (this.d != null) { this.d.a(true, str); } } @Override // com.ubtrobot.jimu.bluetooth.base.BluetoothUtil.BluetoothUtilCallBack public synchronized void a() { if (this.d != null) { this.d.a(false, null); } } 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(); BlueToothClientHandler b3 = b(str); if (b3 != null) { b3.b(b2, b2.length); } } }