Initial commit
This commit is contained in:
8
sources/com/ubtrobot/jimu/bluetooth/Cancellable.java
Normal file
8
sources/com/ubtrobot/jimu/bluetooth/Cancellable.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface Cancellable {
|
||||
boolean cancel();
|
||||
|
||||
boolean isCancelled();
|
||||
}
|
8
sources/com/ubtrobot/jimu/bluetooth/ConnectCallback.java
Normal file
8
sources/com/ubtrobot/jimu/bluetooth/ConnectCallback.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface ConnectCallback {
|
||||
void a(int i, String str);
|
||||
|
||||
void onSuccess();
|
||||
}
|
9
sources/com/ubtrobot/jimu/bluetooth/ConnectionState.java
Normal file
9
sources/com/ubtrobot/jimu/bluetooth/ConnectionState.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public enum ConnectionState {
|
||||
STATE_CONNECTING,
|
||||
STATE_CONNECTED,
|
||||
STATE_DISCONNECTING,
|
||||
STATE_DISCONNECTED
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface ConnectionStateListener {
|
||||
void onConnectionStateChange(String str, ConnectionState connectionState);
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface DataReceiveListener {
|
||||
void onDataAvailable(String str, byte b, byte[] bArr);
|
||||
}
|
257
sources/com/ubtrobot/jimu/bluetooth/JimuBluetoothManager.java
Normal file
257
sources/com/ubtrobot/jimu/bluetooth/JimuBluetoothManager.java
Normal file
@@ -0,0 +1,257 @@
|
||||
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<ConnectionStateListener> connectionStateListeners;
|
||||
|
||||
/* renamed from: d */
|
||||
private List<DataReceiveListener> dataReceiveListeners;
|
||||
|
||||
/* renamed from: e */
|
||||
private HashMap<String, ConnectCallback> connectionCallbacks;
|
||||
|
||||
/* renamed from: f */
|
||||
private HashMap<String, ConnectionState> 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<ConnectionStateListener> 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);
|
||||
}
|
||||
}
|
18
sources/com/ubtrobot/jimu/bluetooth/RobotWalkingType.java
Normal file
18
sources/com/ubtrobot/jimu/bluetooth/RobotWalkingType.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.ubtrobot.jimu.bluetooth;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public enum RobotWalkingType {
|
||||
DOUBLE_FEET("doubleFeet"),
|
||||
FOUR_FEET("fourFeet"),
|
||||
WHEEL_MODE("wheelMode");
|
||||
|
||||
private String type;
|
||||
|
||||
RobotWalkingType(String str) {
|
||||
this.type = str;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
}
|
13
sources/com/ubtrobot/jimu/bluetooth/base/BUFFER_DATA.java
Normal file
13
sources/com/ubtrobot/jimu/bluetooth/base/BUFFER_DATA.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
/* compiled from: DeviceProcessThread.java */
|
||||
/* loaded from: classes2.dex */
|
||||
class BUFFER_DATA {
|
||||
public byte[] a;
|
||||
public int b;
|
||||
public long c;
|
||||
public int d;
|
||||
|
||||
BUFFER_DATA() {
|
||||
}
|
||||
}
|
@@ -0,0 +1,177 @@
|
||||
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;
|
||||
}
|
||||
}
|
168
sources/com/ubtrobot/jimu/bluetooth/base/BlueToothManager.java
Normal file
168
sources/com/ubtrobot/jimu/bluetooth/base/BlueToothManager.java
Normal file
@@ -0,0 +1,168 @@
|
||||
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<BlueToothClientHandler> 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<BlueToothClientHandler> 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<BlueToothClientHandler> 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<BlueToothClientHandler> 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);
|
||||
}
|
||||
}
|
||||
}
|
231
sources/com/ubtrobot/jimu/bluetooth/base/BluetoothLib.java
Normal file
231
sources/com/ubtrobot/jimu/bluetooth/base/BluetoothLib.java
Normal file
@@ -0,0 +1,231 @@
|
||||
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<String> 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);
|
||||
}
|
||||
}
|
245
sources/com/ubtrobot/jimu/bluetooth/base/BluetoothUtil.java
Normal file
245
sources/com/ubtrobot/jimu/bluetooth/base/BluetoothUtil.java
Normal file
@@ -0,0 +1,245 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothServerSocket;
|
||||
import android.bluetooth.BluetoothSocket;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.log.ALog;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.UUID;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BluetoothUtil {
|
||||
private static final UUID i = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||||
private AcceptThread b;
|
||||
private ConnectThread c;
|
||||
private String e;
|
||||
private final BluetoothUtilCallBack g;
|
||||
private BluetoothDevice h;
|
||||
private boolean f = false;
|
||||
private final BluetoothAdapter a = BluetoothAdapter.getDefaultAdapter();
|
||||
private int d = 0;
|
||||
|
||||
private class AcceptThread extends Thread {
|
||||
private final BluetoothServerSocket a;
|
||||
private boolean b = true;
|
||||
|
||||
public AcceptThread() {
|
||||
BluetoothServerSocket bluetoothServerSocket;
|
||||
try {
|
||||
bluetoothServerSocket = BluetoothUtil.this.a.listenUsingRfcommWithServiceRecord("BluetoothChat", BluetoothUtil.i);
|
||||
} catch (IOException e) {
|
||||
Log.e("BluetoothChatService", "listen() failed", e);
|
||||
bluetoothServerSocket = null;
|
||||
}
|
||||
this.a = bluetoothServerSocket;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
ALog.a("BluetoothChatService").d("cancel " + this);
|
||||
try {
|
||||
this.b = false;
|
||||
this.a.close();
|
||||
} catch (IOException e) {
|
||||
ALog.a("BluetoothChatService").d("close() of server failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
ALog.a("BluetoothChatService").d("BEGIN mAcceptThread" + this);
|
||||
setName("AcceptThread");
|
||||
while (this.b) {
|
||||
try {
|
||||
BluetoothSocket accept = this.a.accept();
|
||||
if (accept != null) {
|
||||
BluetoothUtil.this.a(accept, accept.getRemoteDevice());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e("BluetoothChatService", "accept() failed", e);
|
||||
}
|
||||
}
|
||||
Log.i("BluetoothChatService", "END mAcceptThread");
|
||||
}
|
||||
}
|
||||
|
||||
class BluetoothThread extends Thread {
|
||||
BluetoothThread() {
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
if (BluetoothUtil.this.c != null) {
|
||||
BluetoothUtil.this.c.cancel();
|
||||
BluetoothUtil.this.c = null;
|
||||
try {
|
||||
Thread.sleep(500L);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
BluetoothUtil bluetoothUtil = BluetoothUtil.this;
|
||||
bluetoothUtil.c = bluetoothUtil.new ConnectThread(bluetoothUtil.h);
|
||||
BluetoothUtil.this.c.start();
|
||||
BluetoothUtil.this.a(2);
|
||||
}
|
||||
}
|
||||
|
||||
public interface BluetoothUtilCallBack {
|
||||
void a();
|
||||
|
||||
void a(String str, int i);
|
||||
|
||||
void a(String str, BluetoothSocket bluetoothSocket);
|
||||
}
|
||||
|
||||
private class ConnectThread extends Thread {
|
||||
private BluetoothSocket a;
|
||||
private final BluetoothDevice b;
|
||||
|
||||
@SuppressLint({"NewApi"})
|
||||
public ConnectThread(BluetoothDevice bluetoothDevice) {
|
||||
this.b = bluetoothDevice;
|
||||
BluetoothSocket bluetoothSocket = null;
|
||||
try {
|
||||
if (DeviceDependency.a()) {
|
||||
try {
|
||||
try {
|
||||
bluetoothSocket = (BluetoothSocket) bluetoothDevice.getClass().getMethod("createInsecureRfcommSocket", Integer.TYPE).invoke(bluetoothDevice, 6);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SecurityException e2) {
|
||||
e2.printStackTrace();
|
||||
} catch (InvocationTargetException e3) {
|
||||
e3.printStackTrace();
|
||||
}
|
||||
} catch (IllegalArgumentException e4) {
|
||||
e4.printStackTrace();
|
||||
} catch (NoSuchMethodException e5) {
|
||||
e5.printStackTrace();
|
||||
}
|
||||
} else if (DeviceDependency.b()) {
|
||||
bluetoothSocket = this.b.createRfcommSocketToServiceRecord(BluetoothUtil.i);
|
||||
} else {
|
||||
bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(BluetoothUtil.i);
|
||||
Log.v("BluetoothChatService", "createInsecureRfcommSocketToServiceRecord");
|
||||
}
|
||||
} catch (IOException e6) {
|
||||
Log.e("BluetoothChatService", "create() failed", e6);
|
||||
}
|
||||
this.a = bluetoothSocket;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
try {
|
||||
Log.e("zdy", "socket.close()");
|
||||
if (this.a != null) {
|
||||
this.a.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e("BluetoothChatService", "close() of connect socket failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
@SuppressLint({"NewApi"})
|
||||
public void run() {
|
||||
Log.i("BluetoothChatService", "BEGIN mConnectThread");
|
||||
setName("ConnectThread");
|
||||
if (this.a == null) {
|
||||
Log.w("BluetoothChatService", "get BluetoothSocket fail! Stop connect!");
|
||||
return;
|
||||
}
|
||||
BluetoothUtil.this.a.cancelDiscovery();
|
||||
try {
|
||||
Log.e("zdy", "socket.run()");
|
||||
this.a.connect();
|
||||
synchronized (BluetoothUtil.this) {
|
||||
BluetoothUtil.this.c = null;
|
||||
}
|
||||
BluetoothUtil.this.a(this.a, this.b);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("zdy", "IOException", e);
|
||||
BluetoothUtil.this.b();
|
||||
try {
|
||||
this.a.close();
|
||||
} catch (IOException e2) {
|
||||
Log.e("BluetoothChatService", "unable to close() socket during connection failure", e2);
|
||||
} catch (Exception unused) {
|
||||
Log.e("BluetoothChatService", "unable close socket during connection failure.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BluetoothUtil(Context context, BluetoothUtilCallBack bluetoothUtilCallBack) {
|
||||
this.g = bluetoothUtilCallBack;
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: private */
|
||||
public void b() {
|
||||
a(1);
|
||||
BluetoothUtilCallBack bluetoothUtilCallBack = this.g;
|
||||
if (bluetoothUtilCallBack != null) {
|
||||
bluetoothUtilCallBack.a();
|
||||
}
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: private */
|
||||
public synchronized void a(int i2) {
|
||||
ALog.a("BluetoothChatService").d("setState() " + this.d + " -> " + i2);
|
||||
this.d = i2;
|
||||
if (i2 != 3) {
|
||||
this.e = null;
|
||||
}
|
||||
if (this.g != null) {
|
||||
this.g.a(this.e, i2);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void a(boolean z) {
|
||||
ALog.a("BluetoothChatService").d("start");
|
||||
this.f = z;
|
||||
if (this.c != null) {
|
||||
this.c.cancel();
|
||||
this.c = null;
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.cancel();
|
||||
this.b = null;
|
||||
}
|
||||
if (this.f) {
|
||||
this.b = new AcceptThread();
|
||||
this.b.start();
|
||||
}
|
||||
a(1);
|
||||
}
|
||||
|
||||
public synchronized void a(BluetoothDevice bluetoothDevice) {
|
||||
this.h = bluetoothDevice;
|
||||
new BluetoothThread().start();
|
||||
}
|
||||
|
||||
public synchronized void a(BluetoothSocket bluetoothSocket, BluetoothDevice bluetoothDevice) {
|
||||
ALog.a("BluetoothChatService").d("connected");
|
||||
if (this.c != null) {
|
||||
this.c.cancel();
|
||||
this.c = null;
|
||||
}
|
||||
if (this.b != null) {
|
||||
this.b.cancel();
|
||||
this.b = null;
|
||||
}
|
||||
this.e = bluetoothDevice.getAddress();
|
||||
a(3);
|
||||
if (this.g != null) {
|
||||
this.g.a(bluetoothDevice.getAddress(), bluetoothSocket);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
import android.os.Build;
|
||||
import com.baidu.cloud.media.player.BDCloudMediaPlayer;
|
||||
import com.ubt.jimu.blockly.feature.audio.AndroidManufacturer;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class DeviceDependency {
|
||||
public static boolean a() {
|
||||
if (Build.VERSION.RELEASE.startsWith("4.0.") && (Build.MANUFACTURER.equals("samsung") || Build.MANUFACTURER.equals("HTC"))) {
|
||||
return true;
|
||||
}
|
||||
if (Build.VERSION.RELEASE.startsWith("4.1.") && Build.MANUFACTURER.equals("samsung")) {
|
||||
return true;
|
||||
}
|
||||
return Build.MANUFACTURER.equals(AndroidManufacturer.XIAO_MI) && Build.VERSION.RELEASE.equals(BDCloudMediaPlayer.SDK_VERSION);
|
||||
}
|
||||
|
||||
public static boolean b() {
|
||||
if (Build.MANUFACTURER.equals(AndroidManufacturer.XIAO_MI) && Build.MODEL.equals("2013022") && Build.VERSION.RELEASE.equals("4.2.1")) {
|
||||
return true;
|
||||
}
|
||||
return Build.MODEL.equals("Lenovo A820");
|
||||
}
|
||||
}
|
@@ -0,0 +1,111 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class DeviceProcessThread extends Thread {
|
||||
private DeviceProcessThreadCallBack a;
|
||||
private List<BUFFER_DATA> b;
|
||||
private boolean c = true;
|
||||
|
||||
public interface DeviceProcessThreadCallBack {
|
||||
void a(byte[] bArr, int i);
|
||||
}
|
||||
|
||||
public DeviceProcessThread(DeviceProcessThreadCallBack deviceProcessThreadCallBack) {
|
||||
this.a = deviceProcessThreadCallBack;
|
||||
new ProtocolPacket();
|
||||
this.b = new CopyOnWriteArrayList();
|
||||
}
|
||||
|
||||
public void a(byte[] bArr, int i) {
|
||||
synchronized (this) {
|
||||
BUFFER_DATA buffer_data = new BUFFER_DATA();
|
||||
buffer_data.a = bArr;
|
||||
buffer_data.b = i;
|
||||
buffer_data.c = SystemClock.uptimeMillis();
|
||||
buffer_data.d = 0;
|
||||
this.b.add(buffer_data);
|
||||
if (this.a != null) {
|
||||
this.a.a(bArr, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void b(byte[] bArr, int i) {
|
||||
synchronized (this) {
|
||||
if (this.a != null) {
|
||||
this.a.a(bArr, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
while (this.c) {
|
||||
synchronized (this) {
|
||||
BUFFER_DATA buffer_data = null;
|
||||
Iterator<BUFFER_DATA> it = this.b.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
BUFFER_DATA next = it.next();
|
||||
if (SystemClock.uptimeMillis() - next.c >= 10000) {
|
||||
if (next.d >= 2) {
|
||||
buffer_data = next;
|
||||
break;
|
||||
} else {
|
||||
b(next.a, next.b);
|
||||
next.c = SystemClock.uptimeMillis();
|
||||
next.d++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (buffer_data != null) {
|
||||
this.b.remove(buffer_data);
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(100L);
|
||||
} catch (InterruptedException unused) {
|
||||
this.c = false;
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void a() {
|
||||
synchronized (this) {
|
||||
this.c = false;
|
||||
this.b.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void a(byte b) {
|
||||
synchronized (this) {
|
||||
ProtocolPacket protocolPacket = new ProtocolPacket();
|
||||
BUFFER_DATA buffer_data = null;
|
||||
Iterator<BUFFER_DATA> it = this.b.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
BUFFER_DATA next = it.next();
|
||||
protocolPacket.b(next.a);
|
||||
if (protocolPacket.f() == b) {
|
||||
buffer_data = next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (buffer_data != null) {
|
||||
this.b.remove(buffer_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
sources/com/ubtrobot/jimu/bluetooth/base/IPacket.java
Normal file
12
sources/com/ubtrobot/jimu/bluetooth/base/IPacket.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface IPacket extends Cloneable {
|
||||
byte f();
|
||||
|
||||
byte[] g();
|
||||
|
||||
int getId();
|
||||
|
||||
boolean isValid();
|
||||
}
|
13
sources/com/ubtrobot/jimu/bluetooth/base/PROTOCOL_STATE.java
Normal file
13
sources/com/ubtrobot/jimu/bluetooth/base/PROTOCOL_STATE.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
/* compiled from: ProtocolPacket.java */
|
||||
/* loaded from: classes2.dex */
|
||||
enum PROTOCOL_STATE {
|
||||
HEADER1,
|
||||
HEADER2,
|
||||
LENGHT,
|
||||
CMD,
|
||||
PARAM,
|
||||
CHECKSUM,
|
||||
END
|
||||
}
|
253
sources/com/ubtrobot/jimu/bluetooth/base/ProtocolPacket.java
Normal file
253
sources/com/ubtrobot/jimu/bluetooth/base/ProtocolPacket.java
Normal file
@@ -0,0 +1,253 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base;
|
||||
|
||||
import android.util.Log;
|
||||
import com.ijm.dataencryption.de.DataDecryptTool;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class ProtocolPacket implements IPacket {
|
||||
private byte[] a;
|
||||
private int b;
|
||||
private byte c;
|
||||
private byte[] d;
|
||||
private int e;
|
||||
private PROTOCOL_STATE f = PROTOCOL_STATE.HEADER1;
|
||||
private ByteBuffer g = ByteBuffer.allocate(DataDecryptTool.DECRYPT_SP_FILE);
|
||||
|
||||
/* renamed from: com.ubtrobot.jimu.bluetooth.base.ProtocolPacket$1, reason: invalid class name */
|
||||
static /* synthetic */ class AnonymousClass1 {
|
||||
static final /* synthetic */ int[] a = new int[PROTOCOL_STATE.values().length];
|
||||
|
||||
static {
|
||||
try {
|
||||
a[PROTOCOL_STATE.HEADER1.ordinal()] = 1;
|
||||
} catch (NoSuchFieldError unused) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.HEADER2.ordinal()] = 2;
|
||||
} catch (NoSuchFieldError unused2) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.LENGHT.ordinal()] = 3;
|
||||
} catch (NoSuchFieldError unused3) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.CMD.ordinal()] = 4;
|
||||
} catch (NoSuchFieldError unused4) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.PARAM.ordinal()] = 5;
|
||||
} catch (NoSuchFieldError unused5) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.CHECKSUM.ordinal()] = 6;
|
||||
} catch (NoSuchFieldError unused6) {
|
||||
}
|
||||
try {
|
||||
a[PROTOCOL_STATE.END.ordinal()] = 7;
|
||||
} catch (NoSuchFieldError unused7) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ProtocolPacket() {
|
||||
this.g.clear();
|
||||
}
|
||||
|
||||
public byte a(byte[] bArr, int i, int i2) {
|
||||
byte b = 0;
|
||||
while (i <= i2) {
|
||||
b = (byte) (b + bArr[i]);
|
||||
i++;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
public void a(String str) {
|
||||
}
|
||||
|
||||
public void b(byte[] bArr) {
|
||||
this.a = new byte[2];
|
||||
System.arraycopy(bArr, 0, this.a, 0, 2);
|
||||
this.b = bArr[2] & 255;
|
||||
this.c = bArr[3];
|
||||
int i = this.b;
|
||||
int i2 = 4;
|
||||
if (i - 5 > 0) {
|
||||
this.d = new byte[i - 5];
|
||||
System.arraycopy(bArr, 4, this.d, 0, i - 5);
|
||||
int i3 = this.b;
|
||||
i2 = 4 + (i3 - 5);
|
||||
this.e = i3 - 5;
|
||||
}
|
||||
byte b = bArr[i2];
|
||||
byte b2 = bArr[i2 + 1];
|
||||
}
|
||||
|
||||
public Object clone() {
|
||||
try {
|
||||
return (ProtocolPacket) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
Log.e("ContentValues", "Clone BleProtocolPacket fail!", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.IPacket
|
||||
public byte f() {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.IPacket
|
||||
public byte[] g() {
|
||||
return this.d;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.IPacket
|
||||
public int getId() {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.IPacket
|
||||
public boolean isValid() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void a(byte b) {
|
||||
this.c = b;
|
||||
}
|
||||
|
||||
public void a(byte[] bArr) {
|
||||
this.d = bArr;
|
||||
if (bArr == null) {
|
||||
this.e = 0;
|
||||
} else {
|
||||
this.e = bArr.length;
|
||||
}
|
||||
}
|
||||
|
||||
public ProtocolPacket(int i, byte[] bArr) {
|
||||
this.g.clear();
|
||||
a((byte) i);
|
||||
a(bArr);
|
||||
}
|
||||
|
||||
public int a() {
|
||||
return this.e;
|
||||
}
|
||||
|
||||
public void a(int i) {
|
||||
this.e = i;
|
||||
}
|
||||
|
||||
/* JADX WARN: Can't fix incorrect switch cases order, some code will duplicate */
|
||||
public boolean b(byte b) {
|
||||
try {
|
||||
int i = 0;
|
||||
switch (AnonymousClass1.a[this.f.ordinal()]) {
|
||||
case 1:
|
||||
if (b != -5) {
|
||||
Log.v("error_blue", "1");
|
||||
} else {
|
||||
this.g.clear();
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.HEADER2;
|
||||
}
|
||||
return false;
|
||||
case 2:
|
||||
if (b != -65) {
|
||||
this.f = PROTOCOL_STATE.HEADER1;
|
||||
Log.v("error_blue", "2");
|
||||
if (b == -5) {
|
||||
b(b);
|
||||
}
|
||||
} else {
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.LENGHT;
|
||||
}
|
||||
return false;
|
||||
case 3:
|
||||
this.b = b & 255;
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.CMD;
|
||||
return false;
|
||||
case 4:
|
||||
this.c = b;
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.PARAM;
|
||||
this.e = this.b - 5;
|
||||
this.d = new byte[this.e];
|
||||
return false;
|
||||
case 5:
|
||||
this.g.put(b);
|
||||
this.e--;
|
||||
if (this.e == 0) {
|
||||
this.f = PROTOCOL_STATE.CHECKSUM;
|
||||
}
|
||||
return false;
|
||||
case 6:
|
||||
if (a(this.g.array(), 2, this.g.position() - 1) != b) {
|
||||
this.f = PROTOCOL_STATE.HEADER1;
|
||||
Log.v("error_blue", "3");
|
||||
if (b == -5) {
|
||||
b(b);
|
||||
}
|
||||
} else {
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.END;
|
||||
}
|
||||
return false;
|
||||
case 7:
|
||||
if (b != -19) {
|
||||
this.f = PROTOCOL_STATE.HEADER1;
|
||||
Log.v("error_blue", "4");
|
||||
if (b == -5) {
|
||||
b(b);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
this.g.put(b);
|
||||
this.f = PROTOCOL_STATE.HEADER1;
|
||||
int i2 = 0;
|
||||
while (i < this.b - 5) {
|
||||
this.d[i2] = this.g.get(i + 4);
|
||||
i++;
|
||||
i2++;
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.getMessage();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] b() {
|
||||
int i;
|
||||
int i2 = this.e;
|
||||
int i3 = (short) (i2 == 0 ? 7 : i2 + 6);
|
||||
byte[] bArr = new byte[i3];
|
||||
int i4 = 0;
|
||||
bArr[0] = -5;
|
||||
bArr[1] = -65;
|
||||
bArr[2] = (byte) ((i3 - 1) & 255);
|
||||
bArr[3] = this.c;
|
||||
if (this.e == 0) {
|
||||
i = 5;
|
||||
bArr[4] = 0;
|
||||
} else {
|
||||
i = 4;
|
||||
while (i4 < this.e) {
|
||||
bArr[i] = this.d[i4];
|
||||
i4++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
int i5 = i + 1;
|
||||
bArr[i] = a(bArr, 2, i5);
|
||||
bArr[i5] = -19;
|
||||
return bArr;
|
||||
}
|
||||
}
|
@@ -0,0 +1,164 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base.discover;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.bluetooth.le.BluetoothLeScanner;
|
||||
import android.bluetooth.le.ScanCallback;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.jimu.bluetooth.Cancellable;
|
||||
import com.ubtrobot.log.ALog;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BleDeviceScanner {
|
||||
private BluetoothAdapter b;
|
||||
private Handler d;
|
||||
private ScannedHubEmitter e;
|
||||
private int a = 5000;
|
||||
private List<BluetoothDevice> c = new ArrayList();
|
||||
private BluetoothAdapter.LeScanCallback f = new BluetoothAdapter.LeScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner.1
|
||||
@Override // android.bluetooth.BluetoothAdapter.LeScanCallback
|
||||
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
|
||||
if (BleDeviceScanner.this.c.contains(bluetoothDevice)) {
|
||||
return;
|
||||
}
|
||||
BleDeviceScanner.this.c.add(bluetoothDevice);
|
||||
BleDeviceScanner.this.e.a(new ScanResult(bluetoothDevice, i));
|
||||
}
|
||||
};
|
||||
private ScanCallback g = new ScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner.2
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
public void onBatchScanResults(List<android.bluetooth.le.ScanResult> list) {
|
||||
super.onBatchScanResults(list);
|
||||
}
|
||||
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
public void onScanFailed(int i) {
|
||||
Log.e("ContentValues", "Scan ble fail! errorCode:" + i);
|
||||
super.onScanFailed(i);
|
||||
BleDeviceScanner.this.e.a(i, "Scan ble device fail! Detail define refer to ScanCallback.SCAN_FAILED_*");
|
||||
}
|
||||
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
@TargetApi(21)
|
||||
public void onScanResult(int i, android.bluetooth.le.ScanResult scanResult) {
|
||||
BluetoothDevice device = scanResult.getDevice();
|
||||
if (BleDeviceScanner.this.c.contains(device)) {
|
||||
return;
|
||||
}
|
||||
BleDeviceScanner.this.c.add(device);
|
||||
BleDeviceScanner.this.e.a(new ScanResult(device, scanResult.getRssi()));
|
||||
}
|
||||
};
|
||||
|
||||
private class DiscoverCancelable implements Cancellable, Runnable {
|
||||
private final Handler a;
|
||||
private final ScannedHubEmitter b;
|
||||
private boolean c;
|
||||
private boolean d;
|
||||
|
||||
public DiscoverCancelable(Handler handler, ScannedHubEmitter scannedHubEmitter, int i) {
|
||||
this.a = handler;
|
||||
this.a.postDelayed(this, i);
|
||||
this.b = scannedHubEmitter;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
||||
public synchronized boolean cancel() {
|
||||
if (!this.d && !this.c) {
|
||||
this.a.removeCallbacks(this);
|
||||
BleDeviceScanner.this.b();
|
||||
this.d = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
||||
public synchronized boolean isCancelled() {
|
||||
return this.d;
|
||||
}
|
||||
|
||||
@Override // java.lang.Runnable
|
||||
public synchronized void run() {
|
||||
if (!this.d) {
|
||||
this.c = true;
|
||||
BleDeviceScanner.this.b();
|
||||
this.b.a();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BleDeviceScanner(Context context) {
|
||||
this.d = new Handler(context.getMainLooper());
|
||||
this.b = ((BluetoothManager) context.getSystemService("bluetooth")).getAdapter();
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: private */
|
||||
public void b() {
|
||||
BluetoothAdapter bluetoothAdapter = this.b;
|
||||
if (bluetoothAdapter == null) {
|
||||
return;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT < 21) {
|
||||
bluetoothAdapter.stopLeScan(this.f);
|
||||
return;
|
||||
}
|
||||
BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (bluetoothLeScanner == null) {
|
||||
return;
|
||||
}
|
||||
bluetoothLeScanner.stopScan(this.g);
|
||||
ALog.a("ContentValues").d("call BluetoothLeScanner.stopScan by new api after Android LOLLIPOP");
|
||||
}
|
||||
|
||||
public Cancellable a(ScannedHubEmitter scannedHubEmitter, int i) {
|
||||
BluetoothAdapter bluetoothAdapter = this.b;
|
||||
if (bluetoothAdapter == null) {
|
||||
Log.e("ContentValues", "Bluetooth is not support!");
|
||||
this.e.a(4, "Scan fail as bluetooth is not support");
|
||||
return null;
|
||||
}
|
||||
if (bluetoothAdapter.getState() != 12) {
|
||||
scannedHubEmitter.a(-3, "Bluetooth is not turn on!");
|
||||
return null;
|
||||
}
|
||||
if (i < 5000) {
|
||||
Log.e("ContentValues", "Duration must greater than 5000");
|
||||
this.a = 5000;
|
||||
} else {
|
||||
this.a = i;
|
||||
}
|
||||
DiscoverCancelable discoverCancelable = new DiscoverCancelable(this.d, scannedHubEmitter, this.a);
|
||||
this.e = scannedHubEmitter;
|
||||
try {
|
||||
a();
|
||||
} catch (Exception e) {
|
||||
Log.e("ContentValues", "Start scan fail", e);
|
||||
}
|
||||
return discoverCancelable;
|
||||
}
|
||||
|
||||
private void a() {
|
||||
if (this.b == null) {
|
||||
return;
|
||||
}
|
||||
this.c.clear();
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
BluetoothLeScanner bluetoothLeScanner = this.b.getBluetoothLeScanner();
|
||||
if (bluetoothLeScanner == null) {
|
||||
return;
|
||||
}
|
||||
bluetoothLeScanner.startScan(this.g);
|
||||
ALog.a("ContentValues").d("call BluetoothLeScanner.startScan by new api after Android LOLLIPOP");
|
||||
return;
|
||||
}
|
||||
this.b.startLeScan(this.f);
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base.discover;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class ScanResult {
|
||||
private BluetoothDevice a;
|
||||
private int b;
|
||||
|
||||
public ScanResult(BluetoothDevice bluetoothDevice, int i) {
|
||||
this.a = bluetoothDevice;
|
||||
this.b = i;
|
||||
}
|
||||
|
||||
public BluetoothDevice a() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public int b() {
|
||||
return this.b;
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
package com.ubtrobot.jimu.bluetooth.base.discover;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface ScannedHubEmitter {
|
||||
void a();
|
||||
|
||||
void a(int i, String str);
|
||||
|
||||
void a(ScanResult scanResult);
|
||||
}
|
13
sources/com/ubtrobot/jimu/bluetooth/ble/BUFFER_DATA.java
Normal file
13
sources/com/ubtrobot/jimu/bluetooth/ble/BUFFER_DATA.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.ubtrobot.jimu.bluetooth.ble;
|
||||
|
||||
/* compiled from: BleDeviceProcessThread.java */
|
||||
/* loaded from: classes2.dex */
|
||||
class BUFFER_DATA {
|
||||
public byte[] a;
|
||||
public int b;
|
||||
public long c;
|
||||
public int d;
|
||||
|
||||
BUFFER_DATA() {
|
||||
}
|
||||
}
|
197
sources/com/ubtrobot/jimu/bluetooth/ble/BleClientHandler.java
Normal file
197
sources/com/ubtrobot/jimu/bluetooth/ble/BleClientHandler.java
Normal file
@@ -0,0 +1,197 @@
|
||||
package com.ubtrobot.jimu.bluetooth.ble;
|
||||
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
import android.bluetooth.BluetoothGattService;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
import com.afunx.ble.blelitelib.log.BleLiteLog;
|
||||
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
|
||||
import com.afunx.ble.blelitelib.utils.BleUuidUtils;
|
||||
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
|
||||
import com.ubtrobot.jimu.bluetooth.ble.BleDeviceProcessThread;
|
||||
import com.ubtrobot.jimu.bluetooth.utils.ByteHexHelper;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BleClientHandler extends Thread implements BleDeviceProcessThread.Callback {
|
||||
private static final String p = BleClientHandler.class.getSimpleName();
|
||||
private String a;
|
||||
private BleGattClientProxy b;
|
||||
private BluetoothGattService c;
|
||||
private BluetoothGattCharacteristic d;
|
||||
private BluetoothGattCharacteristic e;
|
||||
private int l;
|
||||
private int m;
|
||||
private ClientCallback n;
|
||||
private boolean h = true;
|
||||
private boolean k = true;
|
||||
private BleGattClientProxy.OnCharacteristicNotificationListener o = new BleGattClientProxy.OnCharacteristicNotificationListener() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.1
|
||||
@Override // com.afunx.ble.blelitelib.proxy.BleGattClientProxy.OnCharacteristicNotificationListener
|
||||
public synchronized void onCharacteristicNotification(byte[] bArr) {
|
||||
if (bArr != null) {
|
||||
if (bArr.length != 0) {
|
||||
try {
|
||||
Log.i(BleClientHandler.p, "onReceive:msg :" + ByteHexHelper.a(bArr));
|
||||
for (byte b : bArr) {
|
||||
if (BleClientHandler.this.f.b(b)) {
|
||||
if (BleClientHandler.this.n != null) {
|
||||
BleClientHandler.this.f.a(BleClientHandler.this.f.g().length);
|
||||
BleClientHandler.this.n.a(BleClientHandler.this.a, BleClientHandler.this.f.f(), BleClientHandler.this.f.g(), BleClientHandler.this.f.a());
|
||||
}
|
||||
BleClientHandler.this.l = 0;
|
||||
BleClientHandler.this.m = 0;
|
||||
BleClientHandler.this.i = SystemClock.uptimeMillis();
|
||||
BleClientHandler.this.g.a(BleClientHandler.this.f.f());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
private ProtocolPacket f = new ProtocolPacket();
|
||||
private long i = SystemClock.uptimeMillis();
|
||||
private long j = this.i;
|
||||
private BleDeviceProcessThread g = new BleDeviceProcessThread(this);
|
||||
|
||||
public interface ClientCallback {
|
||||
void a(String str);
|
||||
|
||||
void a(String str, byte b, byte[] bArr, int i);
|
||||
}
|
||||
|
||||
public BleClientHandler(String str, BleGattClientProxy bleGattClientProxy, ClientCallback clientCallback) {
|
||||
this.l = 0;
|
||||
this.m = 0;
|
||||
this.a = str;
|
||||
this.b = bleGattClientProxy;
|
||||
this.n = clientCallback;
|
||||
this.l = 0;
|
||||
this.m = 0;
|
||||
this.g.start();
|
||||
b();
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
while (this.h) {
|
||||
try {
|
||||
if (this.c == null) {
|
||||
this.c = this.b.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
|
||||
}
|
||||
if (this.d == null) {
|
||||
this.d = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-1e4d-4bd9-ba61-23c647249616"));
|
||||
}
|
||||
if (this.e == null) {
|
||||
this.e = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-8841-43f4-a8d4-ecbe34729bb3"));
|
||||
}
|
||||
} catch (Exception unused) {
|
||||
this.h = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void c(byte[] bArr, int i) {
|
||||
if (SystemClock.uptimeMillis() - this.j < 3000) {
|
||||
return;
|
||||
}
|
||||
if (!this.k) {
|
||||
this.i = SystemClock.uptimeMillis();
|
||||
} else {
|
||||
a(bArr, i);
|
||||
this.l++;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean d() {
|
||||
return (this.h && isAlive() && (!(this.l > 3 || this.m > 6) || !this.k)) ? false : true;
|
||||
}
|
||||
|
||||
public void b(byte[] bArr, int i) {
|
||||
this.g.a(bArr, i);
|
||||
}
|
||||
|
||||
public String a() {
|
||||
return this.a;
|
||||
}
|
||||
|
||||
public void b() {
|
||||
if (this.c == null) {
|
||||
BleGattClientProxy bleGattClientProxy = this.b;
|
||||
if (bleGattClientProxy == null) {
|
||||
return;
|
||||
} else {
|
||||
this.c = bleGattClientProxy.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
|
||||
}
|
||||
}
|
||||
BluetoothGattService bluetoothGattService = this.c;
|
||||
if (bluetoothGattService == null) {
|
||||
Log.e(p, "gattWriteService is null");
|
||||
return;
|
||||
}
|
||||
if (this.d == null) {
|
||||
BleGattClientProxy bleGattClientProxy2 = this.b;
|
||||
if (bleGattClientProxy2 == null || bluetoothGattService == null) {
|
||||
return;
|
||||
} else {
|
||||
this.d = bleGattClientProxy2.discoverCharacteristic(bluetoothGattService, BleUuidUtils.str2uuid("49535343-1e4d-4bd9-ba61-23c647249616"));
|
||||
}
|
||||
}
|
||||
BleGattClientProxy bleGattClientProxy3 = this.b;
|
||||
if (bleGattClientProxy3 == null) {
|
||||
return;
|
||||
}
|
||||
BluetoothGattCharacteristic bluetoothGattCharacteristic = this.d;
|
||||
if (bluetoothGattCharacteristic == null) {
|
||||
BleLiteLog.e(p, "registerCharacteristicNotification fail! Because gattNotifyCharacteristic is null! ");
|
||||
} else {
|
||||
bleGattClientProxy3.registerCharacteristicNotification(bluetoothGattCharacteristic, this.o);
|
||||
}
|
||||
}
|
||||
|
||||
public void a(boolean z) {
|
||||
this.k = z;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.ble.BleDeviceProcessThread.Callback
|
||||
public synchronized void a(byte[] bArr, int i) {
|
||||
try {
|
||||
Log.i("Handler-", "onSendData");
|
||||
if (this.c == null) {
|
||||
this.c = this.b.discoverService(BleUuidUtils.str2uuid("49535343-fe7d-4ae5-8fa9-9fafd205e455"), 5000L);
|
||||
}
|
||||
if (this.e == null) {
|
||||
this.e = this.b.discoverCharacteristic(this.c, BleUuidUtils.str2uuid("49535343-8841-43f4-a8d4-ecbe34729bb3"));
|
||||
}
|
||||
Log.i(p, "onSend:" + ByteHexHelper.a(bArr));
|
||||
boolean writeCharacterisitcNoResponse2 = this.b.writeCharacterisitcNoResponse2(this.e, bArr);
|
||||
this.j = SystemClock.uptimeMillis();
|
||||
this.m = this.m + 1;
|
||||
Log.i(p, "sendNormalCount: " + this.m + " writeSucc: " + writeCharacterisitcNoResponse2);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
c();
|
||||
}
|
||||
}
|
||||
|
||||
public void c() {
|
||||
synchronized (this) {
|
||||
try {
|
||||
this.l = 0;
|
||||
this.m = 0;
|
||||
this.h = false;
|
||||
if (this.g != null) {
|
||||
this.g.a();
|
||||
}
|
||||
if (this.n != null) {
|
||||
this.n.a(this.a);
|
||||
}
|
||||
this.b.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,115 @@
|
||||
package com.ubtrobot.jimu.bluetooth.ble;
|
||||
|
||||
import android.os.SystemClock;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BleDeviceProcessThread extends Thread {
|
||||
private static final String d = BleDeviceProcessThread.class.getSimpleName();
|
||||
private List<BUFFER_DATA> a;
|
||||
private boolean b = true;
|
||||
private Callback c;
|
||||
|
||||
public interface Callback {
|
||||
void a(byte[] bArr, int i);
|
||||
}
|
||||
|
||||
public BleDeviceProcessThread(Callback callback) {
|
||||
this.c = callback;
|
||||
new ProtocolPacket();
|
||||
this.a = new ArrayList();
|
||||
}
|
||||
|
||||
public void a(byte[] bArr, int i) {
|
||||
synchronized (this) {
|
||||
Log.i(d, "sendData");
|
||||
BUFFER_DATA buffer_data = new BUFFER_DATA();
|
||||
buffer_data.a = bArr;
|
||||
buffer_data.b = i;
|
||||
buffer_data.c = SystemClock.uptimeMillis();
|
||||
buffer_data.d = 0;
|
||||
this.a.add(buffer_data);
|
||||
if (this.c != null) {
|
||||
this.c.a(bArr, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void b(byte[] bArr, int i) {
|
||||
synchronized (this) {
|
||||
if (this.c != null) {
|
||||
this.c.a(bArr, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
while (this.b) {
|
||||
synchronized (this) {
|
||||
BUFFER_DATA buffer_data = null;
|
||||
Iterator<BUFFER_DATA> it = this.a.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
BUFFER_DATA next = it.next();
|
||||
if (SystemClock.uptimeMillis() - next.c >= 10000) {
|
||||
if (next.d >= 2) {
|
||||
buffer_data = next;
|
||||
break;
|
||||
} else {
|
||||
b(next.a, next.b);
|
||||
next.c = SystemClock.uptimeMillis();
|
||||
next.d++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (buffer_data != null) {
|
||||
this.a.remove(buffer_data);
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(100L);
|
||||
} catch (InterruptedException unused) {
|
||||
this.b = false;
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void a() {
|
||||
synchronized (this) {
|
||||
this.b = false;
|
||||
this.a.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void a(byte b) {
|
||||
synchronized (this) {
|
||||
ProtocolPacket protocolPacket = new ProtocolPacket();
|
||||
BUFFER_DATA buffer_data = null;
|
||||
Iterator<BUFFER_DATA> it = this.a.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
BUFFER_DATA next = it.next();
|
||||
protocolPacket.b(next.a);
|
||||
if (protocolPacket.f() == b) {
|
||||
buffer_data = next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (buffer_data != null) {
|
||||
this.a.remove(buffer_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
199
sources/com/ubtrobot/jimu/bluetooth/ble/BleLib.java
Normal file
199
sources/com/ubtrobot/jimu/bluetooth/ble/BleLib.java
Normal file
@@ -0,0 +1,199 @@
|
||||
package com.ubtrobot.jimu.bluetooth.ble;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.le.ScanCallback;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.jimu.bluetooth.Cancellable;
|
||||
import com.ubtrobot.jimu.bluetooth.base.discover.BleDeviceScanner;
|
||||
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.jimu.bluetooth.utils.ByteHexHelper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BleLib extends Callback implements Proxy {
|
||||
private static BleLib e;
|
||||
private static BleManager f;
|
||||
private ArrayList<String> b = new ArrayList<>();
|
||||
private DeviceListener c;
|
||||
private BleDeviceScanner d;
|
||||
|
||||
private BleLib() {
|
||||
new ArrayList();
|
||||
new Handler(Looper.getMainLooper());
|
||||
new BluetoothAdapter.LeScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.2
|
||||
@Override // android.bluetooth.BluetoothAdapter.LeScanCallback
|
||||
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
|
||||
String replace = TextUtils.isEmpty(bluetoothDevice.getName()) ? "" : bluetoothDevice.getName().replace("\n", "").replace("\\n", "");
|
||||
String str = replace + "\n" + bluetoothDevice.getAddress() + "\n" + i;
|
||||
Log.i("BleLib", str);
|
||||
if (BleLib.this.c(str)) {
|
||||
return;
|
||||
}
|
||||
BleLib.this.b.add(str);
|
||||
BleLib.this.c.b(str);
|
||||
Proxy.a.add(new Device(bluetoothDevice.getAddress(), replace));
|
||||
}
|
||||
};
|
||||
new ScanCallback() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.4
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
public void onBatchScanResults(List<ScanResult> list) {
|
||||
super.onBatchScanResults(list);
|
||||
}
|
||||
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
public void onScanFailed(int i) {
|
||||
Log.e("BleLib", "Scan ble fail! errorCode:" + i);
|
||||
super.onScanFailed(i);
|
||||
}
|
||||
|
||||
@Override // android.bluetooth.le.ScanCallback
|
||||
@TargetApi(21)
|
||||
public void onScanResult(int i, ScanResult scanResult) {
|
||||
Log.d("BleLib", "onScanResult " + scanResult.getDevice().getName());
|
||||
BluetoothDevice device = scanResult.getDevice();
|
||||
String replace = TextUtils.isEmpty(device.getName()) ? "" : device.getName().replace("\n", "").replace("\\n", "");
|
||||
String str = replace + "\n" + device.getAddress() + "\n" + scanResult.getRssi();
|
||||
Log.i("BleLib", str);
|
||||
if (BleLib.this.c(str)) {
|
||||
return;
|
||||
}
|
||||
BleLib.this.b.add(str);
|
||||
BleLib.this.c.b(str);
|
||||
Proxy.a.add(new Device(device.getAddress(), replace));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/* JADX INFO: Access modifiers changed from: private */
|
||||
public boolean c(String str) {
|
||||
for (int i = 0; i < this.b.size(); i++) {
|
||||
if (this.b.get(i).equals(str)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static synchronized BleLib b() {
|
||||
BleLib bleLib;
|
||||
synchronized (BleLib.class) {
|
||||
if (e == null) {
|
||||
synchronized (BleLib.class) {
|
||||
if (e == null) {
|
||||
e = new BleLib();
|
||||
}
|
||||
}
|
||||
}
|
||||
bleLib = e;
|
||||
}
|
||||
return bleLib;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public boolean a(Context context, DeviceListener deviceListener) {
|
||||
this.c = deviceListener;
|
||||
if (f == null) {
|
||||
f = new BleManager(context, this);
|
||||
f.b();
|
||||
}
|
||||
if (this.d == null) {
|
||||
this.d = new BleDeviceScanner(context);
|
||||
}
|
||||
return BluetoothAdapter.getDefaultAdapter() != null;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
|
||||
public void b(String str, byte b, byte[] bArr, int i) {
|
||||
DeviceListener deviceListener = this.c;
|
||||
if (deviceListener != null) {
|
||||
deviceListener.a(str, b, bArr, i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
|
||||
public void b(String str) {
|
||||
DeviceListener deviceListener = this.c;
|
||||
if (deviceListener != null) {
|
||||
deviceListener.disconnect(str);
|
||||
}
|
||||
}
|
||||
|
||||
public Cancellable a(final ScannedHubEmitter scannedHubEmitter, int i) {
|
||||
return this.d.a(new ScannedHubEmitter() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleLib.1
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a(com.ubtrobot.jimu.bluetooth.base.discover.ScanResult scanResult) {
|
||||
if (scanResult.a() == null || scanResult.a().getName() == null || !scanResult.a().getName().toLowerCase().contains("jimu")) {
|
||||
return;
|
||||
}
|
||||
scannedHubEmitter.a(scanResult);
|
||||
BluetoothDevice a = scanResult.a();
|
||||
int b = scanResult.b();
|
||||
String replace = TextUtils.isEmpty(a.getName()) ? "" : a.getName().replace("\n", "").replace("\\n", "");
|
||||
String str = replace + "\n" + a.getAddress() + "\n" + b;
|
||||
Log.v("BleLib", str);
|
||||
if (BleLib.this.c(str)) {
|
||||
return;
|
||||
}
|
||||
BleLib.this.b.add(str);
|
||||
BleLib.this.c.b(str);
|
||||
Proxy.a.add(new Device(a.getAddress(), replace));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a(String str) {
|
||||
if (TextUtils.isEmpty(str)) {
|
||||
return;
|
||||
}
|
||||
Log.i("BleLib", "connect: mac: " + str);
|
||||
f.d(str);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a() {
|
||||
f.a();
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a(String str, boolean z) {
|
||||
f.a(str, z);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public synchronized void a(String str, byte b, byte[] bArr, int i) {
|
||||
Log.i("BleLib", "sendData:" + ByteHexHelper.a(bArr) + " cmd:" + ((int) b));
|
||||
f.b(str, b, bArr, i);
|
||||
Log.i("BleLib", "after-sendData");
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Callback
|
||||
public void a(boolean z, String str) {
|
||||
DeviceListener deviceListener = this.c;
|
||||
if (deviceListener != null) {
|
||||
deviceListener.onConnectState(z, str);
|
||||
}
|
||||
}
|
||||
}
|
174
sources/com/ubtrobot/jimu/bluetooth/ble/BleManager.java
Normal file
174
sources/com/ubtrobot/jimu/bluetooth/ble/BleManager.java
Normal file
@@ -0,0 +1,174 @@
|
||||
package com.ubtrobot.jimu.bluetooth.ble;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
|
||||
import com.afunx.ble.blelitelib.proxy.BleGattClientProxyImpl;
|
||||
import com.ubtrobot.jimu.bluetooth.base.ProtocolPacket;
|
||||
import com.ubtrobot.jimu.bluetooth.ble.BleClientHandler;
|
||||
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 BleManager extends Thread implements BleClientHandler.ClientCallback {
|
||||
private static final String f = BleManager.class.getSimpleName();
|
||||
private boolean a = true;
|
||||
private BleGattClientProxy b;
|
||||
private Callback c;
|
||||
private List<BleClientHandler> d;
|
||||
private Handler e;
|
||||
|
||||
public BleManager(Context context, Callback callback) {
|
||||
new ArrayList();
|
||||
new ArrayList();
|
||||
this.d = new ArrayList();
|
||||
this.e = new Handler(Looper.getMainLooper());
|
||||
this.b = new BleGattClientProxyImpl(context.getApplicationContext());
|
||||
this.c = callback;
|
||||
}
|
||||
|
||||
public synchronized void d(final String str) {
|
||||
if (TextUtils.isEmpty(str)) {
|
||||
return;
|
||||
}
|
||||
Log.i(f, "connect: mac: " + str);
|
||||
new Thread(new Runnable() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleManager.1
|
||||
@Override // java.lang.Runnable
|
||||
public void run() {
|
||||
final boolean connect = BleManager.this.b.connect(str, 30000L);
|
||||
BleManager.this.b(str);
|
||||
BleManager.this.e.post(new Runnable() { // from class: com.ubtrobot.jimu.bluetooth.ble.BleManager.1.1
|
||||
@Override // java.lang.Runnable
|
||||
public void run() {
|
||||
BleManager.this.c.a(connect, str);
|
||||
}
|
||||
});
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
@Override // java.lang.Thread, java.lang.Runnable
|
||||
public void run() {
|
||||
BleClientHandler bleClientHandler;
|
||||
while (this.a) {
|
||||
synchronized (this) {
|
||||
Iterator<BleClientHandler> it = this.d.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
bleClientHandler = null;
|
||||
break;
|
||||
}
|
||||
bleClientHandler = it.next();
|
||||
if (bleClientHandler.d()) {
|
||||
Log.i(f, "tryToReleaseConnection");
|
||||
break;
|
||||
}
|
||||
ProtocolPacket protocolPacket = new ProtocolPacket();
|
||||
protocolPacket.a((byte) 3);
|
||||
protocolPacket.a((byte[]) null);
|
||||
protocolPacket.a(0);
|
||||
byte[] b = protocolPacket.b();
|
||||
bleClientHandler.c(b, b.length);
|
||||
}
|
||||
if (bleClientHandler != null) {
|
||||
String a = bleClientHandler.a();
|
||||
bleClientHandler.c();
|
||||
this.d.remove(bleClientHandler);
|
||||
if (this.c != null) {
|
||||
this.c.b(a);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
Thread.sleep(300L);
|
||||
} catch (InterruptedException unused) {
|
||||
this.a = false;
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void a() {
|
||||
if (this.d != null) {
|
||||
Iterator<BleClientHandler> it = this.d.iterator();
|
||||
while (it.hasNext()) {
|
||||
it.next().c();
|
||||
}
|
||||
this.d.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void b() {
|
||||
this.a = true;
|
||||
start();
|
||||
}
|
||||
|
||||
public synchronized BleClientHandler c(String str) {
|
||||
BleClientHandler bleClientHandler;
|
||||
bleClientHandler = null;
|
||||
Iterator<BleClientHandler> it = this.d.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
break;
|
||||
}
|
||||
BleClientHandler next = it.next();
|
||||
if (next.a().equals(str)) {
|
||||
bleClientHandler = next;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bleClientHandler;
|
||||
}
|
||||
|
||||
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();
|
||||
BleClientHandler c = c(str);
|
||||
if (c != null) {
|
||||
c.b(b2, b2.length);
|
||||
}
|
||||
}
|
||||
|
||||
public void a(String str, boolean z) {
|
||||
BleClientHandler c = c(str);
|
||||
if (c != null) {
|
||||
c.a(z);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.ClientCallback
|
||||
public synchronized void a(String str, byte b, byte[] bArr, int i) {
|
||||
if (this.c != null) {
|
||||
this.c.b(str, b, bArr, i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.ble.BleClientHandler.ClientCallback
|
||||
public synchronized void a(String str) {
|
||||
if (this.c != null) {
|
||||
this.c.b(str);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void b(String str) {
|
||||
if (c(str) != null) {
|
||||
return;
|
||||
}
|
||||
if (this.b == null) {
|
||||
return;
|
||||
}
|
||||
BleClientHandler bleClientHandler = new BleClientHandler(str, this.b, this);
|
||||
this.d.add(bleClientHandler);
|
||||
bleClientHandler.start();
|
||||
}
|
||||
}
|
162
sources/com/ubtrobot/jimu/bluetooth/proxy/BluetoothProxy.java
Normal file
162
sources/com/ubtrobot/jimu/bluetooth/proxy/BluetoothProxy.java
Normal file
@@ -0,0 +1,162 @@
|
||||
package com.ubtrobot.jimu.bluetooth.proxy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import com.ubtech.jimu.storage.UbtPrefs;
|
||||
import com.ubtrobot.jimu.bluetooth.Cancellable;
|
||||
import com.ubtrobot.jimu.bluetooth.base.BluetoothLib;
|
||||
import com.ubtrobot.jimu.bluetooth.base.discover.ScanResult;
|
||||
import com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter;
|
||||
import com.ubtrobot.jimu.bluetooth.ble.BleLib;
|
||||
import com.ubtrobot.log.ALog;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BluetoothProxy implements Proxy {
|
||||
private static final String i = "BluetoothProxy";
|
||||
private static BluetoothProxy j;
|
||||
private Context c;
|
||||
private DeviceListener d;
|
||||
private UbtPrefs e;
|
||||
private Cancellable g;
|
||||
private Cancellable h;
|
||||
private Proxy b = BleLib.b();
|
||||
private ArrayList<String> f = new ArrayList<>();
|
||||
|
||||
private BluetoothProxy() {
|
||||
this.f.add("lemobile");
|
||||
this.f.add("meizu");
|
||||
this.f.add("vivo");
|
||||
this.f.add("oneplus");
|
||||
this.f.add("oppo");
|
||||
this.f.add("lenove");
|
||||
}
|
||||
|
||||
public static synchronized BluetoothProxy c() {
|
||||
BluetoothProxy bluetoothProxy;
|
||||
synchronized (BluetoothProxy.class) {
|
||||
if (j == null) {
|
||||
j = new BluetoothProxy();
|
||||
}
|
||||
bluetoothProxy = j;
|
||||
}
|
||||
return bluetoothProxy;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public boolean a(Context context, DeviceListener deviceListener) {
|
||||
this.c = context;
|
||||
this.d = deviceListener;
|
||||
this.e = new UbtPrefs(context);
|
||||
Log.i(i, "init ble");
|
||||
BluetoothLib.d().a(context, deviceListener);
|
||||
return BleLib.b().a(context, deviceListener);
|
||||
}
|
||||
|
||||
public Cancellable a(final ScannedHubEmitter scannedHubEmitter, final int i2) {
|
||||
if (this.e.a("sp_key_is_last_connect_ble", (Boolean) true).booleanValue()) {
|
||||
this.g = BleLib.b().a(new ScannedHubEmitter(this) { // from class: com.ubtrobot.jimu.bluetooth.proxy.BluetoothProxy.1
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a(ScanResult scanResult) {
|
||||
scannedHubEmitter.a(scanResult);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a(int i3, String str) {
|
||||
scannedHubEmitter.a(i3, str);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a() {
|
||||
ALog.a(BluetoothProxy.i).d("Ble scan end, start classical bluetooth scan");
|
||||
BluetoothLib.d().a(scannedHubEmitter, i2 / 2);
|
||||
}
|
||||
}, i2 / 2);
|
||||
} else {
|
||||
this.h = BluetoothLib.d().a(new ScannedHubEmitter(this) { // from class: com.ubtrobot.jimu.bluetooth.proxy.BluetoothProxy.2
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a(ScanResult scanResult) {
|
||||
scannedHubEmitter.a(scanResult);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a(int i3, String str) {
|
||||
scannedHubEmitter.a(i3, str);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.base.discover.ScannedHubEmitter
|
||||
public void a() {
|
||||
ALog.a(BluetoothProxy.i).d("Classical Bluetooth scan end, start ble scan");
|
||||
BleLib.b().a(scannedHubEmitter, i2 / 2);
|
||||
}
|
||||
}, i2 / 2);
|
||||
}
|
||||
return new Cancellable() { // from class: com.ubtrobot.jimu.bluetooth.proxy.BluetoothProxy.3
|
||||
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
||||
public boolean cancel() {
|
||||
if (BluetoothProxy.this.g != null) {
|
||||
BluetoothProxy.this.g.cancel();
|
||||
}
|
||||
if (BluetoothProxy.this.h == null) {
|
||||
return true;
|
||||
}
|
||||
BluetoothProxy.this.h.cancel();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.Cancellable
|
||||
public boolean isCancelled() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a(String str) {
|
||||
boolean z;
|
||||
Iterator<Device> it = Proxy.a.iterator();
|
||||
while (true) {
|
||||
if (!it.hasNext()) {
|
||||
z = false;
|
||||
break;
|
||||
}
|
||||
Device next = it.next();
|
||||
String mac = next.getMac();
|
||||
String name = next.getName();
|
||||
if (mac.equals(str) && name.startsWith("My_")) {
|
||||
z = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!z) {
|
||||
Log.i(i, "reinit-classify bluetooth");
|
||||
this.e.a("sp_key_is_last_connect_ble", (Object) false);
|
||||
this.b = null;
|
||||
this.b = BluetoothLib.d();
|
||||
this.b.a(this.c, this.d);
|
||||
this.b.a(str);
|
||||
return;
|
||||
}
|
||||
Log.i(i, "reinit-ble");
|
||||
this.e.a("sp_key_is_last_connect_ble", (Object) true);
|
||||
this.b = BleLib.b();
|
||||
this.b.a(this.c, this.d);
|
||||
this.b.a(str);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a() {
|
||||
this.b.a();
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a(String str, boolean z) {
|
||||
this.b.a(str, z);
|
||||
}
|
||||
|
||||
@Override // com.ubtrobot.jimu.bluetooth.proxy.Proxy
|
||||
public void a(String str, byte b, byte[] bArr, int i2) {
|
||||
this.b.a(str, b, bArr, i2);
|
||||
}
|
||||
}
|
10
sources/com/ubtrobot/jimu/bluetooth/proxy/Callback.java
Normal file
10
sources/com/ubtrobot/jimu/bluetooth/proxy/Callback.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.ubtrobot.jimu.bluetooth.proxy;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public abstract class Callback {
|
||||
public abstract void a(boolean z, String str);
|
||||
|
||||
public abstract void b(String str);
|
||||
|
||||
public abstract void b(String str, byte b, byte[] bArr, int i);
|
||||
}
|
30
sources/com/ubtrobot/jimu/bluetooth/proxy/Device.java
Normal file
30
sources/com/ubtrobot/jimu/bluetooth/proxy/Device.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.ubtrobot.jimu.bluetooth.proxy;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class Device implements Serializable {
|
||||
private String mac;
|
||||
private String name;
|
||||
|
||||
public Device(String str, String str2) {
|
||||
this.mac = str;
|
||||
this.name = str2;
|
||||
}
|
||||
|
||||
public String getMac() {
|
||||
return this.mac;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setMac(String str) {
|
||||
this.mac = str;
|
||||
}
|
||||
|
||||
public void setName(String str) {
|
||||
this.name = str;
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.ubtrobot.jimu.bluetooth.proxy;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface DeviceListener {
|
||||
/* renamed from: a */
|
||||
void disconnect(String str);
|
||||
|
||||
void a(String str, byte b, byte[] bArr, int i);
|
||||
|
||||
/* renamed from: a */
|
||||
void onConnectState(boolean z, String str);
|
||||
|
||||
String b(String str);
|
||||
}
|
20
sources/com/ubtrobot/jimu/bluetooth/proxy/Proxy.java
Normal file
20
sources/com/ubtrobot/jimu/bluetooth/proxy/Proxy.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.ubtrobot.jimu.bluetooth.proxy;
|
||||
|
||||
import android.content.Context;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public interface Proxy {
|
||||
public static final List<Device> a = new ArrayList();
|
||||
|
||||
void a();
|
||||
|
||||
void a(String str);
|
||||
|
||||
void a(String str, byte b, byte[] bArr, int i);
|
||||
|
||||
void a(String str, boolean z);
|
||||
|
||||
boolean a(Context context, DeviceListener deviceListener);
|
||||
}
|
20
sources/com/ubtrobot/jimu/bluetooth/utils/ByteHexHelper.java
Normal file
20
sources/com/ubtrobot/jimu/bluetooth/utils/ByteHexHelper.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.ubtrobot.jimu.bluetooth.utils;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class ByteHexHelper {
|
||||
public static String a(byte[] bArr) {
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
if (bArr == null || bArr.length <= 0) {
|
||||
return "";
|
||||
}
|
||||
for (byte b : bArr) {
|
||||
String hexString = Integer.toHexString(b & 255);
|
||||
if (hexString.length() < 2) {
|
||||
sb.append(0);
|
||||
}
|
||||
sb.append(hexString);
|
||||
sb.append(" ");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
121
sources/com/ubtrobot/jimu/bluetooth/view/BluetoothActivity.java
Normal file
121
sources/com/ubtrobot/jimu/bluetooth/view/BluetoothActivity.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.ubtrobot.jimu.bluetooth.view;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import com.ubtrobot.jimu.bluetooth.ConnectionState;
|
||||
import com.ubtrobot.jimu.bluetooth.ConnectionStateListener;
|
||||
import com.ubtrobot.jimu.bluetooth.JimuBluetoothManager;
|
||||
import com.ubtrobot.jimu.bluetooth.base.BluetoothLib;
|
||||
import com.ubtrobot.log.ALog;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
/* loaded from: classes2.dex */
|
||||
public class BluetoothActivity extends Activity {
|
||||
private static final String e = BluetoothActivity.class.getSimpleName();
|
||||
private Timer a = new Timer();
|
||||
private String b = null;
|
||||
private ConnectionStateListener c = new ConnectionStateListener() { // from class: com.ubtrobot.jimu.bluetooth.view.BluetoothActivity.2
|
||||
@Override // com.ubtrobot.jimu.bluetooth.ConnectionStateListener
|
||||
public void onConnectionStateChange(String str, ConnectionState connectionState) {
|
||||
ALog.a(BluetoothActivity.e).d(str + "Bluetooth state changed, new ConnectionState:" + connectionState);
|
||||
if (str == null) {
|
||||
return;
|
||||
}
|
||||
if (connectionState == ConnectionState.STATE_CONNECTED || connectionState == ConnectionState.STATE_DISCONNECTED) {
|
||||
BluetoothActivity.this.finish();
|
||||
}
|
||||
}
|
||||
};
|
||||
private final BroadcastReceiver d = new BroadcastReceiver() { // from class: com.ubtrobot.jimu.bluetooth.view.BluetoothActivity.3
|
||||
@Override // android.content.BroadcastReceiver
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
char c;
|
||||
String action = intent.getAction();
|
||||
ALog.a(BluetoothActivity.e).d("Receive bluetooth broadcast action: " + action);
|
||||
int hashCode = action.hashCode();
|
||||
if (hashCode != -223687943) {
|
||||
if (hashCode == 2116862345 && action.equals("android.bluetooth.device.action.BOND_STATE_CHANGED")) {
|
||||
c = 0;
|
||||
}
|
||||
c = 65535;
|
||||
} else {
|
||||
if (action.equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
|
||||
c = 1;
|
||||
}
|
||||
c = 65535;
|
||||
}
|
||||
if (c != 0) {
|
||||
return;
|
||||
}
|
||||
BluetoothDevice bluetoothDevice = (BluetoothDevice) intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
|
||||
Log.w(BluetoothActivity.e, "蓝牙设备的状态" + bluetoothDevice.getBondState());
|
||||
switch (bluetoothDevice.getBondState()) {
|
||||
case 10:
|
||||
Log.w(BluetoothActivity.e, "取消配对");
|
||||
BluetoothActivity.this.finish();
|
||||
break;
|
||||
case 11:
|
||||
Log.w(BluetoothActivity.e, "正在配对......");
|
||||
break;
|
||||
case 12:
|
||||
Log.w(BluetoothActivity.e, "完成配对");
|
||||
BluetoothActivity.this.finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override // android.app.Activity
|
||||
public void onCreate(Bundle bundle) {
|
||||
ALog.a(e).d("===== onCreate ======= ");
|
||||
super.onCreate(bundle);
|
||||
String stringExtra = getIntent().getStringExtra("MacAddr");
|
||||
if (stringExtra != null) {
|
||||
ALog.a(e).d("Bluetooth connect " + stringExtra);
|
||||
BluetoothLib.d().a(stringExtra);
|
||||
this.b = stringExtra;
|
||||
JimuBluetoothManager.d().a(this.c);
|
||||
this.a.schedule(new TimerTask() { // from class: com.ubtrobot.jimu.bluetooth.view.BluetoothActivity.1
|
||||
@Override // java.util.TimerTask, java.lang.Runnable
|
||||
public void run() {
|
||||
Log.i(BluetoothActivity.e, "Timeout and finish BluetoothActivity!");
|
||||
BluetoothActivity.this.finish();
|
||||
}
|
||||
}, 15000L);
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
IntentFilter intentFilter = new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST");
|
||||
intentFilter.addAction("android.bluetooth.device.action.BOND_STATE_CHANGED");
|
||||
intentFilter.addAction("android.bluetooth.device.action.ACL_CONNECTED");
|
||||
intentFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED");
|
||||
intentFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
|
||||
intentFilter.addAction("android.bluetooth.adapter.action.STATE_CHANGED");
|
||||
intentFilter.addAction("android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED");
|
||||
registerReceiver(this.d, intentFilter);
|
||||
}
|
||||
|
||||
@Override // android.app.Activity
|
||||
public void onDestroy() {
|
||||
ALog.a(e).d("======= onDestroy ========");
|
||||
if (this.b != null) {
|
||||
JimuBluetoothManager.d().b(this.c);
|
||||
}
|
||||
unregisterReceiver(this.d);
|
||||
this.a.cancel();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override // android.app.Activity
|
||||
public void onResume() {
|
||||
ALog.a(e).d("===== onResume ======= ");
|
||||
super.onResume();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user