package com.ubtrobot.jimu.bluetooth; import android.content.Context; import android.util.Log; import com.ubtrobot.jimu.bluetooth.base.discover.ScanResult; import com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter; import com.ubtrobot.jimu.bluetooth.proxy.BluetoothProxy; import com.ubtrobot.jimu.bluetooth.proxy.DeviceListener; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; /* loaded from: classes2.dex */ public class JimuBluetoothManager { private static final String k = "JimuBluetoothManager"; /* renamed from: a */ private Context context; /* renamed from: b */ private BluetoothProxy bluetoothProxy; /* renamed from: c */ private List connectionStateListeners; /* renamed from: d */ private List dataReceiveListeners; /* renamed from: e */ private HashMap connectionCallbacks; /* renamed from: f */ private HashMap connectionStates; /* renamed from: g */ private boolean supportsBluetooth; /* renamed from: h */ private String macAddress; private String i; /* renamed from: j */ private DeviceListener deviceListener; private static class Holder { private static final JimuBluetoothManager a = new JimuBluetoothManager(); } private JimuBluetoothManager() { this.connectionStateListeners = new ArrayList(); this.dataReceiveListeners = new ArrayList(); this.connectionCallbacks = new HashMap<>(); this.connectionStates = new HashMap<>(); this.supportsBluetooth = false; this.macAddress = null; this.i = null; this.deviceListener = new DeviceListener() { // from class: com.ubtrobot.jimu.bluetooth.JimuBluetoothManager.1 @Override // com.ubtrobot.jimu.bluetooth.proxy.DeviceListener /* renamed from: a */ public void onConnectState(boolean isSuccess, String macAddress) { ConnectionState connectionState; Log.d(JimuBluetoothManager.k, "onConnectState isSuccess:" + isSuccess + " mac:" + macAddress); if (macAddress == null) { Log.e(JimuBluetoothManager.k, "onConnectState isSuccess:" + isSuccess + " mac:" + macAddress); if (JimuBluetoothManager.this.i == null) { return; } else { macAddress = JimuBluetoothManager.this.i; } } if (isSuccess) { connectionState = ConnectionState.STATE_CONNECTED; JimuBluetoothManager.this.macAddress = macAddress; } else { connectionState = ConnectionState.STATE_DISCONNECTED; if (macAddress.equals(JimuBluetoothManager.this.macAddress)) { JimuBluetoothManager.this.macAddress = null; } } JimuBluetoothManager.this.a(macAddress, connectionState); ConnectCallback connectCallback = (ConnectCallback) JimuBluetoothManager.this.connectionCallbacks.get(macAddress); if (connectCallback != null) { if (isSuccess) { connectCallback.onSuccess(); } else { connectCallback.a(-1, "connect fail!"); } } } @Override // com.ubtrobot.jimu.bluetooth.proxy.DeviceListener public String b(String str) { return null; } @Override // com.ubtrobot.jimu.bluetooth.proxy.DeviceListener public void a(String str, byte b, byte[] bArr, int i) { Iterator it = JimuBluetoothManager.this.dataReceiveListeners.iterator(); while (it.hasNext()) { ((DataReceiveListener) it.next()).onDataAvailable(str, b, bArr); } } @Override // com.ubtrobot.jimu.bluetooth.proxy.DeviceListener /* renamed from: a */ public void disconnect(String macAddress) { JimuBluetoothManager.this.a(macAddress, ConnectionState.STATE_DISCONNECTED); if (macAddress.equals(JimuBluetoothManager.this.macAddress)) { JimuBluetoothManager.this.macAddress = null; } } }; this.bluetoothProxy = BluetoothProxy.c(); } public static JimuBluetoothManager d() { return Holder.a; } public void b(ConnectionStateListener connectionStateListener) { synchronized (this.connectionStateListeners) { this.connectionStateListeners.remove(connectionStateListener); } } /* JADX INFO: Access modifiers changed from: private */ public void a(String macAddress, ConnectionState connectionState) { synchronized (this.connectionStateListeners) { Iterator it = this.connectionStateListeners.iterator(); while (true) { if (!it.hasNext()) { break; } ConnectionStateListener next = it.next(); ConnectionState connectionState2 = this.connectionStates.get(macAddress); if (connectionState2 == null) { connectionState2 = ConnectionState.STATE_DISCONNECTED; } if (connectionState2 == connectionState) { Log.w(k, "new connection state is same to old state, not notify connectionstate change."); break; } next.onConnectionStateChange(macAddress, connectionState); } this.connectionStates.put(macAddress, connectionState); } } /* renamed from: b */ public void removeDataReceiveListener(DataReceiveListener dataReceiveListener) { this.dataReceiveListeners.remove(dataReceiveListener); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: b */ public boolean isJimu(String str) { String lowerCase = str.toLowerCase(); if (lowerCase.startsWith("My_Jimu_".toLowerCase())) { return true; } return lowerCase.startsWith("Jimu".toLowerCase()) && !lowerCase.startsWith("Jimuspk".toLowerCase()); } public String b() { return this.macAddress; } /* renamed from: a */ public boolean supportsBluetooth(Context context) { this.context = context.getApplicationContext(); this.supportsBluetooth = this.bluetoothProxy.a(this.context, this.deviceListener); boolean supportsBluetooth = this.supportsBluetooth; if (supportsBluetooth) { return supportsBluetooth; } Log.e(k, "Device not support bluetooth or not bluetooth related permissions"); return false; } /* renamed from: a */ public void connectWithCallback(String macAddress, ConnectCallback connectCallback) { this.i = macAddress; this.connectionStates.get(macAddress); this.connectionCallbacks.put(macAddress, connectCallback); this.connectionStates.put(macAddress, ConnectionState.STATE_CONNECTING); this.bluetoothProxy.a(macAddress); } /* renamed from: a */ public void disconnect() { Log.i(k, "Disconnect buletooth."); this.macAddress = null; this.bluetoothProxy.a(); this.connectionStates.clear(); } public void a(ConnectionStateListener connectionStateListener) { synchronized (this.connectionStateListeners) { if (!this.connectionStateListeners.contains(connectionStateListener)) { this.connectionStateListeners.add(connectionStateListener); } } } /* renamed from: a */ public ConnectionState getConnectionState(String macAddress) { if (macAddress == null) { return ConnectionState.STATE_DISCONNECTED; } ConnectionState connectionState = this.connectionStates.get(macAddress); return connectionState == null ? ConnectionState.STATE_DISCONNECTED : connectionState; } public synchronized void a(String str, int i, byte[] bArr) { this.bluetoothProxy.a(str, (byte) i, bArr, bArr.length); } public void a(DataReceiveListener dataReceiveListener) { if (this.dataReceiveListeners.contains(dataReceiveListener)) { return; } this.dataReceiveListeners.add(dataReceiveListener); } public Cancellable a(final ScannedHubEmitter scannedHubEmitter, int i) { if (!this.supportsBluetooth) { Log.e(k, "Bluetooth is not support!"); scannedHubEmitter.a(4, "Scan fail as bluetooth is not support"); return null; } return this.bluetoothProxy.a(new ScannedHubEmitter() { // from class: com.ubtrobot.jimu.bluetooth.JimuBluetoothManager.2 @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a(ScanResult scanResult) { String name; if (scanResult == null || scanResult.a() == null || (name = scanResult.a().getName()) == null || !JimuBluetoothManager.this.isJimu(name)) { return; } scannedHubEmitter.a(scanResult); } @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a(int i2, String str) { scannedHubEmitter.a(i2, str); } @Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter public void a() { scannedHubEmitter.a(); } }, i); } public void a(String str, boolean z) { this.bluetoothProxy.a(str, z); } }