jimu-decompiled/sources/com/ubtrobot/jimu/bluetooth/base/BluetoothUtil.java
2025-05-13 19:24:51 +02:00

246 lines
8.5 KiB
Java

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);
}
}
}