Initial commit
This commit is contained in:
117
sources/com/afunx/ble/blelitelib/connector/BleConnectCompat.java
Normal file
117
sources/com/afunx/ble/blelitelib/connector/BleConnectCompat.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package com.afunx.ble.blelitelib.connector;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCallback;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import com.afunx.ble.blelitelib.log.BleLiteLog;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class BleConnectCompat {
|
||||
private static final String TAG = "BleConnectCompat";
|
||||
private final Context context;
|
||||
|
||||
public BleConnectCompat(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
private BluetoothGatt connectGattCompat(BluetoothGattCallback bluetoothGattCallback, BluetoothDevice bluetoothDevice, boolean z) {
|
||||
BleLiteLog.v(TAG, "Connecting without reflection");
|
||||
return Build.VERSION.SDK_INT >= 23 ? bluetoothDevice.connectGatt(this.context, z, bluetoothGattCallback, 2) : bluetoothDevice.connectGatt(this.context, z, bluetoothGattCallback);
|
||||
}
|
||||
|
||||
private boolean connectUsingReflection(BluetoothGatt bluetoothGatt, BluetoothGattCallback bluetoothGattCallback, boolean z) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
|
||||
BleLiteLog.v(TAG, "Connecting using reflection");
|
||||
setAutoConnectValue(bluetoothGatt, z);
|
||||
Method declaredMethod = bluetoothGatt.getClass().getDeclaredMethod("connect", Boolean.class, BluetoothGattCallback.class);
|
||||
declaredMethod.setAccessible(true);
|
||||
return ((Boolean) declaredMethod.invoke(bluetoothGatt, true, bluetoothGattCallback)).booleanValue();
|
||||
}
|
||||
|
||||
private BluetoothGatt connectUsingReflectionx(BluetoothDevice bluetoothDevice, BluetoothGattCallback bluetoothGattCallback, boolean z) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
|
||||
BleLiteLog.v(TAG, "Connecting using reflection");
|
||||
Method declaredMethod = bluetoothDevice.getClass().getDeclaredMethod("connectGatt", Context.class, Boolean.TYPE, BluetoothGattCallback.class, Integer.TYPE);
|
||||
declaredMethod.setAccessible(true);
|
||||
return (BluetoothGatt) declaredMethod.invoke(bluetoothDevice, this.context, true, bluetoothGattCallback, 2);
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private BluetoothGatt createBluetoothGatt(Object obj, BluetoothDevice bluetoothDevice) throws IllegalAccessException, InvocationTargetException, InstantiationException {
|
||||
Constructor<?> constructor = BluetoothGatt.class.getDeclaredConstructors()[0];
|
||||
constructor.setAccessible(true);
|
||||
BleLiteLog.v(TAG, "Found constructor with args count = \" + bluetoothGattConstructor.getParameterTypes().length");
|
||||
return constructor.getParameterTypes().length == 4 ? (BluetoothGatt) constructor.newInstance(this.context, obj, bluetoothDevice, 2) : (BluetoothGatt) constructor.newInstance(this.context, obj, bluetoothDevice);
|
||||
}
|
||||
|
||||
private Object getIBluetoothGatt(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
return getMethodFromClass(obj.getClass(), "getBluetoothGatt").invoke(obj, new Object[0]);
|
||||
}
|
||||
|
||||
private Object getIBluetoothManager() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||
BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
if (defaultAdapter == null) {
|
||||
return null;
|
||||
}
|
||||
return getMethodFromClass(defaultAdapter.getClass(), "getBluetoothManager").invoke(defaultAdapter, new Object[0]);
|
||||
}
|
||||
|
||||
private Method getMethodFromClass(Class<?> cls, String str) throws NoSuchMethodException {
|
||||
Method declaredMethod = cls.getDeclaredMethod(str, new Class[0]);
|
||||
declaredMethod.setAccessible(true);
|
||||
return declaredMethod;
|
||||
}
|
||||
|
||||
private void setAutoConnectValue(BluetoothGatt bluetoothGatt, boolean z) throws NoSuchFieldException, IllegalAccessException {
|
||||
Field declaredField = bluetoothGatt.getClass().getDeclaredField("mAutoConnect");
|
||||
declaredField.setAccessible(true);
|
||||
declaredField.setBoolean(bluetoothGatt, z);
|
||||
}
|
||||
|
||||
public BluetoothGatt connectGatt(BluetoothDevice bluetoothDevice, boolean z, BluetoothGattCallback bluetoothGattCallback) {
|
||||
int i;
|
||||
if (bluetoothDevice == null) {
|
||||
return null;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 24 || !z) {
|
||||
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, false);
|
||||
}
|
||||
String str = Build.PRODUCT;
|
||||
if (str != null && str.toUpperCase().contains("LenovoTB3".toUpperCase()) && (i = Build.VERSION.SDK_INT) >= 21 && i < 23) {
|
||||
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, false);
|
||||
}
|
||||
try {
|
||||
BleLiteLog.v(TAG, "Trying to connectGatt using reflection.");
|
||||
if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) {
|
||||
return connectUsingReflectionx(bluetoothDevice, bluetoothGattCallback, z);
|
||||
}
|
||||
Object iBluetoothGatt = getIBluetoothGatt(getIBluetoothManager());
|
||||
if (iBluetoothGatt == null) {
|
||||
BleLiteLog.v(TAG, "Couldn't get iBluetoothGatt object");
|
||||
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
|
||||
}
|
||||
BluetoothGatt createBluetoothGatt = createBluetoothGatt(iBluetoothGatt, bluetoothDevice);
|
||||
if (createBluetoothGatt == null) {
|
||||
BleLiteLog.v(TAG, "Couldn't create BluetoothGatt object");
|
||||
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
|
||||
}
|
||||
if (!connectUsingReflection(createBluetoothGatt, bluetoothGattCallback, true)) {
|
||||
BleLiteLog.v(TAG, "Connection using reflection failed, closing gatt");
|
||||
createBluetoothGatt.close();
|
||||
}
|
||||
return createBluetoothGatt;
|
||||
} catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException unused) {
|
||||
BleLiteLog.v(TAG, "Error during reflection");
|
||||
return connectGattCompat(bluetoothGattCallback, bluetoothDevice, true);
|
||||
}
|
||||
}
|
||||
}
|
85
sources/com/afunx/ble/blelitelib/connector/BleConnector.java
Normal file
85
sources/com/afunx/ble/blelitelib/connector/BleConnector.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.afunx.ble.blelitelib.connector;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCallback;
|
||||
import android.content.Context;
|
||||
import com.afunx.ble.blelitelib.log.BleLiteLog;
|
||||
|
||||
/* loaded from: classes.dex */
|
||||
public class BleConnector {
|
||||
private static final String TAG = "BleConnector";
|
||||
private final Context mAppContext;
|
||||
private final BluetoothDevice mBluetoothDevice;
|
||||
private BluetoothGatt mBluetoothGatt;
|
||||
private final BluetoothGattCallback mBluetoothGattCallback;
|
||||
private volatile boolean mIsClosed;
|
||||
|
||||
public synchronized void close() {
|
||||
if (!this.mIsClosed) {
|
||||
this.mIsClosed = true;
|
||||
if (this.mBluetoothGatt != null) {
|
||||
BleLiteLog.i(TAG, "close()");
|
||||
this.mBluetoothGatt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void connect() {
|
||||
if (this.mIsClosed) {
|
||||
BleLiteLog.w(TAG, "connect() has been closed already, do nothing");
|
||||
return;
|
||||
}
|
||||
if (this.mBluetoothGatt == null) {
|
||||
BleLiteLog.i(TAG, "connect() connect...");
|
||||
this.mBluetoothGatt = new BleConnectCompat(this.mAppContext).connectGatt(this.mBluetoothDevice, false, this.mBluetoothGattCallback);
|
||||
} else {
|
||||
BleLiteLog.i(TAG, "connect() reconnect...");
|
||||
this.mBluetoothGatt.connect();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized BluetoothGatt getBluetoothGatt() {
|
||||
if (this.mIsClosed) {
|
||||
BleLiteLog.w(TAG, "getBluetoothGatt() has been closed already, return null");
|
||||
return null;
|
||||
}
|
||||
return this.mBluetoothGatt;
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
private BluetoothDevice mBluetoothDevice;
|
||||
private BluetoothGatt mBluetoothGatt;
|
||||
private BluetoothGattCallback mBluetoothGattCallback;
|
||||
private Context mContext;
|
||||
|
||||
public Builder build(Context context, BluetoothDevice bluetoothDevice) {
|
||||
this.mContext = context;
|
||||
this.mBluetoothDevice = bluetoothDevice;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BleConnector create() {
|
||||
return new BleConnector(this.mContext, this.mBluetoothDevice, this.mBluetoothGatt, this.mBluetoothGattCallback);
|
||||
}
|
||||
|
||||
public Builder setGattCallback(BluetoothGattCallback bluetoothGattCallback) {
|
||||
this.mBluetoothGattCallback = bluetoothGattCallback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder build(Context context, BluetoothDevice bluetoothDevice, BluetoothGatt bluetoothGatt) {
|
||||
this.mContext = context;
|
||||
this.mBluetoothDevice = bluetoothDevice;
|
||||
this.mBluetoothGatt = bluetoothGatt;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private BleConnector(Context context, BluetoothDevice bluetoothDevice, BluetoothGatt bluetoothGatt, BluetoothGattCallback bluetoothGattCallback) {
|
||||
this.mAppContext = context.getApplicationContext();
|
||||
this.mBluetoothDevice = bluetoothDevice;
|
||||
this.mBluetoothGatt = bluetoothGatt;
|
||||
this.mBluetoothGattCallback = bluetoothGattCallback;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user