package com.ubtrobot.jimu.bluetooth.base; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.text.TextUtils; import android.util.Log; import com.ubtrobot.jimu.bluetooth.Cancellable; import com.ubtrobot.jimu.bluetooth.base.discover.ScanResult; import com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter; import com.ubtrobot.jimu.bluetooth.proxy.Callback; import com.ubtrobot.jimu.bluetooth.proxy.Device; import com.ubtrobot.jimu.bluetooth.proxy.DeviceListener; import com.ubtrobot.jimu.bluetooth.proxy.Proxy; import com.ubtrobot.log.ALog; import java.util.ArrayList; /* loaded from: classes2.dex */ public class BluetoothLib extends Callback implements Proxy { private static final String k = "BluetoothLib"; public static BluetoothLib l; public static BlueToothManager m; private BluetoothAdapter b; private Context c; private ArrayList d = new ArrayList<>(); private Boolean e; private DeviceListener f; private ScannedHubEmitter g; private Handler h; private Cancellable i; private final BroadcastReceiver j; public BluetoothLib() { new ArrayList(); this.e = false; new Runnable(this) { // from class: com.ubtrobot.jimu.bluetooth.base.BluetoothLib.1 @Override // java.lang.Runnable public void run() { } }; this.i = new Cancellable() { // from class: com.ubtrobot.jimu.bluetooth.base.BluetoothLib.3 @Override // com.ubtrobot.jimu.bluetooth.Cancellable public boolean cancel() { BluetoothLib.this.c(); BluetoothLib.this.h.removeCallbacksAndMessages(null); return BluetoothLib.this.b != null && BluetoothLib.this.b.isDiscovering(); } @Override // com.ubtrobot.jimu.bluetooth.Cancellable public boolean isCancelled() { return BluetoothLib.this.b != null && BluetoothLib.this.b.isDiscovering(); } }; this.j = new BroadcastReceiver() { // from class: com.ubtrobot.jimu.bluetooth.base.BluetoothLib.4 @Override // android.content.BroadcastReceiver public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (!"android.bluetooth.device.action.FOUND".equals(action)) { "android.bluetooth.adapter.action.DISCOVERY_FINISHED".equals(action); return; } BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); if (bluetoothDevice == null || intent.getExtras() == null) { return; } short s = intent.getExtras().getShort("android.bluetooth.device.extra.RSSI"); String replace = TextUtils.isEmpty(bluetoothDevice.getName()) ? "" : bluetoothDevice.getName().replace("\n", "").replace("\\n", ""); String str = replace + "\n" + bluetoothDevice.getAddress() + "\n" + ((int) s); if (BluetoothLib.this.c(str)) { return; } BluetoothLib.this.d.add(str); BluetoothLib.this.f.b(str); Proxy.a.add(new Device(bluetoothDevice.getAddress(), replace)); if (!TextUtils.isEmpty(bluetoothDevice.getName()) && bluetoothDevice.getName().toLowerCase().contains("jimu")) { ScanResult scanResult = new ScanResult(bluetoothDevice, s); if (BluetoothLib.this.g != null) { BluetoothLib.this.g.a(scanResult); } } } }; } public static synchronized BluetoothLib d() { BluetoothLib bluetoothLib; synchronized (BluetoothLib.class) { if (l == null) { synchronized (BluetoothLib.class) { if (l == null) { l = new BluetoothLib(); } } } bluetoothLib = l; } return bluetoothLib; } private void e() { if (this.e.booleanValue()) { return; } this.c.registerReceiver(this.j, new IntentFilter("android.bluetooth.device.action.FOUND")); this.c.registerReceiver(this.j, new IntentFilter("android.bluetooth.adapter.action.DISCOVERY_FINISHED")); this.e = true; } public void b() { ALog.a(k).d("startScan"); BluetoothAdapter bluetoothAdapter = this.b; if (bluetoothAdapter == null) { return; } try { if (bluetoothAdapter.isDiscovering()) { this.b.cancelDiscovery(); } this.d.clear(); this.b.startDiscovery(); } catch (Exception e) { Log.e(k, "Start scan fail", e); } } public void c() { BluetoothAdapter bluetoothAdapter = this.b; if (bluetoothAdapter == null || !bluetoothAdapter.isDiscovering()) { return; } this.b.cancelDiscovery(); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy public boolean a(Context context, DeviceListener deviceListener) { this.c = context; this.f = deviceListener; if (m == null) { m = new BlueToothManager(context, this); m.c(); } this.b = BluetoothAdapter.getDefaultAdapter(); if (this.b == null) { return false; } e(); Log.i(k, "BluetoothLib-init"); this.h = new Handler(context.getMainLooper()); return true; } /* JADX INFO: Access modifiers changed from: private */ public boolean c(String str) { for (int i = 0; i < this.d.size(); i++) { if (this.d.get(i).equals(str)) { return true; } } return false; } @Override // com.ubtrobot.jimu.bluetooth.proxy.Callback public void b(String str, byte b, byte[] bArr, int i) { this.f.a(str, b, bArr, i); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Callback public void b(String str) { this.f.disconnect(str); } public Cancellable a(final ScannedHubEmitter scannedHubEmitter, int i) { BluetoothAdapter bluetoothAdapter = this.b; if (bluetoothAdapter == null) { scannedHubEmitter.a(-2, "Device not support bluetooth!"); return null; } if (bluetoothAdapter.getState() != 12) { scannedHubEmitter.a(-3, "Bluetooth is not turn on!"); return null; } this.g = scannedHubEmitter; b(); this.h.postDelayed(new Runnable() { // from class: com.ubtrobot.jimu.bluetooth.base.BluetoothLib.2 @Override // java.lang.Runnable public void run() { BluetoothLib.this.c(); scannedHubEmitter.a(); } }, i); return this.i; } @Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy public void a(String str) { BluetoothAdapter bluetoothAdapter = this.b; if (bluetoothAdapter == null) { return; } if (bluetoothAdapter.isDiscovering()) { this.b.cancelDiscovery(); } BluetoothDevice remoteDevice = this.b.getRemoteDevice(str); m.b(); m.a(remoteDevice); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy public void a() { m.b(); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy public void a(String str, byte b, byte[] bArr, int i) { m.b(str, b, bArr, i); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy public void a(String str, boolean z) { m.a(str, z); } @Override // com.ubtrobot.jimu.bluetooth.proxy.Callback public void a(boolean z, String str) { this.f.onConnectState(z, str); } }